79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { PlayerShellComponent } from './player-shell.component';
|
|
|
|
type FetchMock = ReturnType<typeof vi.fn>;
|
|
|
|
function jsonResponse(status: number, body: unknown) {
|
|
return {
|
|
ok: status >= 200 && status < 300,
|
|
status,
|
|
json: vi.fn().mockResolvedValue(body),
|
|
} as unknown as Response;
|
|
}
|
|
|
|
describe('PlayerShellComponent gameplay wiring', () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it('clears selected guess when refreshed status is no longer guess', async () => {
|
|
const fetchMock: FetchMock = vi.fn().mockResolvedValue(
|
|
jsonResponse(200, {
|
|
session: { code: 'ABCD12', status: 'reveal', current_round: 1 },
|
|
round_question: { id: 11, prompt: 'Q?', answers: [{ text: 'A' }] },
|
|
})
|
|
);
|
|
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const component = new PlayerShellComponent();
|
|
component.sessionCode = 'abcd12';
|
|
component.selectedGuess = 'A';
|
|
|
|
await component.refreshSession();
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
'/lobby/sessions/ABCD12',
|
|
expect.objectContaining({ method: 'GET' })
|
|
);
|
|
expect(component.selectedGuess).toBe('');
|
|
});
|
|
|
|
it('surfaces lie submit error and allows retry success flow', async () => {
|
|
const fetchMock: FetchMock = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(jsonResponse(500, { error: 'Temporary submit outage' }))
|
|
.mockResolvedValueOnce(jsonResponse(200, { ok: true }))
|
|
.mockResolvedValueOnce(
|
|
jsonResponse(200, {
|
|
session: { code: 'ABCD12', status: 'guess', current_round: 1 },
|
|
round_question: { id: 11, prompt: 'Q?', answers: [{ text: 'A' }, { text: 'B' }] },
|
|
})
|
|
);
|
|
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const component = new PlayerShellComponent();
|
|
component.sessionCode = 'ABCD12';
|
|
component.playerId = 9;
|
|
component.sessionToken = 'token-1';
|
|
component.lieText = 'my lie';
|
|
component.session = {
|
|
session: { code: 'ABCD12', status: 'lie', current_round: 1 },
|
|
round_question: { id: 11, prompt: 'Q?', answers: [] },
|
|
};
|
|
|
|
await component.submitLie();
|
|
|
|
expect(component.submitError?.kind).toBe('lie');
|
|
expect(component.submitError?.message).toContain('Lie submit failed: Temporary submit outage');
|
|
|
|
await component.submitLie();
|
|
|
|
expect(component.submitError).toBeNull();
|
|
expect(component.session?.session.status).toBe('guess');
|
|
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
});
|
|
});
|