Browse Source

add tests for chat store

romanmatiasko 9 years ago
parent
commit
96091d091a
3 changed files with 93 additions and 0 deletions
  1. 5 0
      jest-preprocessor.js
  2. 9 0
      package.json
  3. 79 0
      src/js/stores/__tests__/ChatStore-test.js

+ 5 - 0
jest-preprocessor.js

@@ -0,0 +1,5 @@
+var babel = require('babel');
+
+module.exports.process = function(sourceText, sourcePath) {
+  return babel.transform(sourceText).code;
+};

+ 9 - 0
package.json

@@ -45,5 +45,14 @@
   },
   "engines": {
     "node": "^0.12.0"
+  },
+  "jest": {
+    "scriptPreprocessor": "./jest-preprocessor.js",
+    "unmockedModulePathPatterns": [
+      "eventemmiter2",
+      "immutable",
+      "react",
+      "es6-shim"
+    ]
   }
 }

+ 79 - 0
src/js/stores/__tests__/ChatStore-test.js

@@ -0,0 +1,79 @@
+jest
+  .dontMock('../ChatStore')
+  .dontMock('../../constants/ChatConstants');
+
+require('es6-shim');
+const ChatConstants = require('../../constants/ChatConstants');
+const AppDispatcher = require('../../dispatcher/AppDispatcher');
+const ChatStore = require('../ChatStore');
+
+describe('ChatStore', () => {
+  
+  var actionToggleVisibility = {
+    source: 'VIEW_ACTION',
+    action: {
+      actionType: ChatConstants.TOGGLE_VISIBILITY
+    }
+  };
+  var actionSubmitMessage = {
+    source: 'VIEW_ACTION',
+    action: {
+      actionType: ChatConstants.SUBMIT_MESSAGE,
+      message: 'hi there',
+      className: 'white right',
+      received: false
+    }
+  };
+
+  var state;
+
+  const callback = function(cb) {
+    AppDispatcher.register.mock.calls[0][0](cb);
+    state = ChatStore.getState();
+  };
+
+  it('should register a callback with the dispatcher', () => {
+    expect(AppDispatcher.register.mock.calls.length).toBe(1);
+  });
+
+  it('should return initial data from chat store', () => {
+    state = ChatStore.getState();
+    expect(state.messages.isEmpty()).toBe(true);
+    expect(state.isChatHidden).toBe(true);
+    expect(state.unseenCount).toBe(0);
+  });
+
+  it('should toggle visibility', () => {
+    callback(actionToggleVisibility);
+    expect(state.isChatHidden).toBe(false);
+  });
+
+  it('should store submitted message', () => {
+    callback(actionSubmitMessage);
+    expect(state.messages.size).toBe(1);
+    expect(state.messages.getIn([0, 'message'])).toBe('hi there');
+    expect(state.unseenCount).toBe(0);
+  });
+
+  it('should increase unseen count if chat is hidden', () => {
+    callback(actionToggleVisibility);
+    actionSubmitMessage.action.received = true;
+    actionSubmitMessage.action.message = 'hello';
+    actionSubmitMessage.action.className = 'black left';
+    callback(actionSubmitMessage);
+    callback(actionSubmitMessage);
+    callback(actionSubmitMessage);
+    expect(state.messages.size).toBe(4);
+    expect(state.unseenCount).toBe(3);
+    expect(state.isChatHidden).toBe(true);
+    // also check the order of the messages
+    expect(state.messages.getIn([0, 'message'])).toBe('hi there');
+    expect(state.messages.getIn([3, 'message'])).toBe('hello');
+    expect(state.messages.getIn([3, 'className'])).toBe('black left');
+  });
+
+  it('should set unseen count to 0 when chat becomes visible', () => {
+    callback(actionToggleVisibility);
+    expect(state.unseenCount).toBe(0);
+  });
+});