ASRouterFeed.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {_ASRouter, ASRouter} from "lib/ASRouter.jsm";
  2. import {FAKE_LOCAL_PROVIDER, FakeRemotePageManager} from "./constants";
  3. import {ASRouterFeed} from "lib/ASRouterFeed.jsm";
  4. import {actionTypes as at} from "common/Actions.jsm";
  5. import {GlobalOverrider} from "test/unit/utils";
  6. describe("ASRouterFeed", () => {
  7. let Router;
  8. let feed;
  9. let channel;
  10. let sandbox;
  11. let storage;
  12. let globals;
  13. let FakeBookmarkPanelHub;
  14. beforeEach(() => {
  15. sandbox = sinon.createSandbox();
  16. globals = new GlobalOverrider();
  17. FakeBookmarkPanelHub = {
  18. init: sandbox.stub(),
  19. uninit: sandbox.stub(),
  20. };
  21. globals.set("BookmarkPanelHub", FakeBookmarkPanelHub);
  22. Router = new _ASRouter({providers: [FAKE_LOCAL_PROVIDER]});
  23. storage = {
  24. get: sandbox.stub().returns(Promise.resolve([])),
  25. set: sandbox.stub().returns(Promise.resolve()),
  26. };
  27. feed = new ASRouterFeed({router: Router}, storage);
  28. channel = new FakeRemotePageManager();
  29. feed.store = {
  30. _messageChannel: {channel},
  31. getState: () => ({}),
  32. dbStorage: {getDbTable: sandbox.stub().returns({})},
  33. };
  34. });
  35. afterEach(() => {
  36. sandbox.restore();
  37. });
  38. it("should set .router to the ASRouter singleton if none is specified in options", () => {
  39. feed = new ASRouterFeed();
  40. assert.equal(feed.router, ASRouter);
  41. feed = new ASRouterFeed({});
  42. assert.equal(feed.router, ASRouter);
  43. });
  44. describe("#onAction: INIT", () => {
  45. it("should initialize the ASRouter if it is not initialized", () => {
  46. sandbox.stub(feed, "enable");
  47. feed.onAction({type: at.INIT});
  48. assert.calledOnce(feed.enable);
  49. });
  50. it("should initialize ASRouter", async () => {
  51. sandbox.stub(Router, "init").returns(Promise.resolve());
  52. await feed.enable();
  53. assert.calledWith(Router.init, channel);
  54. assert.calledOnce(feed.store.dbStorage.getDbTable);
  55. assert.calledWithExactly(feed.store.dbStorage.getDbTable, "snippets");
  56. });
  57. it("should not re-initialize the ASRouter if it is already initialized", async () => {
  58. // Router starts initialized
  59. await Router.init(new FakeRemotePageManager(), storage, () => {});
  60. sinon.stub(Router, "init");
  61. // call .onAction with INIT
  62. feed.onAction({type: at.INIT});
  63. assert.notCalled(Router.init);
  64. });
  65. });
  66. describe("#onAction: UNINIT", () => {
  67. it("should uninitialize the ASRouter", async () => {
  68. await Router.init(new FakeRemotePageManager(), storage, () => {});
  69. sinon.stub(Router, "uninit");
  70. feed.onAction({type: at.UNINIT});
  71. assert.calledOnce(Router.uninit);
  72. });
  73. });
  74. });