Skip to content

Commit b5af02b

Browse files
frankdiwfrankdiwangcapricorn86
authored
fix: [#1181] Adds support for defining keys to the Storage class used by the properties Window.localStorage and Window.sessionStorage
* fix: [#1181] Refactor storage mock * chore: [#1181] Removes the classes LocalStorage and SessionStorage as Storage is the correct class to use * chore: [#1181] Removes the classes LocalStorage and SessionStorage as Storage is the correct class to use --------- Co-authored-by: frankdiwang <[email protected]> Co-authored-by: David Ortner <[email protected]>
1 parent 96b06e6 commit b5af02b

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

packages/happy-dom/src/storage/Storage.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
* @see https://developer.mozilla.org/en-US/docs/Web/API/Storage
55
*/
66
export default class Storage {
7-
#store: { [k: string]: string } = {};
8-
97
/**
108
* Returns length.
119
*
1210
* @returns Length.
1311
*/
1412
public get length(): number {
15-
return Object.keys(this.#store).length;
13+
return Object.keys(this).length;
1614
}
1715

1816
/**
@@ -22,7 +20,7 @@ export default class Storage {
2220
* @returns Name.
2321
*/
2422
public key(index: number): string {
25-
const name = Object.keys(this.#store)[index];
23+
const name = Object.keys(this)[index];
2624
return name === undefined ? null : name;
2725
}
2826

@@ -33,7 +31,7 @@ export default class Storage {
3331
* @param item Item.
3432
*/
3533
public setItem(name: string, item: string): void {
36-
this.#store[name] = item;
34+
this[name] = item;
3735
}
3836

3937
/**
@@ -43,7 +41,7 @@ export default class Storage {
4341
* @returns Item.
4442
*/
4543
public getItem(name: string): string {
46-
return this.#store[name] === undefined ? null : this.#store[name];
44+
return this[name] === undefined ? null : this[name];
4745
}
4846

4947
/**
@@ -52,13 +50,16 @@ export default class Storage {
5250
* @param name Name.
5351
*/
5452
public removeItem(name: string): void {
55-
delete this.#store[name];
53+
delete this[name];
5654
}
5755

5856
/**
5957
* Clears storage.
6058
*/
6159
public clear(): void {
62-
this.#store = {};
60+
const keys = Object.keys(this);
61+
for (const key of keys) {
62+
delete this[key];
63+
}
6364
}
6465
}

packages/happy-dom/test/storage/Storage.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ describe('Storage', () => {
1515
expect(storage.length).toBe(2);
1616
storage.setItem('key3', 'value3');
1717
expect(storage.length).toBe(3);
18+
storage['key4'] = 'value4';
19+
expect(storage.length).toBe(4);
1820
});
1921
});
2022

@@ -34,6 +36,8 @@ describe('Storage', () => {
3436
storage.setItem('key2', 'value2');
3537
expect(storage.getItem('key1')).toBe('value1');
3638
expect(storage.getItem('key2')).toBe('value2');
39+
storage['key3'] = 'value3';
40+
expect(storage.getItem('key3')).toBe('value3');
3741
});
3842
});
3943

@@ -43,6 +47,10 @@ describe('Storage', () => {
4347
storage.setItem('key2', 'value2');
4448
expect(storage.getItem('key1')).toBe('value1');
4549
expect(storage.getItem('key2')).toBe('value2');
50+
expect(storage['key1']).toBe('value1');
51+
expect(storage['key2']).toBe('value2');
52+
storage['key1'] = 'value3';
53+
expect(storage.getItem('key1')).toBe('value3');
4654
});
4755
});
4856

@@ -54,6 +62,21 @@ describe('Storage', () => {
5462
expect(storage.length).toBe(1);
5563
expect(storage.getItem('key1')).toBe('value1');
5664
expect(storage.getItem('key2')).toBe(null);
65+
expect(storage['key1']).toBe('value1');
66+
expect(storage['key2']).toBe(undefined);
67+
});
68+
});
69+
70+
describe('clear()', () => {
71+
it('Clears storage.', () => {
72+
storage.setItem('key1', 'value1');
73+
storage.setItem('key2', 'value2');
74+
storage.clear();
75+
expect(storage.length).toBe(0);
76+
expect(storage.getItem('key1')).toBe(null);
77+
expect(storage.getItem('key2')).toBe(null);
78+
expect(storage['key1']).toBe(undefined);
79+
expect(storage['key2']).toBe(undefined);
5780
});
5881
});
5982
});

0 commit comments

Comments
 (0)