Skip to content

Commit

Permalink
feat: wip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillermo Machado committed Jan 15, 2025
1 parent 4ae95ff commit e2b4180
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions src/components/providers/auth.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/* eslint-disable max-lines-per-function */
import { act, screen, waitFor } from '@testing-library/react-native';
import React from 'react';

import { render } from '@/core/test-utils';

import { AuthProvider, authStorage, HEADER_KEYS, useAuth } from './auth';

jest.mock('@/api', () => {
const originalModule = jest.requireActual('@/api'); // Import the original module
const mockStore: Record<string, string> = {};

return {
...originalModule, // Spread the original module to keep other exports
authStorage: {
getString: jest.fn((key: string) => mockStore[key] || null),
set: jest.fn((key: string, value: string) => {
mockStore[key] = value;
}),
delete: jest.fn((key: string) => {
delete mockStore[key];
}),
},
client: {
interceptors: {
request: { use: jest.fn(), eject: jest.fn() },
response: { use: jest.fn(), eject: jest.fn() },
},
},
};
});

const TestComponent: React.FC = () => {
const { token, isAuthenticated, loading, ready, logout } = useAuth();

return (
<div>
<p data-testid="token">{token}</p>
<p data-testid="isAuthenticated">{isAuthenticated ? 'true' : 'false'}</p>
<p data-testid="loading">{loading ? 'true' : 'false'}</p>
<p data-testid="ready">{ready ? 'true' : 'false'}</p>
<button data-testid="logout" onClick={logout}>
Logout
</button>
</div>
);
};

describe('AuthProvider', () => {
render(
<AuthProvider>
<TestComponent />
</AuthProvider>,
);

afterEach(() => {
jest.clearAllMocks();
});

it('should initialize with loading and ready states', async () => {
(authStorage.getString as jest.Mock).mockImplementation((key) => {
if (key === HEADER_KEYS.ACCESS_TOKEN) {
return 'mockToken';
}
if (key === HEADER_KEYS.EXPIRY) {
return '2100-01-01T00:00:00.000Z';
}
return null;
});

expect(screen.getByTestId('loading').textContent).toBe('true');
expect(screen.getByTestId('ready').textContent).toBe('false');

await waitFor(() =>
expect(screen.getByTestId('loading').textContent).toBe('false'),
);
expect(screen.getByTestId('ready').textContent).toBe('true');
expect(screen.getByTestId('isAuthenticated').textContent).toBe('true');
expect(screen.getByTestId('token').textContent).toBe('mockToken');
});

it('should handle expired token', async () => {
(authStorage.getString as jest.Mock).mockImplementation((key) => {
if (key === HEADER_KEYS.ACCESS_TOKEN) {
return 'expiredToken';
}
if (key === HEADER_KEYS.EXPIRY) {
return '2000-01-01T00:00:00.000Z';
}
return null;
});

await waitFor(() =>
expect(screen.getByTestId('loading').textContent).toBe('false'),
);
expect(screen.getByTestId('isAuthenticated').textContent).toBe('false');
expect(screen.getByTestId('token').textContent).toBe('');
});

it('should clear storage and state on logout', async () => {
(authStorage.getString as jest.Mock).mockImplementation((key) => {
if (key === HEADER_KEYS.ACCESS_TOKEN) {
return 'mockToken';
}
if (key === HEADER_KEYS.EXPIRY) {
return '2100-01-01T00:00:00.000Z';
}
return null;
});

await waitFor(() =>
expect(screen.getByTestId('loading').textContent).toBe('false'),
);

act(() => {
screen.getByTestId('logout').click();
});

expect(authStorage.delete).toHaveBeenCalledWith(HEADER_KEYS.ACCESS_TOKEN);
expect(authStorage.delete).toHaveBeenCalledWith(HEADER_KEYS.REFRESH_TOKEN);
expect(authStorage.delete).toHaveBeenCalledWith(HEADER_KEYS.USER_ID);
expect(authStorage.delete).toHaveBeenCalledWith(HEADER_KEYS.EXPIRY);

expect(screen.getByTestId('isAuthenticated').textContent).toBe('false');
expect(screen.getByTestId('token').textContent).toBe('');
});
});

0 comments on commit e2b4180

Please sign in to comment.