]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/test.webutil.js
1 /* jshint expr: true */
3 const expect
= chai
.expect
;
5 import * as WebUtil
from '../app/webutil.js';
7 describe('WebUtil', function () {
10 describe('settings', function () {
12 describe('localStorage', function () {
13 let chrome
= window
.chrome
;
15 chrome
= window
.chrome
;
19 window
.chrome
= chrome
;
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
31 Object
.defineProperty(window
, "localStorage", {value
: {}});
32 if (window
.localStorage
.setItem
!== undefined) {
33 // Object.defineProperty() doesn't work properly in old
38 window
.localStorage
.setItem
= sinon
.stub();
39 window
.localStorage
.getItem
= sinon
.stub();
40 window
.localStorage
.removeItem
= sinon
.stub();
42 return WebUtil
.initSettings();
44 afterEach(function () {
45 Object
.defineProperty(window
, "localStorage", origLocalStorage
);
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');
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');
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');
70 it('should return the default value when not in local storage', function () {
71 expect(WebUtil
.readSetting('test', 'default')).to
.equal('default');
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');
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;
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');
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');
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');
110 describe('chrome.storage', function () {
111 let chrome
= window
.chrome
;
114 chrome
= window
.chrome
;
118 get(cb
) { cb(settings
); },
126 window
.chrome
= chrome
;
129 const csSandbox
= sinon
.createSandbox();
131 beforeEach(function () {
133 csSandbox
.spy(window
.chrome
.storage
.sync
, 'set');
134 csSandbox
.spy(window
.chrome
.storage
.sync
, 'remove');
135 return WebUtil
.initSettings();
137 afterEach(function () {
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');
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');
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');
163 it('should return the default value when not in chrome storage', function () {
164 expect(WebUtil
.readSetting('test', 'default')).to
.equal('default');
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');
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');