]>
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 | 24 | origLocalStorage = Object.getOwnPropertyDescriptor(window, "localStorage"); |
e9118e3b PO |
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 | ||
1c9b904d | 37 | return WebUtil.initSettings(); |
e0750f9b | 38 | }); |
2c5491e1 | 39 | afterEach(function () { |
eb05b45b PO |
40 | if (origLocalStorage !== undefined) { |
41 | Object.defineProperty(window, "localStorage", origLocalStorage); | |
42 | } | |
e0750f9b AW |
43 | }); |
44 | ||
2c5491e1 PO |
45 | describe('writeSetting', function () { |
46 | it('should save the setting value to local storage', function () { | |
e0750f9b AW |
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 | ||
2c5491e1 PO |
53 | describe('setSetting', function () { |
54 | it('should update the setting but not save to local storage', function () { | |
e0750f9b AW |
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 | ||
2c5491e1 PO |
61 | describe('readSetting', function () { |
62 | it('should read the setting value from local storage', function () { | |
e0750f9b AW |
63 | localStorage.getItem.returns('value'); |
64 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
65 | }); | |
66 | ||
2c5491e1 | 67 | it('should return the default value when not in local storage', function () { |
e0750f9b AW |
68 | expect(WebUtil.readSetting('test', 'default')).to.equal('default'); |
69 | }); | |
70 | ||
2c5491e1 | 71 | it('should return the cached value even if local storage changed', function () { |
e0750f9b AW |
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 | ||
2c5491e1 | 78 | it('should cache the value even if it is not initially in local storage', function () { |
e0750f9b AW |
79 | expect(WebUtil.readSetting('test')).to.be.null; |
80 | localStorage.getItem.returns('value'); | |
81 | expect(WebUtil.readSetting('test')).to.be.null; | |
82 | }); | |
83 | ||
2c5491e1 | 84 | it('should return the default value always if the first read was not in local storage', function () { |
e0750f9b AW |
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 | ||
2c5491e1 | 90 | it('should return the last local written value', function () { |
e0750f9b AW |
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 | |
2c5491e1 PO |
99 | describe('eraseSetting', function () { |
100 | it('should remove the setting from local storage', function () { | |
e0750f9b AW |
101 | WebUtil.eraseSetting('test'); |
102 | expect(window.localStorage.removeItem).to.have.been.calledWithExactly('test'); | |
103 | }); | |
104 | }); | |
105 | }); | |
106 | ||
2c5491e1 | 107 | describe('chrome.storage', function () { |
2b5f94fa JD |
108 | let chrome = window.chrome; |
109 | let settings = {}; | |
2c5491e1 | 110 | before(function () { |
e0750f9b AW |
111 | chrome = window.chrome; |
112 | window.chrome = { | |
113 | storage: { | |
114 | sync: { | |
3f1cda2e PO |
115 | get(cb) { cb(settings); }, |
116 | set() {}, | |
0e4808bf | 117 | remove() {} |
e0750f9b AW |
118 | } |
119 | } | |
120 | }; | |
121 | }); | |
2c5491e1 | 122 | after(function () { |
e0750f9b AW |
123 | window.chrome = chrome; |
124 | }); | |
125 | ||
2b5f94fa | 126 | const csSandbox = sinon.createSandbox(); |
e0750f9b | 127 | |
2c5491e1 | 128 | beforeEach(function () { |
e0750f9b AW |
129 | settings = {}; |
130 | csSandbox.spy(window.chrome.storage.sync, 'set'); | |
131 | csSandbox.spy(window.chrome.storage.sync, 'remove'); | |
1c9b904d | 132 | return WebUtil.initSettings(); |
e0750f9b | 133 | }); |
2c5491e1 | 134 | afterEach(function () { |
e0750f9b AW |
135 | csSandbox.restore(); |
136 | }); | |
137 | ||
2c5491e1 PO |
138 | describe('writeSetting', function () { |
139 | it('should save the setting value to chrome storage', function () { | |
e0750f9b AW |
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 | ||
2c5491e1 PO |
146 | describe('setSetting', function () { |
147 | it('should update the setting but not save to chrome storage', function () { | |
e0750f9b AW |
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 | ||
2c5491e1 PO |
154 | describe('readSetting', function () { |
155 | it('should read the setting value from chrome storage', function () { | |
e0750f9b AW |
156 | settings.test = 'value'; |
157 | expect(WebUtil.readSetting('test')).to.equal('value'); | |
158 | }); | |
159 | ||
2c5491e1 | 160 | it('should return the default value when not in chrome storage', function () { |
e0750f9b AW |
161 | expect(WebUtil.readSetting('test', 'default')).to.equal('default'); |
162 | }); | |
163 | ||
2c5491e1 | 164 | it('should return the last local written value', function () { |
e0750f9b AW |
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 | |
2c5491e1 PO |
173 | describe('eraseSetting', function () { |
174 | it('should remove the setting from chrome storage', function () { | |
e0750f9b AW |
175 | WebUtil.eraseSetting('test'); |
176 | expect(window.chrome.storage.sync.remove).to.have.been.calledWithExactly('test'); | |
177 | }); | |
178 | }); | |
179 | }); | |
180 | }); | |
181 | }); |