forked from obytes/react-native-template-obytes
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuse-login.test.tsx
More file actions
105 lines (87 loc) · 2.47 KB
/
use-login.test.tsx
File metadata and controls
105 lines (87 loc) · 2.47 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react-native';
import React from 'react';
import { useLogin } from '@/api/auth/use-login';
import { client } from '@/api/common';
// Mock the client
jest.mock('@/api/common', () => ({
client: jest.fn(),
}));
const mockedClient = client as jest.MockedFunction<typeof client>;
function createTestQueryClient() {
return new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
});
}
function createMockLoginResponse(overrides = {}) {
return {
data: {
id: 1,
username: 'testuser',
email: 'test@example.com',
accessToken: 'mock-access-token',
refreshToken: 'mock-refresh-token',
...overrides,
},
};
}
function createLoginVariables(overrides = {}) {
return {
email: 'test@example.com',
password: 'password123',
...overrides,
};
}
const createWrapper = () => {
const queryClient = createTestQueryClient();
return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
};
describe('useLogin', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should be defined', () => {
expect(useLogin).toBeDefined();
});
it('should call client with correct parameters', async () => {
const mockResponse = createMockLoginResponse();
mockedClient.mockResolvedValueOnce(mockResponse);
const { result } = renderHook(() => useLogin(), {
wrapper: createWrapper(),
});
const variables = createLoginVariables();
result.current.mutate(variables);
await waitFor(() => {
expect(mockedClient).toHaveBeenCalledWith({
url: '/v1/users/sign_in',
method: 'POST',
data: {
user: variables,
},
});
});
});
it('should handle login with expiresInMins parameter', async () => {
const mockResponse = createMockLoginResponse();
mockedClient.mockResolvedValueOnce(mockResponse);
const { result } = renderHook(() => useLogin(), {
wrapper: createWrapper(),
});
const variables = createLoginVariables({ expiresInMins: 60 });
result.current.mutate(variables);
await waitFor(() => {
expect(mockedClient).toHaveBeenCalledWith({
url: '/v1/users/sign_in',
method: 'POST',
data: {
user: variables,
},
});
});
});
});