ActivityStreamPrefs.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {DefaultPrefs, Prefs} from "lib/ActivityStreamPrefs.jsm";
  2. const TEST_PREF_CONFIG = new Map([
  3. ["foo", {value: true}],
  4. ["bar", {value: "BAR"}],
  5. ["baz", {value: 1}],
  6. ["qux", {value: "foo", value_local_dev: "foofoo"}],
  7. ]);
  8. describe("ActivityStreamPrefs", () => {
  9. describe("Prefs", () => {
  10. let p;
  11. beforeEach(() => {
  12. p = new Prefs();
  13. });
  14. it("should have get, set, and observe methods", () => {
  15. assert.property(p, "get");
  16. assert.property(p, "set");
  17. assert.property(p, "observe");
  18. });
  19. describe("#observeBranch", () => {
  20. let listener;
  21. beforeEach(() => {
  22. p._prefBranch = {addObserver: sinon.stub()};
  23. listener = {onPrefChanged: sinon.stub()};
  24. p.observeBranch(listener);
  25. });
  26. it("should add an observer", () => {
  27. assert.calledOnce(p._prefBranch.addObserver);
  28. assert.calledWith(p._prefBranch.addObserver, "");
  29. });
  30. it("should store the listener", () => {
  31. assert.equal(p._branchObservers.size, 1);
  32. assert.ok(p._branchObservers.has(listener));
  33. });
  34. it("should call listener's onPrefChanged", () => {
  35. p._branchObservers.get(listener)();
  36. assert.calledOnce(listener.onPrefChanged);
  37. });
  38. });
  39. describe("#ignoreBranch", () => {
  40. let listener;
  41. beforeEach(() => {
  42. p._prefBranch = {
  43. addObserver: sinon.stub(),
  44. removeObserver: sinon.stub(),
  45. };
  46. listener = {};
  47. p.observeBranch(listener);
  48. });
  49. it("should remove the observer", () => {
  50. p.ignoreBranch(listener);
  51. assert.calledOnce(p._prefBranch.removeObserver);
  52. assert.calledWith(p._prefBranch.removeObserver, p._prefBranch.addObserver.firstCall.args[0]);
  53. });
  54. it("should remove the listener", () => {
  55. assert.equal(p._branchObservers.size, 1);
  56. p.ignoreBranch(listener);
  57. assert.equal(p._branchObservers.size, 0);
  58. });
  59. });
  60. });
  61. describe("DefaultPrefs", () => {
  62. describe("#init", () => {
  63. let defaultPrefs;
  64. let sandbox;
  65. beforeEach(() => {
  66. sandbox = sinon.createSandbox();
  67. defaultPrefs = new DefaultPrefs(TEST_PREF_CONFIG);
  68. sinon.stub(defaultPrefs, "set");
  69. });
  70. afterEach(() => {
  71. sandbox.restore();
  72. });
  73. it("should initialize a boolean pref", () => {
  74. defaultPrefs.init();
  75. assert.calledWith(defaultPrefs.set, "foo", true);
  76. });
  77. it("should not initialize a pref if a default exists", () => {
  78. defaultPrefs.prefs.foo = false;
  79. defaultPrefs.init();
  80. assert.neverCalledWith(defaultPrefs.set, "foo", true);
  81. });
  82. it("should initialize a string pref", () => {
  83. defaultPrefs.init();
  84. assert.calledWith(defaultPrefs.set, "bar", "BAR");
  85. });
  86. it("should initialize a integer pref", () => {
  87. defaultPrefs.init();
  88. assert.calledWith(defaultPrefs.set, "baz", 1);
  89. });
  90. it("should initialize a pref with value if Firefox is not a local build", () => {
  91. defaultPrefs.init();
  92. assert.calledWith(defaultPrefs.set, "qux", "foo");
  93. });
  94. it("should initialize a pref with value_local_dev if Firefox is a local build", () => {
  95. sandbox.stub(global.AppConstants, "MOZILLA_OFFICIAL").value(false);
  96. defaultPrefs.init();
  97. assert.calledWith(defaultPrefs.set, "qux", "foofoo");
  98. });
  99. });
  100. });
  101. });