docbrown/app/javascript/utilities/__tests__/showUserAlertModal.test.js
Lawrence 56e10f1306
Notifications Initializer Migration to Pack tag (#19124)
Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>
Co-authored-by: Lawrence S <lboogie@Lawrences-Air.attlocal.net>
Co-authored-by: Rajat Talesra <rajat@forem.com>
2023-04-14 17:50:07 -04:00

73 lines
2 KiB
JavaScript

import 'isomorphic-fetch';
import {
getModalHtml,
showModalAfterError,
} from '../../utilities/showUserAlertModal';
describe('ShowUserAlert Utility', () => {
beforeEach(() => {
const mockShowModal = jest.fn();
global.window.Forem = { showModal: mockShowModal };
});
it('should return modal html', () => {
const modalHtml = getModalHtml('Sample text', 'Sample Confirm Text');
expect(modalHtml).toContain('Sample text');
});
test('shows rate limit modal if response status is 429', async () => {
const response = new Response('', { status: 429 });
const showRateLimitModal = jest.fn();
const showUserAlertModal = jest.fn();
await showModalAfterError({
response,
element: 'post',
action_ing: 'creating',
action_past: 'created',
timeframe: '5 minutes',
showRateLimitModal,
showUserAlertModal,
});
expect(showUserAlertModal).not.toHaveBeenCalled();
});
test('shows user alert modal if response status is not 429', async () => {
const response = new Response(
JSON.stringify({ error: 'Something went wrong' }),
);
const showRateLimitModal = jest.fn();
const showUserAlertModal = jest.fn();
await showModalAfterError({
response,
element: 'post',
action_ing: 'creating',
action_past: 'created',
timeframe: '5 minutes',
showRateLimitModal,
showUserAlertModal,
});
expect(showRateLimitModal).not.toHaveBeenCalled();
});
test('shows user alert modal if response cannot be parsed as JSON', async () => {
const response = new Response('Something went wrong', { status: 500 });
const showRateLimitModal = jest.fn();
const showUserAlertModal = jest.fn();
await showModalAfterError({
response,
element: 'post',
action_ing: 'creating',
action_past: 'created',
timeframe: '5 minutes',
showRateLimitModal,
showUserAlertModal,
});
expect(showRateLimitModal).not.toHaveBeenCalled();
});
});