soapbox/app/soapbox/actions/__tests__/alerts.test.ts

147 wiersze
4.0 KiB
TypeScript
Czysty Zwykły widok Historia

2022-05-27 19:22:20 +00:00
import { AxiosError } from 'axios';
2022-07-06 22:02:44 +00:00
import { mockStore, rootState } from 'soapbox/jest/test-helpers';
2022-05-27 19:22:20 +00:00
import { dismissAlert, showAlert, showAlertForError } from '../alerts';
2022-07-06 22:02:44 +00:00
const buildError = (message: string, status: number) => new AxiosError<any>(message, String(status), undefined, null, {
2022-05-27 19:22:20 +00:00
data: {
error: message,
},
statusText: String(status),
status,
headers: {},
config: {},
});
2022-07-06 22:02:44 +00:00
let store: ReturnType<typeof mockStore>;
2022-05-27 19:22:20 +00:00
beforeEach(() => {
2022-07-06 22:02:44 +00:00
const state = rootState;
2022-05-27 19:22:20 +00:00
store = mockStore(state);
});
describe('dismissAlert()', () => {
it('dispatches the proper actions', async() => {
const alert = 'hello world';
const expectedActions = [
{ type: 'ALERT_DISMISS', alert },
];
2022-07-06 22:02:44 +00:00
await store.dispatch(dismissAlert(alert as any));
2022-05-27 19:22:20 +00:00
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
describe('showAlert()', () => {
it('dispatches the proper actions', async() => {
const title = 'title';
const message = 'msg';
const severity = 'info';
const expectedActions = [
{ type: 'ALERT_SHOW', title, message, severity },
];
await store.dispatch(showAlert(title, message, severity));
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
describe('showAlert()', () => {
describe('with a 502 status code', () => {
it('dispatches the proper actions', async() => {
const message = 'The server is down';
const error = buildError(message, 502);
const expectedActions = [
{ type: 'ALERT_SHOW', title: '', message, severity: 'error' },
];
await store.dispatch(showAlertForError(error));
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
describe('with a 404 status code', () => {
it('dispatches the proper actions', async() => {
const error = buildError('', 404);
await store.dispatch(showAlertForError(error));
const actions = store.getActions();
2022-07-06 22:02:44 +00:00
expect(actions).toEqual([]);
2022-05-27 19:22:20 +00:00
});
});
describe('with a 410 status code', () => {
it('dispatches the proper actions', async() => {
const error = buildError('', 410);
await store.dispatch(showAlertForError(error));
const actions = store.getActions();
2022-07-06 22:02:44 +00:00
expect(actions).toEqual([]);
2022-05-27 19:22:20 +00:00
});
});
describe('with an accepted status code', () => {
describe('with a message from the server', () => {
it('dispatches the proper actions', async() => {
const message = 'custom message';
const error = buildError(message, 200);
const expectedActions = [
{ type: 'ALERT_SHOW', title: '', message, severity: 'error' },
];
await store.dispatch(showAlertForError(error));
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
describe('without a message from the server', () => {
it('dispatches the proper actions', async() => {
const message = 'The request has been accepted for processing';
const error = buildError(message, 202);
const expectedActions = [
{ type: 'ALERT_SHOW', title: '', message, severity: 'error' },
];
await store.dispatch(showAlertForError(error));
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
});
describe('without a response', () => {
it('dispatches the proper actions', async() => {
const error = new AxiosError();
const expectedActions = [
{
type: 'ALERT_SHOW',
title: {
defaultMessage: 'Oops!',
id: 'alert.unexpected.title',
},
message: {
defaultMessage: 'An unexpected error occurred.',
id: 'alert.unexpected.message',
},
severity: 'error',
},
];
await store.dispatch(showAlertForError(error));
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
});