forked from cadence-workflow/cadence-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule-actions-modal-content.test.tsx
More file actions
200 lines (167 loc) · 5.71 KB
/
Copy pathschedule-actions-modal-content.test.tsx
File metadata and controls
200 lines (167 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { HttpResponse } from 'msw';
import { render, screen, userEvent, waitFor } from '@/test-utils/rtl';
import { mockDescribeScheduleResponse } from '@/route-handlers/describe-schedule/__fixtures__/mock-describe-schedule-response';
import { type PauseScheduleResponse } from '@/route-handlers/pause-schedule/pause-schedule.types';
import { type UnpauseScheduleResponse } from '@/route-handlers/unpause-schedule/unpause-schedule.types';
import { mockScheduleActionsConfig } from '../../__fixtures__/schedule-actions-config';
import { type ScheduleAction } from '../../schedule-actions.types';
import ScheduleActionsModalContent from '../schedule-actions-modal-content';
const mockScheduleParams = {
domain: 'mock-domain',
cluster: 'mock-cluster',
scheduleId: 'mock-schedule-id',
};
const mockEnqueue = jest.fn();
const mockDequeue = jest.fn();
jest.mock('baseui/snackbar', () => ({
...jest.requireActual('baseui/snackbar'),
useSnackbar: () => ({
enqueue: mockEnqueue,
dequeue: mockDequeue,
}),
}));
const mockPush = jest.fn();
jest.mock('next/navigation', () => ({
...jest.requireActual('next/navigation'),
useRouter: () => ({ push: mockPush }),
}));
describe(ScheduleActionsModalContent.name, () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('renders the modal content as expected', async () => {
setup({});
expect(await screen.findAllByText('Mock pause schedule')).toHaveLength(2);
expect(screen.getByText('Mock pause banner message')).toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'Mock pause schedule' })
).toBeInTheDocument();
});
it('calls onCloseModal when the Cancel button is clicked', async () => {
const { user, mockOnClose } = setup({});
await user.click(await screen.findByText('Cancel'));
expect(mockOnClose).toHaveBeenCalled();
});
it('calls pause API, sends toast, and closes modal when confirmed', async () => {
const { user, mockOnClose, getLatestRequestBody, waitForRequest } = setup(
{}
);
await user.type(
screen.getByTestId('mock-pause-reason'),
'Mock pause reason'
);
await user.click(
await screen.findByRole('button', { name: 'Mock pause schedule' })
);
await waitForRequest();
expect(getLatestRequestBody()).toEqual({ reason: 'Mock pause reason' });
await waitFor(() => {
expect(mockEnqueue).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Mock pause notification',
})
);
});
expect(mockOnClose).toHaveBeenCalled();
});
it('renders pause banner without describe schedule data', async () => {
setup({ schedule: undefined });
expect(
await screen.findByText('Mock pause banner message')
).toBeInTheDocument();
});
it('displays banner when the action fails', async () => {
const { user, mockOnClose } = setup({ error: true });
await user.type(
screen.getByTestId('mock-pause-reason'),
'Mock pause reason'
);
await user.click(
await screen.findByRole('button', { name: 'Mock pause schedule' })
);
await waitFor(() => {
expect(screen.getByText('Failed to pause schedule')).toBeInTheDocument();
});
expect(mockOnClose).not.toHaveBeenCalled();
});
describe('form handling', () => {
it('renders form when provided in action config', () => {
setup({});
expect(screen.getByTestId('mock-pause-form')).toBeInTheDocument();
expect(screen.getByTestId('mock-pause-reason')).toBeInTheDocument();
});
it('disables submit button when form has validation errors', async () => {
const { user } = setup({});
const submitButton = screen.getByRole('button', {
name: 'Mock pause schedule',
});
await user.click(submitButton);
expect(submitButton).toHaveAttribute('disabled');
});
it('shows validation error when reason is empty', async () => {
const { user } = setup({});
const submitButton = screen.getByRole('button', {
name: 'Mock pause schedule',
});
await user.click(submitButton);
expect(screen.getByTestId('mock-pause-reason')).toHaveAttribute(
'aria-invalid',
'true'
);
});
});
});
function setup({
error,
actionConfig,
schedule = mockDescribeScheduleResponse,
}: {
error?: boolean;
actionConfig?: ScheduleAction<any, any, any>;
schedule?: typeof mockDescribeScheduleResponse;
}) {
const user = userEvent.setup();
const mockOnClose = jest.fn();
let latestRequestBody: unknown = null;
let requestPromiseResolve: (value: unknown) => void = () => undefined;
const requestPromise = new Promise((resolve) => {
requestPromiseResolve = resolve;
});
render(
<ScheduleActionsModalContent
action={actionConfig ?? mockScheduleActionsConfig[0]}
params={{ ...mockScheduleParams }}
schedule={schedule}
onCloseModal={mockOnClose}
/>,
{
endpointsMocks: [
{
path: '/api/domains/:domain/:cluster/schedules/:scheduleId/:action',
httpMethod: 'POST',
mockOnce: false,
httpResolver: async ({ request }) => {
const text = await request.text();
latestRequestBody = text ? JSON.parse(text) : null;
requestPromiseResolve(null);
if (error) {
return HttpResponse.json(
{ message: 'Failed to pause schedule' },
{ status: 500 }
);
}
return HttpResponse.json(
{} satisfies PauseScheduleResponse | UnpauseScheduleResponse
);
},
},
],
}
);
return {
user,
mockOnClose,
getLatestRequestBody: () => latestRequestBody,
waitForRequest: () => requestPromise,
};
}