Skip to content

Commit

Permalink
feat: add test case for setStorageStateRecorder
Browse files Browse the repository at this point in the history
  • Loading branch information
bbb169 committed Oct 5, 2024
1 parent 47146da commit b031aae
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
32 changes: 31 additions & 1 deletion packages/hooks/src/usePageCacheState/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ describe('usePageCacheState', () => {
// remove over count data
expect(localStorage.getItem(getRealityStorageKey(LOCAL_STORAGE_KEY))).toBe(null);

secondHook.result.current.setState('B2');
act(() => {
secondHook.result.current.setState('B2');
});
await act(async () => {
jest.advanceTimersByTime(6000);
});
Expand All @@ -198,5 +200,33 @@ describe('usePageCacheState', () => {
});
expect(secondHook.result.current.state).toBe('A2');
expect(thirdHook.result.current.state).toBe('B3');

const thirdHookOtherVersion = setUp(LOCAL_STORAGE_KEY, 'A3', {
expire: 5,
version: '4',
subKey: 'test3',
});

// test delete all versions in a subKey
let recorder = thirdHookOtherVersion.result.current.operations.storageStateRecorder || {};
let versionsMap = recorder['test3'];
expect(Object.keys(versionsMap).toString()).toBe('3,4');
act(() => {
thirdHookOtherVersion.result.current.operations.delete('test3');
});
recorder = thirdHookOtherVersion.result.current.operations.storageStateRecorder || {};
versionsMap = recorder['test3'];
// only keep own version
expect(Object.keys(versionsMap || {}).toString()).toBe('4');

// test `setStorageStateRecorder`
act(() => {
thirdHookOtherVersion.result.current.operations.setStorageStateRecorder({});
});
expect(
Object.keys(
thirdHookOtherVersion.result.current.operations.storageStateRecorder || {},
).toString(),
).toBe('');
});
});
13 changes: 11 additions & 2 deletions packages/hooks/src/usePageCacheState/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default function <T>(key: string, options?: Options<T>) {
// ============================= hanlde recorder =============================
const [pageCacheKeysRecorder, setPageCacheKeysRecorder] = useStorageState<
StorageStateRecorder<T>
>(`${key}_all_keys_recorder`, {});
>(`${key}_storage_state_recorder`, {});

// remove data when expired or over count
useEffect(() => {
Expand Down Expand Up @@ -195,12 +195,21 @@ export default function <T>(key: string, options?: Options<T>) {
setPageCache(undefined);

// ===================== find all keys and remove =====================
const curSubKeyStorageRecorder = pageCacheKeysRecorder?.[key]?.[deleteSubKey] || {};
if (!pageCacheKeysRecorder) {
return;
}
const curSubKeyStorageRecorder = pageCacheKeysRecorder?.[deleteSubKey] || {};
const versionKeys = Object.keys(curSubKeyStorageRecorder);

versionKeys.forEach((versionItem) => {
getStorage()?.removeItem(getRealityStorageKey(key, versionItem, deleteSubKey));
});

delete pageCacheKeysRecorder[deleteSubKey];

setPageCacheKeysRecorder({
...pageCacheKeysRecorder,
});
});

return [
Expand Down

0 comments on commit b031aae

Please sign in to comment.