NewTabInit.test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {actionCreators as ac, actionTypes as at} from "common/Actions.jsm";
  2. import {NewTabInit} from "lib/NewTabInit.jsm";
  3. describe("NewTabInit", () => {
  4. let instance;
  5. let store;
  6. let STATE;
  7. const requestFromTab = portID => instance.onAction(ac.AlsoToMain(
  8. {type: at.NEW_TAB_STATE_REQUEST}, portID));
  9. beforeEach(() => {
  10. STATE = {};
  11. store = {getState: sinon.stub().returns(STATE), dispatch: sinon.stub()};
  12. instance = new NewTabInit();
  13. instance.store = store;
  14. });
  15. it("should reply with a copy of the state immediately", () => {
  16. requestFromTab(123);
  17. const resp = ac.AlsoToOneContent({type: at.NEW_TAB_INITIAL_STATE, data: STATE}, 123);
  18. assert.calledWith(store.dispatch, resp);
  19. });
  20. describe("early / simulated new tabs", () => {
  21. const simulateTabInit = portID => instance.onAction({
  22. type: at.NEW_TAB_INIT,
  23. data: {portID, simulated: true},
  24. });
  25. beforeEach(() => {
  26. simulateTabInit("foo");
  27. });
  28. it("should dispatch if not replied yet", () => {
  29. requestFromTab("foo");
  30. assert.calledWith(store.dispatch, ac.AlsoToOneContent({type: at.NEW_TAB_INITIAL_STATE, data: STATE}, "foo"));
  31. });
  32. it("should dispatch once for multiple requests", () => {
  33. requestFromTab("foo");
  34. requestFromTab("foo");
  35. requestFromTab("foo");
  36. assert.calledOnce(store.dispatch);
  37. });
  38. describe("multiple tabs", () => {
  39. beforeEach(() => {
  40. simulateTabInit("bar");
  41. });
  42. it("should dispatch once to each tab", () => {
  43. requestFromTab("foo");
  44. requestFromTab("bar");
  45. assert.calledTwice(store.dispatch);
  46. requestFromTab("foo");
  47. requestFromTab("bar");
  48. assert.calledTwice(store.dispatch);
  49. });
  50. it("should clean up when tabs close", () => {
  51. assert.propertyVal(instance._repliedEarlyTabs, "size", 2);
  52. instance.onAction(ac.AlsoToMain({type: at.NEW_TAB_UNLOAD}, "foo"));
  53. assert.propertyVal(instance._repliedEarlyTabs, "size", 1);
  54. instance.onAction(ac.AlsoToMain({type: at.NEW_TAB_UNLOAD}, "foo"));
  55. assert.propertyVal(instance._repliedEarlyTabs, "size", 1);
  56. instance.onAction(ac.AlsoToMain({type: at.NEW_TAB_UNLOAD}, "bar"));
  57. assert.propertyVal(instance._repliedEarlyTabs, "size", 0);
  58. });
  59. });
  60. });
  61. });