Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add useCookieState test case #2112

Merged
merged 8 commits into from
Mar 21, 2023
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');
});
});