Skip to content

Commit

Permalink
chore: [capricorn86#1181] Removes the classes LocalStorage and Sessio…
Browse files Browse the repository at this point in the history
…nStorage as Storage is the correct class to use
  • Loading branch information
capricorn86 committed Mar 21, 2024
1 parent e6e0016 commit b1b23c3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 79 deletions.
25 changes: 4 additions & 21 deletions packages/happy-dom/src/storage/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/Storage
*/
export class Storage {
/**
*
*/
constructor() {
if (new.target === Storage) {
throw Error('Storage is a base class and cannot be constructed directly.');
}
}
export default class Storage {
/**
* Returns length.
*
Expand Down Expand Up @@ -65,18 +57,9 @@ export class Storage {
* Clears storage.
*/
public clear(): void {
Object.keys(this).forEach((key) => {
const keys = Object.keys(this);
for (const key of keys) {
delete this[key];
});
}
}
}

/**
* LocalStorage.
*/
export class LocalStorage extends Storage {}

/**
* SessionStorage.
*/
export class SessionStorage extends Storage {}
6 changes: 3 additions & 3 deletions packages/happy-dom/src/window/BrowserWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import SubmitEvent from '../event/events/SubmitEvent.js';
import Screen from '../screen/Screen.js';
import IResponse from '../fetch/types/IResponse.js';
import IRequestInit from '../fetch/types/IRequestInit.js';
import { Storage, LocalStorage, SessionStorage } from '../storage/Storage.js';
import Storage from '../storage/Storage.js';
import HTMLCollection from '../nodes/element/HTMLCollection.js';
import HTMLFormControlsCollection from '../nodes/html-form-element/HTMLFormControlsCollection.js';
import NodeList from '../nodes/node/NodeList.js';
Expand Down Expand Up @@ -523,8 +523,8 @@ export default class BrowserWindow extends EventTarget implements IBrowserWindow
this.navigator = new Navigator(this);
this.history = new History();
this.screen = new Screen();
this.sessionStorage = new SessionStorage();
this.localStorage = new LocalStorage();
this.sessionStorage = new Storage();
this.localStorage = new Storage();
this.location = new Location(this.#browserFrame, options?.url ?? 'about:blank');
this.console = browserFrame.page.console;

Expand Down
2 changes: 1 addition & 1 deletion packages/happy-dom/src/window/IBrowserWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ import SubmitEvent from '../event/events/SubmitEvent.js';
import MessageEvent from '../event/events/MessageEvent.js';
import MessagePort from '../event/MessagePort.js';
import Screen from '../screen/Screen.js';
import { Storage } from '../storage/Storage.js';
import Storage from '../storage/Storage.js';
import NodeFilter from '../tree-walker/NodeFilter.js';
import HTMLCollection from '../nodes/element/HTMLCollection.js';
import HTMLFormControlsCollection from '../nodes/html-form-element/HTMLFormControlsCollection.js';
Expand Down
74 changes: 20 additions & 54 deletions packages/happy-dom/test/storage/Storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { SessionStorage, Storage, LocalStorage } from '../../src/storage/Storage.js';
import Storage from '../../src/storage/Storage.js';
import { beforeEach, describe, it, expect } from 'vitest';

describe('LocalStorage', () => {
describe('Storage', () => {
let storage: Storage;

beforeEach(() => {
storage = new LocalStorage();
storage = new Storage();
});

describe('get length()', () => {
Expand All @@ -15,6 +15,8 @@ describe('LocalStorage', () => {
expect(storage.length).toBe(2);
storage.setItem('key3', 'value3');
expect(storage.length).toBe(3);
storage['key4'] = 'value4';
expect(storage.length).toBe(4);
});
});

Expand All @@ -34,6 +36,8 @@ describe('LocalStorage', () => {
storage.setItem('key2', 'value2');
expect(storage.getItem('key1')).toBe('value1');
expect(storage.getItem('key2')).toBe('value2');
storage['key3'] = 'value3';
expect(storage.getItem('key3')).toBe('value3');
});
});

Expand All @@ -43,6 +47,10 @@ describe('LocalStorage', () => {
storage.setItem('key2', 'value2');
expect(storage.getItem('key1')).toBe('value1');
expect(storage.getItem('key2')).toBe('value2');
expect(storage['key1']).toBe('value1');
expect(storage['key2']).toBe('value2');
storage['key1'] = 'value3';
expect(storage.getItem('key1')).toBe('value3');
});
});

Expand All @@ -54,63 +62,21 @@ describe('LocalStorage', () => {
expect(storage.length).toBe(1);
expect(storage.getItem('key1')).toBe('value1');
expect(storage.getItem('key2')).toBe(null);
expect(storage['key1']).toBe('value1');
expect(storage['key2']).toBe(undefined);
});
});
});

describe('SessionStorage', () => {
let storage: Storage;

beforeEach(() => {
storage = new SessionStorage();
});

describe('get length()', () => {
it('Returns length.', () => {
describe('clear()', () => {
it('Clears storage.', () => {
storage.setItem('key1', 'value1');
storage.setItem('key2', 'value2');
expect(storage.length).toBe(2);
storage.setItem('key3', 'value3');
expect(storage.length).toBe(3);
});
});

describe('key()', () => {
it('Returns name of the nth name.', () => {
storage.setItem('key1', 'value1');
storage.setItem('key2', 'value2');
expect(storage.key(0)).toBe('key1');
expect(storage.key(1)).toBe('key2');
expect(storage.key(2)).toBe(null);
});
});

describe('getItem()', () => {
it('Returns item.', () => {
storage.setItem('key1', 'value1');
storage.setItem('key2', 'value2');
expect(storage.getItem('key1')).toBe('value1');
expect(storage.getItem('key2')).toBe('value2');
});
});

describe('setItem()', () => {
it('Returns item.', () => {
storage.setItem('key1', 'value1');
storage.setItem('key2', 'value2');
expect(storage.getItem('key1')).toBe('value1');
expect(storage.getItem('key2')).toBe('value2');
});
});

describe('removeItem()', () => {
it('Removes an item.', () => {
storage.setItem('key1', 'value1');
storage.setItem('key2', 'value2');
storage.removeItem('key2');
expect(storage.length).toBe(1);
expect(storage.getItem('key1')).toBe('value1');
storage.clear();
expect(storage.length).toBe(0);
expect(storage.getItem('key1')).toBe(null);
expect(storage.getItem('key2')).toBe(null);
expect(storage['key1']).toBe(undefined);
expect(storage['key2']).toBe(undefined);
});
});
});

0 comments on commit b1b23c3

Please sign in to comment.