PrefsFeed.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {actionCreators as ac, actionTypes as at} from "common/Actions.jsm";
  2. import {GlobalOverrider} from "test/unit/utils";
  3. import {PrefsFeed} from "lib/PrefsFeed.jsm";
  4. let overrider = new GlobalOverrider();
  5. describe("PrefsFeed", () => {
  6. let feed;
  7. let FAKE_PREFS;
  8. let sandbox;
  9. beforeEach(() => {
  10. sandbox = sinon.createSandbox();
  11. FAKE_PREFS = new Map([["foo", 1], ["bar", 2], ["baz", {value: 1, skipBroadcast: true}]]);
  12. feed = new PrefsFeed(FAKE_PREFS);
  13. const storage = {
  14. getAll: sandbox.stub().resolves(),
  15. set: sandbox.stub().resolves(),
  16. };
  17. feed.store = {
  18. dispatch: sinon.spy(),
  19. getState() { return this.state; },
  20. dbStorage: {getDbTable: sandbox.stub().returns(storage)},
  21. };
  22. // Setup for tests that don't call `init`
  23. feed._storage = storage;
  24. feed._prefs = {
  25. get: sinon.spy(item => FAKE_PREFS.get(item)),
  26. set: sinon.spy((name, value) => FAKE_PREFS.set(name, value)),
  27. observe: sinon.spy(),
  28. observeBranch: sinon.spy(),
  29. ignore: sinon.spy(),
  30. ignoreBranch: sinon.spy(),
  31. reset: sinon.stub(),
  32. };
  33. overrider.set({PrivateBrowsingUtils: {enabled: true}});
  34. });
  35. afterEach(() => {
  36. overrider.restore();
  37. sandbox.restore();
  38. });
  39. it("should set a pref when a SET_PREF action is received", () => {
  40. feed.onAction(ac.SetPref("foo", 2));
  41. assert.calledWith(feed._prefs.set, "foo", 2);
  42. });
  43. it("should dispatch PREFS_INITIAL_VALUES on init with pref values and .isPrivateBrowsingEnabled", () => {
  44. feed.onAction({type: at.INIT});
  45. assert.calledOnce(feed.store.dispatch);
  46. assert.equal(feed.store.dispatch.firstCall.args[0].type, at.PREFS_INITIAL_VALUES);
  47. const [{data}] = feed.store.dispatch.firstCall.args;
  48. assert.equal(data.foo, 1);
  49. assert.equal(data.bar, 2);
  50. assert.isTrue(data.isPrivateBrowsingEnabled);
  51. });
  52. it("should add one branch observer on init", () => {
  53. feed.onAction({type: at.INIT});
  54. assert.calledOnce(feed._prefs.observeBranch);
  55. assert.calledWith(feed._prefs.observeBranch, feed);
  56. });
  57. it("should initialise the storage on init", () => {
  58. feed.init();
  59. assert.calledOnce(feed.store.dbStorage.getDbTable);
  60. assert.calledWithExactly(feed.store.dbStorage.getDbTable, "sectionPrefs");
  61. });
  62. it("should remove the branch observer on uninit", () => {
  63. feed.onAction({type: at.UNINIT});
  64. assert.calledOnce(feed._prefs.ignoreBranch);
  65. assert.calledWith(feed._prefs.ignoreBranch, feed);
  66. });
  67. it("should send a PREF_CHANGED action when onPrefChanged is called", () => {
  68. feed.onPrefChanged("foo", 2);
  69. assert.calledWith(feed.store.dispatch, ac.BroadcastToContent({type: at.PREF_CHANGED, data: {name: "foo", value: 2}}));
  70. });
  71. it("should set storage pref on UPDATE_SECTION_PREFS", async () => {
  72. await feed.onAction({
  73. type: at.UPDATE_SECTION_PREFS,
  74. data: {id: "topsites", value: {collapsed: false}},
  75. });
  76. assert.calledWith(feed._storage.set, "topsites", {collapsed: false});
  77. });
  78. it("should set storage pref with section prefix on UPDATE_SECTION_PREFS", async () => {
  79. await feed.onAction({
  80. type: at.UPDATE_SECTION_PREFS,
  81. data: {id: "topstories", value: {collapsed: false}},
  82. });
  83. assert.calledWith(feed._storage.set, "feeds.section.topstories", {collapsed: false});
  84. });
  85. it("should catch errors on UPDATE_SECTION_PREFS", async () => {
  86. feed._storage.set.throws(new Error("foo"));
  87. assert.doesNotThrow(async () => {
  88. await feed.onAction({
  89. type: at.UPDATE_SECTION_PREFS,
  90. data: {id: "topstories", value: {collapsed: false}},
  91. });
  92. });
  93. });
  94. it("should send OnlyToMain pref update if config for pref has skipBroadcast: true", async () => {
  95. feed.onPrefChanged("baz", {value: 2, skipBroadcast: true});
  96. assert.calledWith(feed.store.dispatch, ac.OnlyToMain({type: at.PREF_CHANGED, data: {name: "baz", value: {value: 2, skipBroadcast: true}}}));
  97. });
  98. });