Skip to content

Commit 6f7b455

Browse files
committed
refactor: move test files to __tests__ directory
1 parent c2ae678 commit 6f7b455

31 files changed

+1169
-22
lines changed

.github/workflows/sonar.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ jobs:
4040
-Dsonar.projectKey=${{ secrets.SONAR_PROJECT }}
4141
-Dsonar.sonar.sourceEncoding=UTF-8
4242
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
43-
-Dsonar.coverage.exclusions=**/node_modules/**,**/storage/**,**/**.config.js,**/*.test.ts,**/*.test.tsx,**/*.spec.ts,**/*.spec.tsx,**/icons/**,**/docs/**,**/cli/**,**/android/**,**/ios/**,env.js
44-
-Dsonar.exclusions=**/docs/**,**/__mocks__/**
43+
-Dsonar.coverage.exclusions=**/node_modules/**,**/storage/**,**/**.config.js,**/*.test.ts,**/*.test.tsx,**/*.spec.ts,**/*.spec.tsx,**/icons/**,**/docs/**,**/cli/**,**/android/**,**/ios/**,env.js,app.config.ts,jest-setup.ts
44+
-Dsonar.exclusions=**/docs/**,**/__mocks__/**,**/__tests__/**,**/*.test.ts,**/*.test.tsx,**/*.spec.ts,**/*.spec.tsx,app.config.ts,jest-setup.ts
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2+
import { renderHook, waitFor } from '@testing-library/react-native';
3+
import React from 'react';
4+
5+
import { useForgotPassword } from '@/api/auth/use-forgot-password';
6+
import { client } from '@/api/common';
7+
8+
// Mock the client
9+
jest.mock('@/api/common', () => ({
10+
client: jest.fn(),
11+
}));
12+
13+
const mockedClient = client as jest.MockedFunction<typeof client>;
14+
15+
const createWrapper = () => {
16+
const queryClient = new QueryClient({
17+
defaultOptions: {
18+
queries: { retry: false },
19+
mutations: { retry: false },
20+
},
21+
});
22+
23+
return ({ children }: { children: React.ReactNode }) => (
24+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
25+
);
26+
};
27+
28+
describe('useForgotPassword', () => {
29+
beforeEach(() => {
30+
jest.clearAllMocks();
31+
});
32+
33+
it('should be defined', () => {
34+
expect(useForgotPassword).toBeDefined();
35+
});
36+
37+
it('should call client with correct parameters', async () => {
38+
const mockResponse = {
39+
data: {
40+
message: 'Password reset email sent',
41+
},
42+
};
43+
44+
mockedClient.mockResolvedValueOnce(mockResponse);
45+
46+
const { result } = renderHook(() => useForgotPassword(), {
47+
wrapper: createWrapper(),
48+
});
49+
50+
const variables = {
51+
52+
};
53+
54+
result.current.mutate(variables);
55+
56+
await waitFor(() => {
57+
expect(mockedClient).toHaveBeenCalledWith({
58+
url: '/v1/users/password',
59+
method: 'POST',
60+
data: {
61+
email: variables.email,
62+
redirect_url: 'https://example.com',
63+
},
64+
headers: {
65+
'Content-Type': 'application/json',
66+
},
67+
});
68+
});
69+
});
70+
});
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2+
import { renderHook, waitFor } from '@testing-library/react-native';
3+
import React from 'react';
4+
5+
import { useLogin } from '@/api/auth/use-login';
6+
import { client } from '@/api/common';
7+
8+
// Mock the client
9+
jest.mock('@/api/common', () => ({
10+
client: jest.fn(),
11+
}));
12+
13+
const mockedClient = client as jest.MockedFunction<typeof client>;
14+
15+
function createTestQueryClient() {
16+
return new QueryClient({
17+
defaultOptions: {
18+
queries: { retry: false },
19+
mutations: { retry: false },
20+
},
21+
});
22+
}
23+
24+
function createMockLoginResponse(overrides = {}) {
25+
return {
26+
data: {
27+
id: 1,
28+
username: 'testuser',
29+
30+
accessToken: 'mock-access-token',
31+
refreshToken: 'mock-refresh-token',
32+
...overrides,
33+
},
34+
};
35+
}
36+
37+
function createLoginVariables(overrides = {}) {
38+
return {
39+
40+
password: 'password123',
41+
...overrides,
42+
};
43+
}
44+
45+
const createWrapper = () => {
46+
const queryClient = createTestQueryClient();
47+
48+
return ({ children }: { children: React.ReactNode }) => (
49+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
50+
);
51+
};
52+
53+
describe('useLogin', () => {
54+
beforeEach(() => {
55+
jest.clearAllMocks();
56+
});
57+
58+
it('should be defined', () => {
59+
expect(useLogin).toBeDefined();
60+
});
61+
62+
it('should call client with correct parameters', async () => {
63+
const mockResponse = createMockLoginResponse();
64+
mockedClient.mockResolvedValueOnce(mockResponse);
65+
66+
const { result } = renderHook(() => useLogin(), {
67+
wrapper: createWrapper(),
68+
});
69+
70+
const variables = createLoginVariables();
71+
result.current.mutate(variables);
72+
73+
await waitFor(() => {
74+
expect(mockedClient).toHaveBeenCalledWith({
75+
url: '/v1/users/sign_in',
76+
method: 'POST',
77+
data: {
78+
user: variables,
79+
},
80+
});
81+
});
82+
});
83+
84+
it('should handle login with expiresInMins parameter', async () => {
85+
const mockResponse = createMockLoginResponse();
86+
mockedClient.mockResolvedValueOnce(mockResponse);
87+
88+
const { result } = renderHook(() => useLogin(), {
89+
wrapper: createWrapper(),
90+
});
91+
92+
const variables = createLoginVariables({ expiresInMins: 60 });
93+
result.current.mutate(variables);
94+
95+
await waitFor(() => {
96+
expect(mockedClient).toHaveBeenCalledWith({
97+
url: '/v1/users/sign_in',
98+
method: 'POST',
99+
data: {
100+
user: variables,
101+
},
102+
});
103+
});
104+
});
105+
});
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2+
import { renderHook, waitFor } from '@testing-library/react-native';
3+
import React from 'react';
4+
5+
import { useSignUp } from '@/api/auth/use-sign-up';
6+
import { client } from '@/api/common';
7+
8+
// Mock the client
9+
jest.mock('@/api/common', () => ({
10+
client: jest.fn(),
11+
}));
12+
13+
const mockedClient = client as jest.MockedFunction<typeof client>;
14+
15+
function createTestQueryClient() {
16+
return new QueryClient({
17+
defaultOptions: {
18+
queries: { retry: false },
19+
mutations: { retry: false },
20+
},
21+
});
22+
}
23+
24+
function createMockSignUpResponse(overrides = {}) {
25+
return {
26+
data: {
27+
status: 'success',
28+
data: {
29+
id: '1',
30+
31+
name: 'Test User',
32+
provider: 'email',
33+
34+
allowPasswordChange: true,
35+
...overrides,
36+
},
37+
},
38+
};
39+
}
40+
41+
function createSignUpVariables(overrides = {}) {
42+
return {
43+
44+
name: 'Test User',
45+
password: 'password123',
46+
passwordConfirmation: 'password123',
47+
...overrides,
48+
};
49+
}
50+
51+
const createWrapper = () => {
52+
const queryClient = createTestQueryClient();
53+
54+
return ({ children }: { children: React.ReactNode }) => (
55+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
56+
);
57+
};
58+
59+
describe('useSignUp', () => {
60+
beforeEach(() => {
61+
jest.clearAllMocks();
62+
});
63+
64+
it('should be defined', () => {
65+
expect(useSignUp).toBeDefined();
66+
});
67+
68+
it('should call client with correct parameters', async () => {
69+
const mockResponse = createMockSignUpResponse({
70+
createdAt: '2023-01-01T00:00:00Z',
71+
updatedAt: '2023-01-01T00:00:00Z',
72+
});
73+
74+
mockedClient.mockResolvedValueOnce(mockResponse);
75+
76+
const { result } = renderHook(() => useSignUp(), {
77+
wrapper: createWrapper(),
78+
});
79+
80+
const variables = createSignUpVariables();
81+
result.current.mutate(variables);
82+
83+
await waitFor(() => {
84+
expect(mockedClient).toHaveBeenCalledWith({
85+
url: '/v1/users',
86+
method: 'POST',
87+
data: {
88+
user: variables,
89+
},
90+
});
91+
});
92+
});
93+
94+
it('should handle sign up with all required fields', async () => {
95+
const mockResponse = createMockSignUpResponse({
96+
97+
name: 'New User',
98+
99+
createdAt: '2023-01-01T00:00:00Z',
100+
updatedAt: '2023-01-01T00:00:00Z',
101+
});
102+
103+
mockedClient.mockResolvedValueOnce(mockResponse);
104+
105+
const { result } = renderHook(() => useSignUp(), {
106+
wrapper: createWrapper(),
107+
});
108+
109+
const variables = createSignUpVariables({
110+
111+
name: 'New User',
112+
password: 'securepassword',
113+
passwordConfirmation: 'securepassword',
114+
});
115+
116+
result.current.mutate(variables);
117+
118+
await waitFor(() => {
119+
expect(mockedClient).toHaveBeenCalledWith({
120+
url: '/v1/users',
121+
method: 'POST',
122+
data: {
123+
user: variables,
124+
},
125+
});
126+
});
127+
});
128+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2+
import { renderHook, waitFor } from '@testing-library/react-native';
3+
import React from 'react';
4+
5+
import { useUpdatePassword } from '@/api/auth/use-update-password';
6+
import { client } from '@/api/common';
7+
8+
// Mock the client
9+
jest.mock('@/api/common', () => ({
10+
client: jest.fn(),
11+
}));
12+
13+
const mockedClient = client as jest.MockedFunction<typeof client>;
14+
15+
const createWrapper = () => {
16+
const queryClient = new QueryClient({
17+
defaultOptions: {
18+
queries: { retry: false },
19+
mutations: { retry: false },
20+
},
21+
});
22+
23+
return ({ children }: { children: React.ReactNode }) => (
24+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
25+
);
26+
};
27+
28+
describe('useUpdatePassword', () => {
29+
beforeEach(() => {
30+
jest.clearAllMocks();
31+
});
32+
33+
it('should be defined', () => {
34+
expect(useUpdatePassword).toBeDefined();
35+
});
36+
37+
it('should call client with correct parameters', async () => {
38+
const mockResponse = {
39+
data: {
40+
message: 'Password updated successfully',
41+
},
42+
};
43+
44+
mockedClient.mockResolvedValueOnce(mockResponse);
45+
46+
const { result } = renderHook(() => useUpdatePassword(), {
47+
wrapper: createWrapper(),
48+
});
49+
50+
const variables = {
51+
password: 'newPassword123',
52+
passwordConfirmation: 'newPassword123',
53+
};
54+
55+
result.current.mutate(variables);
56+
57+
await waitFor(() => {
58+
expect(mockedClient).toHaveBeenCalledWith({
59+
url: '/v1/users/password',
60+
method: 'PUT',
61+
data: variables,
62+
headers: {
63+
'Content-Type': 'application/json',
64+
},
65+
});
66+
});
67+
});
68+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AxiosError, AxiosHeaders } from 'axios';
33

44
import interceptors from '@/api/common/interceptors';
55

6-
import { client } from './client';
6+
import { client } from '../../../src/api/common/client';
77

88
const testRequestInterceptors = () => {
99
describe('request interceptors', () => {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getUrlParameters, toCamelCase, toSnakeCase } from './utils';
1+
import { getUrlParameters, toCamelCase, toSnakeCase } from '../../../src/api/common/utils';
22

33
describe('utils', () => {
44
describe('toCamelCase', () => {

0 commit comments

Comments
 (0)