Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ jobs:
-Dsonar.projectKey=${{ secrets.SONAR_PROJECT }}
-Dsonar.sonar.sourceEncoding=UTF-8
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
-Dsonar.coverage.exclusions=**/node_modules/**,**/storage/**,**/**.config.js,**/*.test.ts,**/*.test.tsx,**/*.spec.ts,**/*.spec.tsx,**/icons/**,**/docs/**,**/cli/**,**/android/**,**/ios/**,env.js
-Dsonar.exclusions=**/docs/**,**/__mocks__/**
-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
-Dsonar.exclusions=**/docs/**,**/__mocks__/**,**/__tests__/**,**/*.test.ts,**/*.test.tsx,**/*.spec.ts,**/*.spec.tsx,app.config.ts,jest-setup.ts
70 changes: 70 additions & 0 deletions __tests__/api/auth/use-forgot-password.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react-native';
import React from 'react';

import { useForgotPassword } from '@/api/auth/use-forgot-password';
import { client } from '@/api/common';

// Mock the client
jest.mock('@/api/common', () => ({
client: jest.fn(),
}));

const mockedClient = client as jest.MockedFunction<typeof client>;

const createWrapper = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
});

return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
};

describe('useForgotPassword', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should be defined', () => {
expect(useForgotPassword).toBeDefined();
});

it('should call client with correct parameters', async () => {
const mockResponse = {
data: {
message: 'Password reset email sent',
},
};

mockedClient.mockResolvedValueOnce(mockResponse);

const { result } = renderHook(() => useForgotPassword(), {
wrapper: createWrapper(),
});

const variables = {
email: 'test@example.com',
};

result.current.mutate(variables);

await waitFor(() => {
expect(mockedClient).toHaveBeenCalledWith({
url: '/v1/users/password',
method: 'POST',
data: {
email: variables.email,
redirect_url: 'https://example.com',
},
headers: {
'Content-Type': 'application/json',
},
});
});
});
});
105 changes: 105 additions & 0 deletions __tests__/api/auth/use-login.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,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,
},
});
});
});
});
128 changes: 128 additions & 0 deletions __tests__/api/auth/use-sign-up.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react-native';
import React from 'react';

import { useSignUp } from '@/api/auth/use-sign-up';
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 createMockSignUpResponse(overrides = {}) {
return {
data: {
status: 'success',
data: {
id: '1',
email: 'test@example.com',
name: 'Test User',
provider: 'email',
uid: 'test@example.com',
allowPasswordChange: true,
...overrides,
},
},
};
}

function createSignUpVariables(overrides = {}) {
return {
email: 'test@example.com',
name: 'Test User',
password: 'password123',
passwordConfirmation: 'password123',
...overrides,
};
}

const createWrapper = () => {
const queryClient = createTestQueryClient();

return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
};

describe('useSignUp', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should be defined', () => {
expect(useSignUp).toBeDefined();
});

it('should call client with correct parameters', async () => {
const mockResponse = createMockSignUpResponse({
createdAt: '2023-01-01T00:00:00Z',
updatedAt: '2023-01-01T00:00:00Z',
});

mockedClient.mockResolvedValueOnce(mockResponse);

const { result } = renderHook(() => useSignUp(), {
wrapper: createWrapper(),
});

const variables = createSignUpVariables();
result.current.mutate(variables);

await waitFor(() => {
expect(mockedClient).toHaveBeenCalledWith({
url: '/v1/users',
method: 'POST',
data: {
user: variables,
},
});
});
});

it('should handle sign up with all required fields', async () => {
const mockResponse = createMockSignUpResponse({
email: 'newuser@example.com',
name: 'New User',
uid: 'newuser@example.com',
createdAt: '2023-01-01T00:00:00Z',
updatedAt: '2023-01-01T00:00:00Z',
});

mockedClient.mockResolvedValueOnce(mockResponse);

const { result } = renderHook(() => useSignUp(), {
wrapper: createWrapper(),
});

const variables = createSignUpVariables({
email: 'newuser@example.com',
name: 'New User',
password: 'securepassword',
passwordConfirmation: 'securepassword',
});

result.current.mutate(variables);

await waitFor(() => {
expect(mockedClient).toHaveBeenCalledWith({
url: '/v1/users',
method: 'POST',
data: {
user: variables,
},
});
});
});
});
68 changes: 68 additions & 0 deletions __tests__/api/auth/use-update-password.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react-native';
import React from 'react';

import { useUpdatePassword } from '@/api/auth/use-update-password';
import { client } from '@/api/common';

// Mock the client
jest.mock('@/api/common', () => ({
client: jest.fn(),
}));

const mockedClient = client as jest.MockedFunction<typeof client>;

const createWrapper = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
});

return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
};

describe('useUpdatePassword', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should be defined', () => {
expect(useUpdatePassword).toBeDefined();
});

it('should call client with correct parameters', async () => {
const mockResponse = {
data: {
message: 'Password updated successfully',
},
};

mockedClient.mockResolvedValueOnce(mockResponse);

const { result } = renderHook(() => useUpdatePassword(), {
wrapper: createWrapper(),
});

const variables = {
password: 'newPassword123',
passwordConfirmation: 'newPassword123',
};

result.current.mutate(variables);

await waitFor(() => {
expect(mockedClient).toHaveBeenCalledWith({
url: '/v1/users/password',
method: 'PUT',
data: variables,
headers: {
'Content-Type': 'application/json',
},
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AxiosError, AxiosHeaders } from 'axios';

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

import { client } from './client';
import { client } from '../../../src/api/common/client';

const testRequestInterceptors = () => {
describe('request interceptors', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getUrlParameters, toCamelCase, toSnakeCase } from './utils';
import { getUrlParameters, toCamelCase, toSnakeCase } from '../../../src/api/common/utils';

describe('utils', () => {
describe('toCamelCase', () => {
Expand Down
Loading