mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
32837afe2e
msw 3 renames onUnhandledRequest to onUnhandledFrame and reports every network frame there; the setup judges the HTTP frames as before (another origin is an error, the page's own a warning). It also resolves a relative handler, and its fetch interceptor a relative URL, against location.href at the time of the request, while jsdom's XHR sends to the document's own address. Tests that replaced window.location with a stand-in on http://localhost while the page runs on http://localhost:3000 sent their requests past every handler. tests/helpers/location.ts builds such stand-ins on the page's own origin (locationAt, pageUrl), and every one of them now uses it. msw no longer escapes fake timers, and responses are timed differently, which surfaced tests that ended with work still running: a held request answering in the next test, a lookup still out when the assertions ran, a reload switched by counting reads. Each of those now waits for, or releases, what it started, and answers by what happened rather than by a count. The day panel suites share their setup through tests/helpers/dayDetailPanel.ts instead of a copy each, which keeps the larger one under its size limit.
173 lines
7.4 KiB
TypeScript
173 lines
7.4 KiB
TypeScript
// FE-MOB-MVACINV-001 to FE-MOB-MVACINV-010
|
|
import React from 'react';
|
|
import { describe, it, expect, beforeEach, afterEach, vi, type MockInstance } from 'vitest';
|
|
import { http, HttpResponse } from 'msw';
|
|
import { render, screen, fireEvent, waitFor, within, act } from '../../../helpers/render';
|
|
import { server } from '../../../helpers/msw/server';
|
|
import { resetAllStores } from '../../../helpers/store';
|
|
import { useVacayStore } from '../../../../src/store/vacayStore';
|
|
import apiClient from '../../../../src/api/client';
|
|
import MVacayInviteSheet from '../../../../src/mobile/screens/vacay/MVacayInviteSheet';
|
|
|
|
const toasts: { type: string; message: string }[] = [];
|
|
const USERS_ENDPOINT = '/addons/vacay/available-users';
|
|
let getSpy: MockInstance<typeof apiClient.get>;
|
|
|
|
function availableUsers(users: { id: number; username: string; color: string | null }[]) {
|
|
server.use(http.get('/api/addons/vacay/available-users', () => HttpResponse.json({ users })));
|
|
}
|
|
|
|
/**
|
|
* Waits until the sheet has asked for the invitable users `lookups` times and
|
|
* every answer has come back and been applied. The answer is not on screen when
|
|
* it changes nothing (an empty list, the same list on a reload), and a lookup
|
|
* still out when the test ends would land in the next one.
|
|
*/
|
|
async function usersSettled(lookups: number) {
|
|
const calls = () => getSpy.mock.calls.flatMap(([url], i) => (url === USERS_ENDPOINT ? [getSpy.mock.results[i].value] : []));
|
|
await waitFor(() => expect(calls()).toHaveLength(lookups));
|
|
await act(async () => {
|
|
await Promise.allSettled(calls());
|
|
});
|
|
}
|
|
|
|
beforeEach(() => {
|
|
resetAllStores();
|
|
getSpy = vi.spyOn(apiClient, 'get');
|
|
toasts.length = 0;
|
|
window.__addToast = ((message: string, type?: string) => {
|
|
toasts.push({ type: type ?? 'info', message });
|
|
return 1;
|
|
}) as Window['__addToast'];
|
|
availableUsers([
|
|
{ id: 2, username: 'bob', color: '#ec4899' },
|
|
{ id: 3, username: 'carol', color: '#f59e0b' },
|
|
]);
|
|
});
|
|
|
|
afterEach(() => {
|
|
getSpy.mockRestore();
|
|
delete window.__addToast;
|
|
});
|
|
|
|
function renderSheet(onClose = vi.fn(() => undefined)) {
|
|
const view = render(<MVacayInviteSheet open onClose={onClose} />);
|
|
return { ...view, onClose };
|
|
}
|
|
|
|
describe('MVacayInviteSheet', () => {
|
|
it('FE-MOB-MVACINV-001: renders nothing while closed', () => {
|
|
render(<MVacayInviteSheet open={false} onClose={() => {}} />);
|
|
|
|
expect(screen.queryByRole('dialog', { name: 'Invite User' })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('FE-MOB-MVACINV-002: loads the invitable users and shows the picker placeholder', async () => {
|
|
renderSheet();
|
|
|
|
expect(await screen.findByRole('button', { name: 'Select user' })).toBeInTheDocument();
|
|
expect(screen.getByText('Invite another TREK user to share a combined vacation calendar.')).toBeInTheDocument();
|
|
expect(screen.queryByText('No users available')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('FE-MOB-MVACINV-003: falls back to the empty state when the lookup fails', async () => {
|
|
server.use(http.get('/api/addons/vacay/available-users', () => HttpResponse.json({ error: 'nope' }, { status: 500 })));
|
|
renderSheet();
|
|
await usersSettled(1);
|
|
|
|
expect(await screen.findByText('No users available')).toBeInTheDocument();
|
|
expect(screen.queryByRole('button', { name: 'Select user' })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('FE-MOB-MVACINV-004: the picker opens, selects a user and collapses again', async () => {
|
|
renderSheet();
|
|
fireEvent.click(await screen.findByRole('button', { name: 'Select user' }));
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: 'carol' }));
|
|
expect(await screen.findByRole('button', { name: 'carol' })).toBeInTheDocument();
|
|
// Only the trigger keeps the name once the list collapsed.
|
|
expect(screen.getAllByRole('button', { name: 'carol' })).toHaveLength(1);
|
|
});
|
|
|
|
it('FE-MOB-MVACINV-005: the send button stays disabled until someone is picked', async () => {
|
|
renderSheet();
|
|
const send = await screen.findByRole('button', { name: 'Send Invite' });
|
|
expect(send).toBeDisabled();
|
|
|
|
// The picker shows up once the users are in; nobody is picked yet.
|
|
fireEvent.click(await screen.findByRole('button', { name: 'Select user' }));
|
|
expect(screen.getByRole('button', { name: 'Send Invite' })).toBeDisabled();
|
|
fireEvent.click(screen.getByRole('button', { name: 'bob' }));
|
|
expect(screen.getByRole('button', { name: 'Send Invite' })).toBeEnabled();
|
|
});
|
|
|
|
it('FE-MOB-MVACINV-006: sending posts the picked user, toasts and closes', async () => {
|
|
let invitedId: number | undefined;
|
|
server.use(http.post('/api/addons/vacay/invite', async ({ request }) => {
|
|
invitedId = ((await request.json()) as { user_id: number }).user_id;
|
|
return HttpResponse.json({ success: true });
|
|
}));
|
|
const { onClose } = renderSheet();
|
|
|
|
fireEvent.click(await screen.findByRole('button', { name: 'Select user' }));
|
|
fireEvent.click(screen.getByRole('button', { name: 'bob' }));
|
|
fireEvent.click(screen.getByRole('button', { name: 'Send Invite' }));
|
|
|
|
await waitFor(() => expect(invitedId).toBe(2));
|
|
await waitFor(() => expect(onClose).toHaveBeenCalledTimes(1));
|
|
expect(toasts).toEqual([{ type: 'success', message: 'Invite sent' }]);
|
|
});
|
|
|
|
it('FE-MOB-MVACINV-007: a rejected invite surfaces the server error and keeps the sheet open', async () => {
|
|
server.use(http.post('/api/addons/vacay/invite', () =>
|
|
HttpResponse.json({ error: 'Already fused' }, { status: 409 })));
|
|
const { onClose } = renderSheet();
|
|
|
|
fireEvent.click(await screen.findByRole('button', { name: 'Select user' }));
|
|
fireEvent.click(screen.getByRole('button', { name: 'bob' }));
|
|
fireEvent.click(screen.getByRole('button', { name: 'Send Invite' }));
|
|
|
|
await waitFor(() => expect(toasts).toEqual([{ type: 'error', message: 'Already fused' }]));
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-MOB-MVACINV-008: pending invites are listed and can be withdrawn', async () => {
|
|
const cancelInvite = vi.fn(async (_userId: number) => {});
|
|
useVacayStore.setState({
|
|
pendingInvites: [{ user_id: 9, username: 'dan' }],
|
|
cancelInvite,
|
|
});
|
|
renderSheet();
|
|
|
|
const row = (await screen.findByText('dan')).closest('div') as HTMLElement;
|
|
expect(within(row).getByText('(pending)')).toBeInTheDocument();
|
|
|
|
fireEvent.click(within(row).getByRole('button', { name: 'Cancel' }));
|
|
expect(cancelInvite).toHaveBeenCalledWith(9);
|
|
await usersSettled(1);
|
|
});
|
|
|
|
it('FE-MOB-MVACINV-009: the close icon and the cancel button both dismiss the sheet', async () => {
|
|
const { onClose } = renderSheet();
|
|
|
|
fireEvent.click(await screen.findByRole('button', { name: 'Close' }));
|
|
const dialog = screen.getByRole('dialog', { name: 'Invite User' });
|
|
fireEvent.click(within(dialog).getAllByRole('button', { name: 'Cancel' })[0]);
|
|
expect(onClose).toHaveBeenCalledTimes(2);
|
|
await usersSettled(1);
|
|
});
|
|
|
|
it('FE-MOB-MVACINV-010: reopening resets the previous selection', async () => {
|
|
const { rerender } = render(<MVacayInviteSheet open onClose={() => {}} />);
|
|
fireEvent.click(await screen.findByRole('button', { name: 'Select user' }));
|
|
fireEvent.click(screen.getByRole('button', { name: 'bob' }));
|
|
expect(screen.getByRole('button', { name: 'bob' })).toBeInTheDocument();
|
|
|
|
rerender(<MVacayInviteSheet open={false} onClose={() => {}} />);
|
|
rerender(<MVacayInviteSheet open onClose={() => {}} />);
|
|
await usersSettled(2);
|
|
|
|
expect(await screen.findByRole('button', { name: 'Select user' })).toBeInTheDocument();
|
|
});
|
|
});
|