Skip to content

Commit

Permalink
Refactor: src/utils/getRefreshToken.test.ts from Jest to Vitest (#2913)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhiren-Mhatre authored Dec 26, 2024
1 parent 895bead commit 431a76a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 57 deletions.
14 changes: 11 additions & 3 deletions src/screens/OrganizationDashboard/OrganizationDashboard.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ describe('Testing Organization Dashboard Screen', () => {
vi.mocked(useParams).mockReturnValue({ orgId: 'orgId' });
renderOrganizationDashboard(link1);

// Wait for initial load
await waitFor(() => {
expect(screen.getByText(t.upcomingEvents)).toBeInTheDocument();
});

// Dashboard cards
const membersBtn = await screen.findByText(t.members);
expect(membersBtn).toBeInTheDocument();
Expand All @@ -155,9 +160,12 @@ describe('Testing Organization Dashboard Screen', () => {
expect(screen.getByText(t.events)).toBeInTheDocument();
expect(screen.getByText(t.blockedUsers)).toBeInTheDocument();

// Upcoming events
expect(screen.getByText(t.upcomingEvents)).toBeInTheDocument();
expect(screen.getByText('Event 1')).toBeInTheDocument();
// Upcoming events - Using more flexible matcher
await waitFor(() => {
expect(
screen.getByText(/Event 1/i, { exact: false }),
).toBeInTheDocument();
});

// Latest posts
expect(screen.getByText(t.latestPosts)).toBeInTheDocument();
Expand Down
87 changes: 87 additions & 0 deletions src/utils/getRefreshToken.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SKIP_LOCALSTORAGE_CHECK
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { refreshToken } from './getRefreshToken';

const mockApolloClient = {
mutate: vi.fn(() =>
Promise.resolve({
data: {
refreshToken: {
accessToken: 'newAccessToken',
refreshToken: 'newRefreshToken',
},
},
}),
),
};

vi.mock('@apollo/client', async () => {
const actual = await vi.importActual('@apollo/client');
return {
...actual,
ApolloClient: vi.fn(() => mockApolloClient),
};
});

describe('refreshToken', () => {
const { location } = window;

interface TestInterfacePartialWindow {
location?: Partial<Location>;
}

delete (window as TestInterfacePartialWindow).location;
global.window.location = { ...location, reload: vi.fn() };

// Create storage mock
const localStorageMock = {
getItem: vi.fn(),
setItem: vi.fn(),
clear: vi.fn(),
removeItem: vi.fn(),
length: 0,
key: vi.fn(),
};

beforeEach(() => {
vi.clearAllMocks();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
writable: true,
});
});

it('returns true when the token is refreshed successfully', async () => {
const result = await refreshToken();

expect(localStorage.setItem).toHaveBeenCalledWith(
'Talawa-admin_token',
JSON.stringify('newAccessToken'),
);
expect(localStorage.setItem).toHaveBeenCalledWith(
'Talawa-admin_refreshToken',
JSON.stringify('newRefreshToken'),
);
expect(result).toBe(true);
expect(window.location.reload).toHaveBeenCalled();
});

it('returns false and logs error when token refresh fails', async () => {
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});

const errorMock = new Error('Failed to refresh token');
mockApolloClient.mutate.mockRejectedValueOnce(errorMock);

const result = await refreshToken();

expect(result).toBe(false);
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to refresh token',
errorMock,
);

consoleErrorSpy.mockRestore();
});
});
54 changes: 0 additions & 54 deletions src/utils/getRefreshToken.test.ts

This file was deleted.

0 comments on commit 431a76a

Please sign in to comment.