-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathclient-context.test.js
More file actions
49 lines (36 loc) · 1.6 KB
/
client-context.test.js
File metadata and controls
49 lines (36 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { describe, it, expect, vi } from 'vitest';
import { renderHook } from '@testing-library/react';
import { useClient, ClientProvider } from './client-context';
import { bigIntJSON } from '../common/bigIntJSON';
// Mock localStorage
const mockLocalStorage = {
getItem: vi.fn(),
setItem: vi.fn(),
};
Object.defineProperty(window, 'localStorage', {
value: mockLocalStorage,
});
// Mock JWT token
const mockRestrictedToken = 'eyJhbGciOiJIUzI1NiJ9.eyJhY2Nlc3MiOlt7ImFjY2VzcyI6InBydyJ9XX0.x';
const mockUnrestrictedToken = 'eyJhbGciOiJIUzI1NiJ9.eyJhY2Nlc3MiOlt7ImFjY2VzcyI6InIifV19.x';
describe('useClient', () => {
beforeEach(() => {
mockLocalStorage.getItem.mockReset();
mockLocalStorage.setItem.mockReset();
});
it('should return isRestricted=true for restricted token', () => {
mockLocalStorage.getItem.mockReturnValue(bigIntJSON.stringify({ apiKey: mockRestrictedToken }));
const { result } = renderHook(() => useClient(), { wrapper: ClientProvider });
expect(result.current.isRestricted).toBe(true);
});
it('should return isRestricted=false for unrestricted token', () => {
mockLocalStorage.getItem.mockReturnValue(bigIntJSON.stringify({ apiKey: mockUnrestrictedToken }));
const { result } = renderHook(() => useClient(), { wrapper: ClientProvider });
expect(result.current.isRestricted).toBe(false);
});
it('should return isRestricted=false for no token', () => {
mockLocalStorage.getItem.mockReturnValue(null);
const { result } = renderHook(() => useClient(), { wrapper: ClientProvider });
expect(result.current.isRestricted).toBe(false);
});
});