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