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