]> git.proxmox.com Git - mirror_novnc.git/blame - tests/test.webutil.js
Make afterEach() hooks work when skipping tests
[mirror_novnc.git] / tests / test.webutil.js
CommitLineData
e0750f9b
AW
1/* jshint expr: true */
2
2b5f94fa 3const expect = chai.expect;
e0750f9b
AW
4
5import * as WebUtil from '../app/webutil.js';
6
2c5491e1 7describe('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 () {
eb05b45b
PO
45 if (origLocalStorage !== undefined) {
46 Object.defineProperty(window, "localStorage", origLocalStorage);
47 }
e0750f9b
AW
48 });
49
2c5491e1
PO
50 describe('writeSetting', function () {
51 it('should save the setting value to local storage', function () {
e0750f9b
AW
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
2c5491e1
PO
58 describe('setSetting', function () {
59 it('should update the setting but not save to local storage', function () {
e0750f9b
AW
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
2c5491e1
PO
66 describe('readSetting', function () {
67 it('should read the setting value from local storage', function () {
e0750f9b
AW
68 localStorage.getItem.returns('value');
69 expect(WebUtil.readSetting('test')).to.equal('value');
70 });
71
2c5491e1 72 it('should return the default value when not in local storage', function () {
e0750f9b
AW
73 expect(WebUtil.readSetting('test', 'default')).to.equal('default');
74 });
75
2c5491e1 76 it('should return the cached value even if local storage changed', function () {
e0750f9b
AW
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
2c5491e1 83 it('should cache the value even if it is not initially in local storage', function () {
e0750f9b
AW
84 expect(WebUtil.readSetting('test')).to.be.null;
85 localStorage.getItem.returns('value');
86 expect(WebUtil.readSetting('test')).to.be.null;
87 });
88
2c5491e1 89 it('should return the default value always if the first read was not in local storage', function () {
e0750f9b
AW
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
2c5491e1 95 it('should return the last local written value', function () {
e0750f9b
AW
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
2c5491e1
PO
104 describe('eraseSetting', function () {
105 it('should remove the setting from local storage', function () {
e0750f9b
AW
106 WebUtil.eraseSetting('test');
107 expect(window.localStorage.removeItem).to.have.been.calledWithExactly('test');
108 });
109 });
110 });
111
2c5491e1 112 describe('chrome.storage', function () {
2b5f94fa
JD
113 let chrome = window.chrome;
114 let settings = {};
2c5491e1 115 before(function () {
e0750f9b
AW
116 chrome = window.chrome;
117 window.chrome = {
118 storage: {
119 sync: {
3f1cda2e
PO
120 get(cb) { cb(settings); },
121 set() {},
0e4808bf 122 remove() {}
e0750f9b
AW
123 }
124 }
125 };
126 });
2c5491e1 127 after(function () {
e0750f9b
AW
128 window.chrome = chrome;
129 });
130
2b5f94fa 131 const csSandbox = sinon.createSandbox();
e0750f9b 132
2c5491e1 133 beforeEach(function () {
e0750f9b
AW
134 settings = {};
135 csSandbox.spy(window.chrome.storage.sync, 'set');
136 csSandbox.spy(window.chrome.storage.sync, 'remove');
1c9b904d 137 return WebUtil.initSettings();
e0750f9b 138 });
2c5491e1 139 afterEach(function () {
e0750f9b
AW
140 csSandbox.restore();
141 });
142
2c5491e1
PO
143 describe('writeSetting', function () {
144 it('should save the setting value to chrome storage', function () {
e0750f9b
AW
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
2c5491e1
PO
151 describe('setSetting', function () {
152 it('should update the setting but not save to chrome storage', function () {
e0750f9b
AW
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
2c5491e1
PO
159 describe('readSetting', function () {
160 it('should read the setting value from chrome storage', function () {
e0750f9b
AW
161 settings.test = 'value';
162 expect(WebUtil.readSetting('test')).to.equal('value');
163 });
164
2c5491e1 165 it('should return the default value when not in chrome storage', function () {
e0750f9b
AW
166 expect(WebUtil.readSetting('test', 'default')).to.equal('default');
167 });
168
2c5491e1 169 it('should return the last local written value', function () {
e0750f9b
AW
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
2c5491e1
PO
178 describe('eraseSetting', function () {
179 it('should remove the setting from chrome storage', function () {
e0750f9b
AW
180 WebUtil.eraseSetting('test');
181 expect(window.chrome.storage.sync.remove).to.have.been.calledWithExactly('test');
182 });
183 });
184 });
185 });
186});