Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: src/utils/getRefreshToken.test.ts from Jest to Vitest #2913

Merged
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
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.

Loading