pleroma/packages/pl-fe/src/toast.test.tsx

167 lines
4.7 KiB
TypeScript
Raw Normal View History

2022-12-20 07:47:02 -08:00
import { render } from '@testing-library/react';
// import { AxiosError, AxiosHeaders } from 'axios';
2022-12-20 07:47:02 -08:00
import React from 'react';
import { Toaster } from 'react-hot-toast';
2022-12-20 07:47:02 -08:00
import { IntlProvider } from 'react-intl';
import { act, screen } from 'pl-fe/jest/test-helpers';
2022-12-20 07:47:02 -08:00
import toast from './toast';
2022-12-20 07:47:02 -08:00
const renderApp = () => ({
toast,
...render(
<IntlProvider locale='en'>
<Toaster />,
</IntlProvider>,
),
});
2022-12-20 07:47:02 -08:00
beforeAll(() => {
2023-09-16 13:16:59 -07:00
vi.spyOn(console, 'error').mockImplementation(() => {});
2022-12-20 07:47:02 -08:00
});
afterEach(() => {
(console.error as any).mockClear();
});
afterAll(() => {
(console.error as any).mockRestore();
});
describe('toasts', () => {
2022-12-20 07:47:02 -08:00
it('renders successfully', async() => {
const { toast } = renderApp();
act(() => {
toast.success('hello');
});
expect(screen.getByTestId('toast')).toBeInTheDocument();
expect(screen.getByTestId('toast-message')).toHaveTextContent('hello');
});
describe('actionable button', () => {
it('renders the button', async() => {
const { toast } = renderApp();
act(() => {
toast.success('hello', { action: () => null, actionLabel: 'click me' });
});
expect(screen.getByTestId('toast-action')).toHaveTextContent('click me');
});
it('does not render the button', async() => {
const { toast } = renderApp();
act(() => {
toast.success('hello');
});
expect(screen.queryAllByTestId('toast-action')).toHaveLength(0);
});
});
// describe('showAlertForError()', () => {
// const buildError = (message: string, status: number) => new AxiosError<any>(message, String(status), undefined, null, {
// data: {
// error: message,
// },
// statusText: String(status),
// status,
// headers: {},
// config: {
// headers: new AxiosHeaders(),
// },
// });
// describe('with a 502 status code', () => {
// it('renders the correct message', async() => {
// const message = 'The server is down';
// const error = buildError(message, 502);
// const { toast } = renderApp();
// act(() => {
// toast.showAlertForError(error);
// });
// expect(screen.getByTestId('toast')).toBeInTheDocument();
// expect(screen.getByTestId('toast-message')).toHaveTextContent('The server is down');
// });
// });
// describe('with a 404 status code', () => {
// it('renders the correct message', async() => {
// const error = buildError('', 404);
// const { toast } = renderApp();
// act(() => {
// toast.showAlertForError(error);
// });
// expect(screen.queryAllByTestId('toast')).toHaveLength(0);
// });
// });
// describe('with a 410 status code', () => {
// it('renders the correct message', async() => {
// const error = buildError('', 410);
// const { toast } = renderApp();
// act(() => {
// toast.showAlertForError(error);
// });
// expect(screen.queryAllByTestId('toast')).toHaveLength(0);
// });
// });
// describe('with an accepted status code', () => {
// describe('with a message from the server', () => {
// it('renders the correct message', async() => {
// const message = 'custom message';
// const error = buildError(message, 200);
// const { toast } = renderApp();
// act(() => {
// toast.showAlertForError(error);
// });
// expect(screen.getByTestId('toast')).toBeInTheDocument();
// expect(screen.getByTestId('toast-message')).toHaveTextContent(message);
// });
// });
// describe('without a message from the server', () => {
// it('renders the correct message', async() => {
// const message = 'The request has been accepted for processing';
// const error = buildError(message, 202);
// const { toast } = renderApp();
// act(() => {
// toast.showAlertForError(error);
// });
// expect(screen.getByTestId('toast')).toBeInTheDocument();
// expect(screen.getByTestId('toast-message')).toHaveTextContent(message);
// });
// });
// });
// // describe('without a response', () => {
// // it('renders the default message', async() => {
// // const error = new AxiosError();
// // const { toast } = renderApp();
// // act(() => {
// // toast.showAlertForError(error);
// // });
// // expect(screen.getByTestId('toast')).toBeInTheDocument();
// // expect(screen.getByTestId('toast-message')).toHaveTextContent('An unexpected error occurred.');
// // });
// // });
// });
});