]> git.proxmox.com Git - mirror_novnc.git/blob - tests/test.webutil.js
Make sure "undefined" can be a default parameter value
[mirror_novnc.git] / tests / test.webutil.js
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 Object.defineProperty(window, "localStorage", origLocalStorage);
46 });
47
48 describe('writeSetting', function () {
49 it('should save the setting value to local storage', function () {
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
56 describe('setSetting', function () {
57 it('should update the setting but not save to local storage', function () {
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
64 describe('readSetting', function () {
65 it('should read the setting value from local storage', function () {
66 localStorage.getItem.returns('value');
67 expect(WebUtil.readSetting('test')).to.equal('value');
68 });
69
70 it('should return the default value when not in local storage', function () {
71 expect(WebUtil.readSetting('test', 'default')).to.equal('default');
72 });
73
74 it('should return the cached value even if local storage changed', function () {
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
81 it('should cache the value even if it is not initially in local storage', function () {
82 expect(WebUtil.readSetting('test')).to.be.null;
83 localStorage.getItem.returns('value');
84 expect(WebUtil.readSetting('test')).to.be.null;
85 });
86
87 it('should return the default value always if the first read was not in local storage', function () {
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
93 it('should return the last local written value', function () {
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
102 describe('eraseSetting', function () {
103 it('should remove the setting from local storage', function () {
104 WebUtil.eraseSetting('test');
105 expect(window.localStorage.removeItem).to.have.been.calledWithExactly('test');
106 });
107 });
108 });
109
110 describe('chrome.storage', function () {
111 let chrome = window.chrome;
112 let settings = {};
113 before(function () {
114 chrome = window.chrome;
115 window.chrome = {
116 storage: {
117 sync: {
118 get(cb) { cb(settings); },
119 set() {},
120 remove() {}
121 }
122 }
123 };
124 });
125 after(function () {
126 window.chrome = chrome;
127 });
128
129 const csSandbox = sinon.createSandbox();
130
131 beforeEach(function () {
132 settings = {};
133 csSandbox.spy(window.chrome.storage.sync, 'set');
134 csSandbox.spy(window.chrome.storage.sync, 'remove');
135 return WebUtil.initSettings();
136 });
137 afterEach(function () {
138 csSandbox.restore();
139 });
140
141 describe('writeSetting', function () {
142 it('should save the setting value to chrome storage', function () {
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
149 describe('setSetting', function () {
150 it('should update the setting but not save to chrome storage', function () {
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
157 describe('readSetting', function () {
158 it('should read the setting value from chrome storage', function () {
159 settings.test = 'value';
160 expect(WebUtil.readSetting('test')).to.equal('value');
161 });
162
163 it('should return the default value when not in chrome storage', function () {
164 expect(WebUtil.readSetting('test', 'default')).to.equal('default');
165 });
166
167 it('should return the last local written value', function () {
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
176 describe('eraseSetting', function () {
177 it('should remove the setting from chrome storage', function () {
178 WebUtil.eraseSetting('test');
179 expect(window.chrome.storage.sync.remove).to.have.been.calledWithExactly('test');
180 });
181 });
182 });
183 });
184 });