]>
Commit | Line | Data |
---|---|---|
1 | /* jshint expr: true */ | |
2 | ||
3 | const expect = chai.expect; | |
4 | ||
5 | import * as WebUtil from '../app/webutil.js'; | |
6 | ||
7 | describe('WebUtil', function () { | |
8 | "use strict"; | |
9 | ||
10 | describe('settings', function () { | |
11 | ||
12 | describe('localStorage', function () { | |
13 | let chrome = window.chrome; | |
14 | before(function () { | |
15 | chrome = window.chrome; | |
16 | window.chrome = null; | |
17 | }); | |
18 | after(function () { | |
19 | window.chrome = chrome; | |
20 | }); | |
21 | ||
22 | let origLocalStorage; | |
23 | beforeEach(function () { | |
24 | origLocalStorage = Object.getOwnPropertyDescriptor(window, "localStorage"); | |
25 | ||
26 | Object.defineProperty(window, "localStorage", {value: {}}); | |
27 | if (window.localStorage.setItem !== undefined) { | |
28 | // Object.defineProperty() doesn't work properly in old | |
29 | // versions of Chrome | |
30 | this.skip(); | |
31 | } | |
32 | ||
33 | window.localStorage.setItem = sinon.stub(); | |
34 | window.localStorage.getItem = sinon.stub(); | |
35 | window.localStorage.removeItem = sinon.stub(); | |
36 | ||
37 | return WebUtil.initSettings(); | |
38 | }); | |
39 | afterEach(function () { | |
40 | if (origLocalStorage !== undefined) { | |
41 | Object.defineProperty(window, "localStorage", origLocalStorage); | |
42 | } | |
43 | }); | |
44 | ||
45 | describe('writeSetting', function () { | |
46 | it('should save the setting value to local storage', function () { | |
47 | WebUtil.writeSetting('test', 'value'); | |
48 | expect(window.localStorage.setItem).to.have.been.calledWithExactly('test', 'value'); | |
49 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
50 | }); | |
51 | }); | |
52 | ||
53 | describe('setSetting', function () { | |
54 | it('should update the setting but not save to local storage', function () { | |
55 | WebUtil.setSetting('test', 'value'); | |
56 | expect(window.localStorage.setItem).to.not.have.been.called; | |
57 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
58 | }); | |
59 | }); | |
60 | ||
61 | describe('readSetting', function () { | |
62 | it('should read the setting value from local storage', function () { | |
63 | localStorage.getItem.returns('value'); | |
64 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
65 | }); | |
66 | ||
67 | it('should return the default value when not in local storage', function () { | |
68 | expect(WebUtil.readSetting('test', 'default')).to.equal('default'); | |
69 | }); | |
70 | ||
71 | it('should return the cached value even if local storage changed', function () { | |
72 | localStorage.getItem.returns('value'); | |
73 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
74 | localStorage.getItem.returns('something else'); | |
75 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
76 | }); | |
77 | ||
78 | it('should cache the value even if it is not initially in local storage', function () { | |
79 | expect(WebUtil.readSetting('test')).to.be.null; | |
80 | localStorage.getItem.returns('value'); | |
81 | expect(WebUtil.readSetting('test')).to.be.null; | |
82 | }); | |
83 | ||
84 | it('should return the default value always if the first read was not in local storage', function () { | |
85 | expect(WebUtil.readSetting('test', 'default')).to.equal('default'); | |
86 | localStorage.getItem.returns('value'); | |
87 | expect(WebUtil.readSetting('test', 'another default')).to.equal('another default'); | |
88 | }); | |
89 | ||
90 | it('should return the last local written value', function () { | |
91 | localStorage.getItem.returns('value'); | |
92 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
93 | WebUtil.writeSetting('test', 'something else'); | |
94 | expect(WebUtil.readSetting('test')).to.equal('something else'); | |
95 | }); | |
96 | }); | |
97 | ||
98 | // this doesn't appear to be used anywhere | |
99 | describe('eraseSetting', function () { | |
100 | it('should remove the setting from local storage', function () { | |
101 | WebUtil.eraseSetting('test'); | |
102 | expect(window.localStorage.removeItem).to.have.been.calledWithExactly('test'); | |
103 | }); | |
104 | }); | |
105 | }); | |
106 | ||
107 | describe('chrome.storage', function () { | |
108 | let chrome = window.chrome; | |
109 | let settings = {}; | |
110 | before(function () { | |
111 | chrome = window.chrome; | |
112 | window.chrome = { | |
113 | storage: { | |
114 | sync: { | |
115 | get(cb) { cb(settings); }, | |
116 | set() {}, | |
117 | remove() {} | |
118 | } | |
119 | } | |
120 | }; | |
121 | }); | |
122 | after(function () { | |
123 | window.chrome = chrome; | |
124 | }); | |
125 | ||
126 | const csSandbox = sinon.createSandbox(); | |
127 | ||
128 | beforeEach(function () { | |
129 | settings = {}; | |
130 | csSandbox.spy(window.chrome.storage.sync, 'set'); | |
131 | csSandbox.spy(window.chrome.storage.sync, 'remove'); | |
132 | return WebUtil.initSettings(); | |
133 | }); | |
134 | afterEach(function () { | |
135 | csSandbox.restore(); | |
136 | }); | |
137 | ||
138 | describe('writeSetting', function () { | |
139 | it('should save the setting value to chrome storage', function () { | |
140 | WebUtil.writeSetting('test', 'value'); | |
141 | expect(window.chrome.storage.sync.set).to.have.been.calledWithExactly(sinon.match({ test: 'value' })); | |
142 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
143 | }); | |
144 | }); | |
145 | ||
146 | describe('setSetting', function () { | |
147 | it('should update the setting but not save to chrome storage', function () { | |
148 | WebUtil.setSetting('test', 'value'); | |
149 | expect(window.chrome.storage.sync.set).to.not.have.been.called; | |
150 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
151 | }); | |
152 | }); | |
153 | ||
154 | describe('readSetting', function () { | |
155 | it('should read the setting value from chrome storage', function () { | |
156 | settings.test = 'value'; | |
157 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
158 | }); | |
159 | ||
160 | it('should return the default value when not in chrome storage', function () { | |
161 | expect(WebUtil.readSetting('test', 'default')).to.equal('default'); | |
162 | }); | |
163 | ||
164 | it('should return the last local written value', function () { | |
165 | settings.test = 'value'; | |
166 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
167 | WebUtil.writeSetting('test', 'something else'); | |
168 | expect(WebUtil.readSetting('test')).to.equal('something else'); | |
169 | }); | |
170 | }); | |
171 | ||
172 | // this doesn't appear to be used anywhere | |
173 | describe('eraseSetting', function () { | |
174 | it('should remove the setting from chrome storage', function () { | |
175 | WebUtil.eraseSetting('test'); | |
176 | expect(window.chrome.storage.sync.remove).to.have.been.calledWithExactly('test'); | |
177 | }); | |
178 | }); | |
179 | }); | |
180 | }); | |
181 | }); |