Skip to content

Commit

Permalink
test: add useCookieState test case (#2112)
Browse files Browse the repository at this point in the history
* test(useCookieState): add test case

* docs(useCookieState):  default value store to cookie

* docs: update

* test: update cookie name

* test(useCookieState): update test case

* Revert "test: add useCookieState test case and fix documentation"

* test(useCookieState): add test case

---------

Co-authored-by: liuyib <[email protected]>
  • Loading branch information
KangXinzhi and liuyib authored Mar 21, 2023
1 parent b5674b5 commit 24245bd
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/hooks/src/useCookieState/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('useCookieState', () => {
});
expect(anotherHook.result.current.state).toBe('C');
expect(hook.result.current.state).toBe('B');
expect(Cookies.get(COOKIE)).toBe('C');
});

it('should support undefined', () => {
Expand All @@ -86,6 +87,13 @@ describe('useCookieState', () => {
defaultValue: 'false',
});
expect(anotherHook.result.current.state).toBe('false');
expect(Cookies.get(COOKIE)).toBe('false');
act(() => {
// @ts-ignore
hook.result.current.setState();
});
expect(hook.result.current.state).toBeUndefined();
expect(Cookies.get(COOKIE)).toBeUndefined();
});

it('should support empty string', () => {
Expand All @@ -109,4 +117,24 @@ describe('useCookieState', () => {
});
expect(hook.result.current.state).toBe('hello world, zhangsan');
});

it('using the same cookie name', () => {
const COOKIE_NAME = 'test-same-cookie-name';
const { result: result1 } = setUp(COOKIE_NAME, { defaultValue: 'A' });
const { result: result2 } = setUp(COOKIE_NAME, { defaultValue: 'B' });
expect(result1.current.state).toBe('A');
expect(result2.current.state).toBe('A');
act(() => {
result1.current.setState('B');
});
expect(result1.current.state).toBe('B');
expect(result2.current.state).toBe('A');
expect(Cookies.get(COOKIE_NAME)).toBe('B');
act(() => {
result2.current.setState('C');
});
expect(result1.current.state).toBe('B');
expect(result2.current.state).toBe('C');
expect(Cookies.get(COOKIE_NAME)).toBe('C');
});
});

0 comments on commit 24245bd

Please sign in to comment.