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