summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/lib/ActivityStreamPrefs.test.js
blob: bff1708ef744baa5d3b75e198f6d8c3ac7339abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { DefaultPrefs, Prefs } from "lib/ActivityStreamPrefs.sys.mjs";

const TEST_PREF_CONFIG = new Map([
  ["foo", { value: true }],
  ["bar", { value: "BAR" }],
  ["baz", { value: 1 }],
  ["qux", { value: "foo", value_local_dev: "foofoo" }],
]);

describe("ActivityStreamPrefs", () => {
  describe("Prefs", () => {
    let p;
    beforeEach(() => {
      p = new Prefs();
    });
    it("should have get, set, and observe methods", () => {
      assert.property(p, "get");
      assert.property(p, "set");
      assert.property(p, "observe");
    });
    describe("#observeBranch", () => {
      let listener;
      beforeEach(() => {
        p._prefBranch = { addObserver: sinon.stub() };
        listener = { onPrefChanged: sinon.stub() };
        p.observeBranch(listener);
      });
      it("should add an observer", () => {
        assert.calledOnce(p._prefBranch.addObserver);
        assert.calledWith(p._prefBranch.addObserver, "");
      });
      it("should store the listener", () => {
        assert.equal(p._branchObservers.size, 1);
        assert.ok(p._branchObservers.has(listener));
      });
      it("should call listener's onPrefChanged", () => {
        p._branchObservers.get(listener)();

        assert.calledOnce(listener.onPrefChanged);
      });
    });
    describe("#ignoreBranch", () => {
      let listener;
      beforeEach(() => {
        p._prefBranch = {
          addObserver: sinon.stub(),
          removeObserver: sinon.stub(),
        };
        listener = {};
        p.observeBranch(listener);
      });
      it("should remove the observer", () => {
        p.ignoreBranch(listener);

        assert.calledOnce(p._prefBranch.removeObserver);
        assert.calledWith(
          p._prefBranch.removeObserver,
          p._prefBranch.addObserver.firstCall.args[0]
        );
      });
      it("should remove the listener", () => {
        assert.equal(p._branchObservers.size, 1);

        p.ignoreBranch(listener);

        assert.equal(p._branchObservers.size, 0);
      });
    });
  });

  describe("DefaultPrefs", () => {
    describe("#init", () => {
      let defaultPrefs;
      let sandbox;
      beforeEach(() => {
        sandbox = sinon.createSandbox();
        defaultPrefs = new DefaultPrefs(TEST_PREF_CONFIG);
        sinon.stub(defaultPrefs, "set");
      });
      afterEach(() => {
        sandbox.restore();
      });
      it("should initialize a boolean pref", () => {
        defaultPrefs.init();
        assert.calledWith(defaultPrefs.set, "foo", true);
      });
      it("should not initialize a pref if a default exists", () => {
        defaultPrefs.prefs.set("foo", false);

        defaultPrefs.init();

        assert.neverCalledWith(defaultPrefs.set, "foo", true);
      });
      it("should initialize a string pref", () => {
        defaultPrefs.init();
        assert.calledWith(defaultPrefs.set, "bar", "BAR");
      });
      it("should initialize a integer pref", () => {
        defaultPrefs.init();
        assert.calledWith(defaultPrefs.set, "baz", 1);
      });
      it("should initialize a pref with value if Firefox is not a local build", () => {
        defaultPrefs.init();
        assert.calledWith(defaultPrefs.set, "qux", "foo");
      });
      it("should initialize a pref with value_local_dev if Firefox is a local build", () => {
        sandbox.stub(global.AppConstants, "MOZILLA_OFFICIAL").value(false);
        defaultPrefs.init();
        assert.calledWith(defaultPrefs.set, "qux", "foofoo");
      });
    });
  });
});