Skip to content

Commit

Permalink
Fix bug with using underscore in prefixes
Browse files Browse the repository at this point in the history
index.js - code clean up. Delete duplicated code
utils.js - add trim function toSpaceCase. Last character might be replaced with space, that causes bugs in other functions
index.test.js - code clean up && styling
utils.test.js - add additional test cases for to space case converter
package.json - bump the version
  • Loading branch information
creynir committed Nov 4, 2018
1 parent 299c205 commit 55551ca
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vuex-socketio",
"description": "A Socket.io-client Plugin for Vuex",
"version": "0.3.0",
"version": "0.3.1",
"main": "dist/build.js",
"author": "Mikhail Rogov",
"license": "MIT",
Expand Down
21 changes: 8 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ export default function createSocketIoPlugin (socket, { converter = toUppSnakeCa
function bindMutationsToSocket (options) {
Object.entries(options.store._mutations).forEach(([mutationName, funcArr]) => {
if (checkType(mutationName, options.socketNsp + options.onPrefix, options.modulesNspList)) {
let channelName = getChannelName(mutationName, options.onPrefix);
channelName = options.defaultChannels.find(item => item === channelName.toLowerCase()) || options.converter(channelName);
funcArr.forEach((func) => {
options.socket.on(channelName, (payload) =>
func(payload));
});
bindFunctionToListener(mutationName, funcArr, options);
}
});
}
Expand All @@ -63,7 +58,7 @@ function bindMutationsToSocket (options) {
function bindActionsToSocket (options) {
Object.entries(options.store._actions).forEach(([actionName, funcArr]) => {
if (checkType(actionName, options.socketNsp + options.onPrefix, options.modulesNspList)) {
bindActionToListener(actionName, funcArr, options);
bindFunctionToListener(actionName, funcArr, options);
}
if (checkType(actionName, options.socketNsp + options.emitPrefix, options.modulesNspList)) {
bindActionToEmitter(actionName, funcArr, options);
Expand All @@ -75,17 +70,17 @@ function bindActionsToSocket (options) {
});
}

/** Bind action to socket listener if its name contains onPrefix
* @param actionName
/** Bind store function to socket listener if its name contains onPrefix
* @param functionName
* @param funcArr
* @param options
* @api private
*/
function bindActionToListener (actionName, funcArr, options) {
let channelName = getChannelName(actionName, options.onPrefix);
channelName = options.defaultChannels.find(item => item === channelName.toLowerCase()) || options.converter(channelName);
function bindFunctionToListener (functionName, funcArr, options) {
const channelName = getChannelName(functionName, options.onPrefix);
const fChannelName = options.defaultChannels.find(item => item === channelName.toLowerCase()) || options.converter(channelName);
funcArr.forEach((func) => {
options.socket.on(channelName, (payload) =>
options.socket.on(fChannelName, (payload) =>
func(payload));
});
}
Expand Down
43 changes: 14 additions & 29 deletions src/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,42 +281,27 @@ describe('main', function () {
const bindMutationsToSocket = pluginAPI.__get__('bindMutationsToSocket');

it('should bind mutation to default channel listener', function () {
const mockChannelName = 'Connect';
const mockChannel = 'connect';
const bindFunctionToListenerStub = sinon.stub();
pluginAPI.__Rewire__('bindFunctionToListener', bindFunctionToListenerStub);
checkTypeStub.returns(true);
options.store._mutations = { socketOnConnect: [onConnectStub] };
getChannelNameStub.onFirstCall().returns(mockChannelName);

bindMutationsToSocket(options);
expect(socketOnStub.getCall(0).args[0]).to.eql(mockChannel);
expect(socketOnStub.getCall(0).args[1]).to.be.a('function');
});

it('should bind mutation to MESSAGE channel listener', function () {
const mockChannelName = 'Message';
const mockChannel = 'MESSAGE';
checkTypeStub.returns(true);
options.store._mutations = { socketOnMessage: [onMessageStub] };
options.converter.returns(mockChannel);
getChannelNameStub.onFirstCall().returns(mockChannelName);

bindMutationsToSocket(options);
expect(socketOnStub.getCall(0).args[0]).to.eql(mockChannel);
expect(socketOnStub.getCall(0).args[1]).to.be.a('function');
expect(bindFunctionToListenerStub).to.have.been.callCount(1);
});
});
describe('bindActionsToSocket', function () {
let bindActionToListenerStub;
let bindFunctionToListenerStub;
let bindActionToEmitterStub;
let bindDefaultActionToSocketStub;
const bindActionsToSocket = pluginAPI.__get__('bindActionsToSocket');

beforeEach(function () {
bindActionToListenerStub = sinon.stub();
bindFunctionToListenerStub = sinon.stub();
bindActionToEmitterStub = sinon.stub();
bindDefaultActionToSocketStub = sinon.stub();
pluginAPI.__Rewire__('checkType', checkTypeStub);
pluginAPI.__Rewire__('bindActionToListener', bindActionToListenerStub);
pluginAPI.__Rewire__('bindFunctionToListener', bindFunctionToListenerStub);
pluginAPI.__Rewire__('bindActionToEmitter', bindActionToEmitterStub);
pluginAPI.__Rewire__('bindDefaultActionToSocket', bindDefaultActionToSocketStub);
});
Expand All @@ -326,15 +311,15 @@ describe('main', function () {
pluginAPI.__ResetDependency__('bindDefaultActionToSocket');
});

it('should call binActionToListener', function () {
it('should call bindFunctionToListener', function () {
checkTypeStub.onFirstCall().returns(true);
options.store._actions = {
socketOnMessage: [function () {
}]
};
bindActionsToSocket(options);

expect(bindActionToListenerStub).to.have.been.callCount(1);
expect(bindFunctionToListenerStub).to.have.been.callCount(1);
expect(bindActionToEmitterStub).to.have.been.callCount(0);
expect(bindDefaultActionToSocketStub).to.have.been.callCount(0);
});
Expand All @@ -348,7 +333,7 @@ describe('main', function () {
bindActionsToSocket(options);

expect(bindActionToEmitterStub).to.have.been.callCount(1);
expect(bindActionToListenerStub).to.have.been.callCount(0);
expect(bindFunctionToListenerStub).to.have.been.callCount(0);
expect(bindDefaultActionToSocketStub).to.have.been.callCount(0);
});

Expand All @@ -362,20 +347,20 @@ describe('main', function () {
bindActionsToSocket(options);

expect(bindDefaultActionToSocketStub).to.have.been.callCount(1);
expect(bindActionToListenerStub).to.have.been.callCount(0);
expect(bindFunctionToListenerStub).to.have.been.callCount(0);
expect(bindActionToEmitterStub).to.have.been.callCount(0);
});
});
describe('bindActionToListener', function () {
const bindActionToListener = pluginAPI.__get__('bindActionToListener');
describe('bindFunctionToListener', function () {
const bindFunctionToListener = pluginAPI.__get__('bindFunctionToListener');

it('should subscribe action to default channel', function () {
const mockChannelName = 'Connect';
const mockChannel = 'connect';
const mockActionName = 'socketOnConnect';
getChannelNameStub.onFirstCall().returns(mockChannelName);

bindActionToListener(mockActionName, [onConnectStub], options);
bindFunctionToListener(mockActionName, [onConnectStub], options);
expect(socketOnStub.getCall(0).args[0]).to.eql(mockChannel);
expect(socketOnStub.getCall(0).args[1]).to.be.a('function');
});
Expand All @@ -387,7 +372,7 @@ describe('main', function () {
options.converter.returns(mockChannel);
getChannelNameStub.onFirstCall().returns(mockChannelName);

bindActionToListener(mockActionName, [onMessageStub], options);
bindFunctionToListener(mockActionName, [onMessageStub], options);
expect(socketOnStub.getCall(0).args[0]).to.eql(mockChannel);
expect(socketOnStub.getCall(0).args[1]).to.be.a('function');
});
Expand Down
2 changes: 1 addition & 1 deletion src/tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('utils', function () {
});
});
describe('converters', function () {
const mockStrings = ['someString', 'SomeString', 'some-string', 'some_string', 'SOME-STRING', 'SOME_STRING'];
const mockStrings = ['someString ', 'SomeString', '/someString/ ', '_SomeString_', '-some-string-', 'some_string', '_SOME-STRING_', ' SOME_STRING'];

describe('toUppSnakeCase', function () {
it('should convert all strings to snake case', function () {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export function toUppSnakeCase (string) {
*/
function toSpaceCase (string) {
return string.replace(/[\W_]/g, ' ')
.replace(/([a-z])([A-Z])/g, '$1 $2');
.replace(/([a-z])([A-Z])/g, '$1 $2').trim();
}

0 comments on commit 55551ca

Please sign in to comment.