ChatStore-test.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. jest
  2. .dontMock('../ChatStore')
  3. .dontMock('../../constants/ChatConstants');
  4. require('es6-shim');
  5. const ChatConstants = require('../../constants/ChatConstants');
  6. const AppDispatcher = require('../../dispatcher/AppDispatcher');
  7. const ChatStore = require('../ChatStore');
  8. describe('ChatStore', () => {
  9. var actionToggleVisibility = {
  10. source: 'VIEW_ACTION',
  11. action: {
  12. actionType: ChatConstants.TOGGLE_VISIBILITY
  13. }
  14. };
  15. var actionSubmitMessage = {
  16. source: 'VIEW_ACTION',
  17. action: {
  18. actionType: ChatConstants.SUBMIT_MESSAGE,
  19. message: 'hi there',
  20. className: 'white right',
  21. received: false
  22. }
  23. };
  24. var state;
  25. const callback = function(cb) {
  26. AppDispatcher.register.mock.calls[0][0](cb);
  27. state = ChatStore.getState();
  28. };
  29. it('should register a callback with the dispatcher', () => {
  30. expect(AppDispatcher.register.mock.calls.length).toBe(1);
  31. });
  32. it('should return initial data from chat store', () => {
  33. state = ChatStore.getState();
  34. expect(state.messages.isEmpty()).toBe(true);
  35. expect(state.isChatHidden).toBe(true);
  36. expect(state.unseenCount).toBe(0);
  37. });
  38. it('should toggle visibility', () => {
  39. callback(actionToggleVisibility);
  40. expect(state.isChatHidden).toBe(false);
  41. });
  42. it('should store submitted message', () => {
  43. callback(actionSubmitMessage);
  44. expect(state.messages.size).toBe(1);
  45. expect(state.messages.getIn([0, 'message'])).toBe('hi there');
  46. expect(state.unseenCount).toBe(0);
  47. });
  48. it('should increase unseen count if chat is hidden', () => {
  49. callback(actionToggleVisibility);
  50. actionSubmitMessage.action.received = true;
  51. actionSubmitMessage.action.message = 'hello';
  52. actionSubmitMessage.action.className = 'black left';
  53. callback(actionSubmitMessage);
  54. callback(actionSubmitMessage);
  55. callback(actionSubmitMessage);
  56. expect(state.messages.size).toBe(4);
  57. expect(state.unseenCount).toBe(3);
  58. expect(state.isChatHidden).toBe(true);
  59. // also check the order of the messages
  60. expect(state.messages.getIn([0, 'message'])).toBe('hi there');
  61. expect(state.messages.getIn([3, 'message'])).toBe('hello');
  62. expect(state.messages.getIn([3, 'className'])).toBe('black left');
  63. });
  64. it('should set unseen count to 0 when chat becomes visible', () => {
  65. callback(actionToggleVisibility);
  66. expect(state.unseenCount).toBe(0);
  67. });
  68. });