// @vitest-environment jsdom
import {afterEach, describe, expect, it, vi} from 'vitest';
import {cleanup, fireEvent, render, screen} from '@testing-library/react';
import ResendConfirmation from './ResendConfirmation.jsx';
import {fetchRoutes, json} from '../test/fetchRoutes.js';
vi.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key, options) => (options?.minutes === undefined ? key : `${key}:${options.minutes}`),
i18n: {language: 'de'},
}),
}));
describe('ResendConfirmation', () => {
afterEach(cleanup);
it('sends the prefilled address and shows the same answer for any address', async () => {
const mock = fetchRoutes({'POST /api/registration/resend': json(204, null)});
render();
fireEvent.click(screen.getByRole('button', {name: 'REGISTRATION.RESEND'}));
expect(await screen.findByText('REGISTRATION.RESEND_SENT')).toBeTruthy();
expect(JSON.parse(mock.mock.calls[0][1].body)).toEqual({email: 'a@b.de'});
});
it('shows the wait time when the IP is blocked', async () => {
fetchRoutes({
'POST /api/registration/resend': {...json(429, {}), headers: new Headers({'Retry-After': '900'})},
});
render();
fireEvent.click(screen.getByRole('button', {name: 'REGISTRATION.RESEND'}));
expect(await screen.findByText('REGISTRATION.ERROR_TOO_MANY_ATTEMPTS:15')).toBeTruthy();
});
});