Move TimeOff test to vite
All checks were successful
CI / Go tests & lint (push) Successful in 7s
CI / Frontend tests & type-check (push) Successful in 26s
CI / Go tests & lint (pull_request) Successful in 7s
CI / Frontend tests & type-check (pull_request) Successful in 24s

This commit is contained in:
2026-04-09 10:48:45 -03:00
parent 07f11fa94e
commit 704f11cec3
2 changed files with 20 additions and 45 deletions

View File

@@ -1,11 +1,12 @@
import React from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { vi, type Mock } from 'vitest';
import { MemoryRouter } from 'react-router-dom';
import TimeOff from './TimeOff';
import { api, TimeOffRequest, ApiError } from '../api';
import { AuthProvider } from '../auth';
jest.mock('../api', () => {
vi.mock('../api', () => {
class MockApiError extends Error {
status: number;
data: any;
@@ -17,24 +18,24 @@ jest.mock('../api', () => {
}
return {
api: {
listTimeOff: jest.fn(),
createTimeOff: jest.fn(),
updateTimeOff: jest.fn(),
deleteTimeOff: jest.fn(),
reviewTimeOff: jest.fn(),
getRemovedShifts: jest.fn(),
listVolunteers: jest.fn(),
listTimeOff: vi.fn(),
createTimeOff: vi.fn(),
updateTimeOff: vi.fn(),
deleteTimeOff: vi.fn(),
reviewTimeOff: vi.fn(),
getRemovedShifts: vi.fn(),
listVolunteers: vi.fn(),
},
ApiError: MockApiError,
};
});
const mockListTimeOff = api.listTimeOff as jest.Mock;
const mockCreateTimeOff = api.createTimeOff as jest.Mock;
const mockDeleteTimeOff = api.deleteTimeOff as jest.Mock;
const mockReviewTimeOff = api.reviewTimeOff as jest.Mock;
const mockGetRemovedShifts = api.getRemovedShifts as jest.Mock;
const mockListVolunteers = api.listVolunteers as jest.Mock;
const mockListTimeOff = api.listTimeOff as Mock;
const mockCreateTimeOff = api.createTimeOff as Mock;
const mockDeleteTimeOff = api.deleteTimeOff as Mock;
const mockReviewTimeOff = api.reviewTimeOff as Mock;
const mockGetRemovedShifts = api.getRemovedShifts as Mock;
const mockListVolunteers = api.listVolunteers as Mock;
function buildFakeJWT(payload: object): string {
const header = btoa(JSON.stringify({ alg: 'HS256', typ: 'JWT' }));
@@ -90,7 +91,7 @@ function renderAsAdmin() {
}
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
localStorage.clear();
mockListVolunteers.mockResolvedValue([]);
});
@@ -147,10 +148,10 @@ describe('TimeOff page', () => {
it('shows conflict warning on 409 and allows confirmation', async () => {
mockListTimeOff.mockResolvedValue([]);
const { ApiError: MockApiError } = jest.requireMock('../api');
const { ApiError: MockApiError } = await vi.importMock<typeof import('../api')>('../api');
mockCreateTimeOff
.mockRejectedValueOnce(
new MockApiError('conflict', 409, {
new (MockApiError as any)('conflict', 409, {
message: 'Time off conflicts with assigned shifts.',
conflicts: [
{ instance_id: 100, name: 'Morning Walk', date: '2026-06-01', start_time: '08:00', end_time: '12:00' },