Skip to content

Commit

Permalink
fix sync storage data (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
wqcstrong authored Dec 16, 2023
1 parent 969d951 commit d4d0e58
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
37 changes: 15 additions & 22 deletions src/plugins/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

Expand Down
26 changes: 21 additions & 5 deletions tests/plugins/storage.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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);
});
});

0 comments on commit d4d0e58

Please sign in to comment.