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