-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLocalStorage.spec.js
337 lines (304 loc) · 13.2 KB
/
LocalStorage.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const LocalStorageMock = require("./LocalStorage.mock");
const CyMock = require("./Cy.mock");
const CypressMock = require("./Cypress.mock");
const LocalStorage = require("../src/LocalStorage");
describe("LocalStorage", () => {
let windowLocalStorageMock;
let localStorageMock;
let localStorage;
let cyMock;
let cypressMock;
beforeAll(() => {
cyMock = new CyMock();
// Ensure that tasks are not called (plugin must be disabled in these tests)
cyMock.stubs.task.throws();
cypressMock = new CypressMock();
localStorageMock = new LocalStorageMock();
localStorage = new LocalStorage(
localStorageMock.stubs,
cyMock.stubs,
cypressMock.stubs,
);
});
afterAll(() => {
cyMock.restore();
localStorageMock.restore();
});
describe("LocalStorage", () => {
describe("save and restore methods", () => {
it("should restore values that localStorage had when save method was called", () => {
expect.assertions(2);
localStorageMock.stubs.setItem("foo", "foo-value");
localStorageMock.stubs.setItem("var", "var-value");
localStorage.saveLocalStorage();
localStorageMock.stubs.setItem("foo", "foo-new-value");
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-new-value");
localStorage.restoreLocalStorage();
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-value");
});
it("should restore values after calling localStorage clear", () => {
expect.assertions(2);
localStorageMock.stubs.clear();
expect(localStorageMock.stubs.getItem("var")).toEqual(undefined);
localStorage.restoreLocalStorage();
expect(localStorageMock.stubs.getItem("var")).toEqual("var-value");
});
it("should restore new values if Save is called again", () => {
expect.assertions(2);
localStorageMock.stubs.setItem("foo", "foo-new-value");
localStorageMock.stubs.removeItem("var");
localStorage.saveLocalStorage();
localStorageMock.stubs.setItem("foo", "foo-another-new-value");
localStorageMock.stubs.setItem("var", "foo-var-value");
localStorage.restoreLocalStorage();
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-new-value");
expect(localStorageMock.stubs.getItem("var")).toEqual(undefined);
});
});
describe("Clear method", () => {
it("should clear values in localStorage snapshot, but maintain localStorage values", () => {
expect.assertions(4);
localStorageMock.stubs.setItem("var", "foo-var-value");
localStorage.clearLocalStorageSnapshot();
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-new-value");
expect(localStorageMock.stubs.getItem("var")).toEqual("foo-var-value");
localStorage.restoreLocalStorage();
expect(localStorageMock.stubs.getItem("foo")).toEqual(undefined);
expect(localStorageMock.stubs.getItem("var")).toEqual(undefined);
});
});
});
describe("LocalStorage named snapshots", () => {
describe("save and restore methods", () => {
it("should restore values that localStorage had when save method was called", () => {
expect.assertions(3);
localStorageMock.stubs.setItem("foo", "foo-value");
localStorageMock.stubs.setItem("var", "var-value");
localStorage.saveLocalStorage("first");
localStorageMock.stubs.setItem("foo", "foo-new-value");
localStorage.saveLocalStorage("second");
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-new-value");
localStorage.restoreLocalStorage("first");
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-value");
localStorage.restoreLocalStorage("second");
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-new-value");
});
it("should clear whole localStorage if snapshot to restore does not exists", () => {
localStorage.restoreLocalStorage("fourth");
expect(localStorageMock.stubs.getItem("foo")).toEqual(undefined);
expect(localStorageMock.stubs.getItem("var")).toEqual(undefined);
});
});
describe("Clear method", () => {
it("should clear values in localStorage snapshot, but maintain localStorage values", () => {
expect.assertions(4);
localStorage.restoreLocalStorage("second");
localStorageMock.stubs.setItem("var", "foo-var-value");
localStorage.clearLocalStorageSnapshot("second");
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-new-value");
expect(localStorageMock.stubs.getItem("var")).toEqual("foo-var-value");
localStorage.restoreLocalStorage("second");
expect(localStorageMock.stubs.getItem("foo")).toEqual(undefined);
expect(localStorageMock.stubs.getItem("var")).toEqual(undefined);
});
it("should not clear values from other snapshot", () => {
localStorage.restoreLocalStorage("first");
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-value");
expect(localStorageMock.stubs.getItem("var")).toEqual("var-value");
});
});
});
describe("setLocalStorage method", () => {
it("should set values in localStorage", () => {
expect.assertions(2);
localStorage.setLocalStorage("foo", "foo-value");
localStorage.setLocalStorage("var", "var-value");
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-value");
expect(localStorageMock.stubs.getItem("var")).toEqual("var-value");
});
it("should not have set values in localStorage snapshot", () => {
expect.assertions(2);
localStorage.restoreLocalStorage();
expect(localStorageMock.stubs.getItem("foo")).toEqual(undefined);
expect(localStorageMock.stubs.getItem("var")).toEqual(undefined);
});
});
describe("getLocalStorage method", () => {
it("should get localStorage items", () => {
expect.assertions(2);
localStorage.setLocalStorage("foo", "foo-value");
localStorage.setLocalStorage("var", "var-value");
expect(localStorage.getLocalStorage("foo")).toEqual("foo-value");
expect(localStorage.getLocalStorage("var")).toEqual("var-value");
});
});
describe("removeLocalStorage", () => {
it("should remove local storage items", () => {
expect.assertions(2);
localStorage.saveLocalStorage();
localStorage.removeLocalStorage("foo");
expect(localStorage.getLocalStorage("foo")).toEqual(undefined);
expect(localStorage.getLocalStorage("var")).toEqual("var-value");
});
it("should not remove local storage items from localstorage snapshot", () => {
expect.assertions(2);
localStorage.restoreLocalStorage();
expect(localStorage.getLocalStorage("foo")).toEqual("foo-value");
expect(localStorage.getLocalStorage("var")).toEqual("var-value");
});
});
describe("LocalStorage when memory is cleaned", () => {
describe("save and restore methods", () => {
it("should not restore values that localStorage had when save method was called", () => {
expect.assertions(2);
localStorageMock.stubs.setItem("foo", "foo-value");
localStorageMock.stubs.setItem("var", "var-value");
localStorage.saveLocalStorage();
localStorageMock.stubs.setItem("foo", "foo-new-value");
expect(localStorageMock.stubs.getItem("foo")).toEqual("foo-new-value");
localStorage._namedSnapshots = {};
localStorage._snapshot = {};
localStorage.restoreLocalStorage();
expect(localStorageMock.stubs.getItem("foo")).toEqual(undefined);
});
it("should not restore values after calling localStorage clear", () => {
expect.assertions(2);
localStorageMock.stubs.clear();
expect(localStorageMock.stubs.getItem("var")).toEqual(undefined);
localStorage.restoreLocalStorage();
expect(localStorageMock.stubs.getItem("var")).toEqual(undefined);
});
it("should not restore new values if Save is called again", () => {
expect.assertions(2);
localStorageMock.stubs.setItem("foo", "foo-new-value");
localStorageMock.stubs.removeItem("var");
localStorage.saveLocalStorage();
localStorageMock.stubs.setItem("foo", "foo-another-new-value");
localStorageMock.stubs.setItem("var", "foo-var-value");
localStorage._namedSnapshots = {};
localStorage._snapshot = {};
localStorage.restoreLocalStorage();
expect(localStorageMock.stubs.getItem("foo")).toEqual(undefined);
expect(localStorageMock.stubs.getItem("var")).toEqual(undefined);
});
});
});
describe("disableLocalStorage", () => {
beforeEach(() => {
windowLocalStorageMock = new CyMock();
cyMock = new CyMock();
cypressMock = new CypressMock();
localStorage = new LocalStorage(
windowLocalStorageMock.window.localStorage,
cyMock.stubs,
cypressMock.stubs,
);
});
afterEach(() => {
windowLocalStorageMock.restore();
});
it("should do nothing if page is not reloaded", () => {
expect.assertions(3);
localStorage.disableLocalStorage();
expect(() => cyMock.window.localStorage.setItem()).not.toThrow();
expect(() => cyMock.window.localStorage.getItem()).not.toThrow();
expect(() => cyMock.window.localStorage.removeItem()).not.toThrow();
});
it("should use Cypress window:before:load event to create stubs", () => {
localStorage.disableLocalStorage();
expect(cyMock.stubs.on.getCall(0).args[0]).toEqual("window:before:load");
});
it("should make localStorage methods to throw after reloading page", () => {
expect.assertions(3);
localStorage.disableLocalStorage();
cyMock.loadWindow();
expect(() => cyMock.window.localStorage.setItem()).toThrow();
expect(() => cyMock.window.localStorage.getItem()).toThrow();
expect(() => cyMock.window.localStorage.removeItem()).toThrow();
});
it("should make cy.setLocalStorage command to log after reloading page", () => {
expect.assertions(1);
localStorage.disableLocalStorage();
cyMock.loadWindow();
localStorage.setLocalStorage("foo", "foo");
expect(
cyMock.stubs.log.calledWith("localStorage.setItem is disabled"),
).toEqual(true);
});
it("should make cy.getLocalStorage command to log after reloading page", () => {
expect.assertions(1);
localStorage.disableLocalStorage();
cyMock.loadWindow();
localStorage.getLocalStorage("foo");
expect(
cyMock.stubs.log.calledWith("localStorage.getItem is disabled"),
).toEqual(true);
});
it("should make cy.removeLocalStorage command to log after reloading page", () => {
expect.assertions(1);
localStorage.disableLocalStorage();
cyMock.loadWindow();
localStorage.removeLocalStorage("foo");
expect(
cyMock.stubs.log.calledWith("localStorage.removeItem is disabled"),
).toEqual(true);
});
it("should make cy.restoreLocalStorage command to log after reloading page", () => {
expect.assertions(1);
localStorage.disableLocalStorage();
cyMock.loadWindow();
localStorage.restoreLocalStorage();
expect(
cyMock.stubs.log.calledWith("localStorage.clear is disabled"),
).toEqual(true);
});
it("should make cy.saveLocalStorage command to do nothing", () => {
expect.assertions(1);
localStorage.disableLocalStorage();
cyMock.loadWindow();
localStorage.saveLocalStorage();
expect(
windowLocalStorageMock.window.localStorage.getItem.callCount,
).toEqual(0);
});
it("should do nothing if window.localStorage is not available", () => {
cyMock.window.localStorage = null;
localStorage.disableLocalStorage();
cyMock.loadWindow();
localStorage.setLocalStorage("foo", "foo");
expect(cyMock.stubs.log.callCount).toEqual(0);
});
it("should work when reloading page multiple times", () => {
expect.assertions(3);
localStorage.disableLocalStorage();
cyMock.loadWindow();
cyMock.loadWindow();
cyMock.loadWindow();
expect(() => cyMock.window.localStorage.setItem()).toThrow();
expect(() => cyMock.window.localStorage.getItem()).toThrow();
expect(() => cyMock.window.localStorage.removeItem()).toThrow();
});
it("should work when called multiple times", () => {
expect.assertions(3);
localStorage.disableLocalStorage();
cyMock.loadWindow();
localStorage.disableLocalStorage();
localStorage.disableLocalStorage();
cyMock.loadWindow();
expect(() => cyMock.window.localStorage.setItem()).toThrow();
expect(() => cyMock.window.localStorage.getItem()).toThrow();
expect(() => cyMock.window.localStorage.removeItem()).toThrow();
});
it("should throw error provided in the 'withError' option", () => {
expect.assertions(3);
const error = new Error("foo");
localStorage.disableLocalStorage({
withError: error,
});
cyMock.loadWindow();
expect(() => cyMock.window.localStorage.setItem()).toThrow(error);
expect(() => cyMock.window.localStorage.getItem()).toThrow(error);
expect(() => cyMock.window.localStorage.removeItem()).toThrow(error);
});
});
});