From d4d0e5876c5cdce89fe947e5cf25d6deceb38ba4 Mon Sep 17 00:00:00 2001 From: Blucas <15555602203@163.com> Date: Sat, 16 Dec 2023 14:37:58 +0800 Subject: [PATCH] fix sync storage data (#25) --- src/plugins/storage.ts | 37 ++++++++++++++--------------------- tests/plugins/storage.test.ts | 26 +++++++++++++++++++----- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/plugins/storage.ts b/src/plugins/storage.ts index 7cf75f5f..fe798ffb 100644 --- a/src/plugins/storage.ts +++ b/src/plugins/storage.ts @@ -24,10 +24,8 @@ export class StoragePlugin implements PageSpyPlugin { switch (data) { case 'localStorage': - result = StoragePlugin.takeLocalStorage(); - break; case 'sessionStorage': - result = StoragePlugin.takeSessionStorage(); + result = StoragePlugin.takeStorage(data); break; case 'cookie': result = await StoragePlugin.takeCookie(); @@ -42,31 +40,26 @@ export class StoragePlugin implements PageSpyPlugin { }); } - private static takeLocalStorage() { + private static takeStorage(type: 'localStorage' | 'sessionStorage') { const data: SpyStorage.GetTypeDataItem = { - type: 'localStorage', + type, action: 'get', - data: Object.entries(localStorage).map(([name, value]) => { - return { - name, - value, - }; - }), + data: [], }; - return data; - } + const storage = window[type]; + const size = storage.length; + if (!size) return data; - private static takeSessionStorage() { - const data: SpyStorage.GetTypeDataItem = { - type: 'sessionStorage', - action: 'get', - data: Object.entries(sessionStorage).map(([name, value]) => { - return { + for (let i = 0; i <= size - 1; i++) { + const name = storage.key(i); + if (name) { + const value = storage.getItem(name) || ''; + data.data.push({ name, value, - }; - }), - }; + }); + } + } return data; } diff --git a/tests/plugins/storage.test.ts b/tests/plugins/storage.test.ts index 416d071c..e6f7ac99 100644 --- a/tests/plugins/storage.test.ts +++ b/tests/plugins/storage.test.ts @@ -1,20 +1,22 @@ import { StoragePlugin } from 'src/plugins/storage'; +import socketStore from 'src/utils/socket'; // @ts-ignore const trigger = jest.spyOn(StoragePlugin, 'sendStorageItem'); -beforeEach(() => { +beforeAll(() => { + new StoragePlugin().onCreated(); +}); +afterEach(() => { localStorage.clear(); sessionStorage.clear(); document.cookie = ''; -}); -afterEach(() => { - jest.resetAllMocks(); + trigger.mockReset(); + console.log(trigger.mock); }); describe('Storage plugin', () => { it('cookieStore / localStorage /sessionStorage', async () => { - new StoragePlugin().onCreated(); await cookieStore.set('1', '1'); await cookieStore.set('2', '2'); expect(trigger).toHaveBeenCalledTimes(2); @@ -68,4 +70,18 @@ describe('Storage plugin', () => { ); expect(trigger).toHaveBeenCalledTimes(10); }); + + it('Special keys in Storage', () => { + const keys = ['key', 'setItem', 'getItem', 'removeItem', 'clear']; + + keys.forEach((k) => { + localStorage.setItem(k, k); + }); + + expect(trigger).toHaveBeenCalledTimes(5); + // @ts-ignore + const storage = StoragePlugin.takeStorage('localStorage'); + expect(storage.data.length).toBe(5); + expect(storage.data.map((i) => i.name)).toEqual(keys); + }); });