Skip to content

Commit 6f94cef

Browse files
committed
test: fixed tests
Signed-off-by: Evzen Gasta <evzen.ml@seznam.cz>
1 parent dcd0239 commit 6f94cef

File tree

1 file changed

+110
-3
lines changed

1 file changed

+110
-3
lines changed

packages/renderer/src/lib/toast/ToastTaskNotifications.spec.ts

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import '@testing-library/jest-dom/vitest';
2020

2121
import { render, waitFor } from '@testing-library/svelte';
2222
import { toast } from '@zerodevx/svelte-toast';
23-
import { beforeAll, beforeEach, expect, test, vi } from 'vitest';
23+
import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
2424

2525
import { tasksInfo } from '/@/stores/tasks';
2626
import type { TaskInfo } from '/@api/taskInfo';
@@ -72,7 +72,6 @@ test('Check a toast is being created when there is a task created', async () =>
7272
expect(toast.push).toHaveBeenCalledWith(
7373
IN_PROGRESS_TASK.name,
7474
expect.objectContaining({
75-
initial: 0,
7675
dismissable: false,
7776
component: {
7877
props: expect.objectContaining({
@@ -129,7 +128,6 @@ test('Check a toast is being updated after a task is updated', async () => {
129128
expect(toast.set).toHaveBeenCalledWith(
130129
dummyToastId,
131130
expect.objectContaining({
132-
initial: 0,
133131
dismissable: false,
134132
component: {
135133
props: expect.objectContaining({
@@ -141,3 +139,112 @@ test('Check a toast is being updated after a task is updated', async () => {
141139
}),
142140
);
143141
});
142+
143+
describe('Toast disappearing and notifying again', () => {
144+
beforeEach(() => {
145+
vi.useFakeTimers();
146+
});
147+
afterEach(() => {
148+
vi.useRealTimers();
149+
});
150+
151+
test('Check that toast is being pushed again if the task took longer than 60s', async () => {
152+
// make it enabled
153+
vi.mocked(window.getConfigurationValue).mockResolvedValue(true);
154+
155+
// return a toast id when we create one
156+
const dummyToastId = 1256;
157+
vi.mocked(toast.push).mockReturnValue(dummyToastId);
158+
159+
tasksInfo.set([IN_PROGRESS_TASK]);
160+
161+
render(ToastTaskNotifications, {});
162+
163+
// check we have called toast library to create a toast
164+
await waitFor(() => expect(toast.push).toHaveBeenCalled());
165+
166+
// Simulate that the task is now doing its stuff for 1 min
167+
vi.advanceTimersByTime(60000);
168+
169+
// ok, now update the task to go from in-progress to success
170+
const updatedTask: TaskInfo = {
171+
...IN_PROGRESS_TASK,
172+
status: 'success',
173+
state: 'completed',
174+
};
175+
// reset call to toast.set
176+
vi.mocked(toast.set).mockClear();
177+
tasksInfo.set([updatedTask]);
178+
179+
// check we have called toast library to update a toast
180+
await waitFor(() => expect(toast.set).toHaveBeenCalled());
181+
182+
// check we have called toast library to update the toast
183+
expect(toast.set).toHaveBeenCalledWith(
184+
dummyToastId,
185+
expect.objectContaining({
186+
dismissable: false,
187+
component: {
188+
props: expect.objectContaining({
189+
taskInfo: updatedTask,
190+
}),
191+
sendIdTo: 'toastId',
192+
src: expect.anything(),
193+
},
194+
}),
195+
);
196+
197+
// Check that the toast is being pushed again
198+
await waitFor(() => expect(toast.push).toHaveBeenCalledTimes(2));
199+
});
200+
201+
test('Check that toast is not being pushed again if the task took less than 60s', async () => {
202+
// make it enabled
203+
vi.mocked(window.getConfigurationValue).mockResolvedValue(true);
204+
205+
// return a toast id when we create one
206+
const dummyToastId = 1256;
207+
vi.mocked(toast.push).mockReturnValue(dummyToastId);
208+
209+
tasksInfo.set([IN_PROGRESS_TASK]);
210+
211+
render(ToastTaskNotifications, {});
212+
213+
// check we have called toast library to create a toast
214+
await waitFor(() => expect(toast.push).toHaveBeenCalled());
215+
216+
// Simulate that the task is now doing its stuff for 30sec
217+
vi.advanceTimersByTime(30000);
218+
219+
// ok, now update the task to go from in-progress to success
220+
const updatedTask: TaskInfo = {
221+
...IN_PROGRESS_TASK,
222+
status: 'success',
223+
state: 'completed',
224+
};
225+
// reset call to toast.set
226+
vi.mocked(toast.set).mockClear();
227+
tasksInfo.set([updatedTask]);
228+
229+
// check we have called toast library to update a toast
230+
await waitFor(() => expect(toast.set).toHaveBeenCalled());
231+
232+
// check we have called toast library to update the toast
233+
expect(toast.set).toHaveBeenCalledWith(
234+
dummyToastId,
235+
expect.objectContaining({
236+
dismissable: false,
237+
component: {
238+
props: expect.objectContaining({
239+
taskInfo: updatedTask,
240+
}),
241+
sendIdTo: 'toastId',
242+
src: expect.anything(),
243+
},
244+
}),
245+
);
246+
247+
// Check that the toast is being pushed again
248+
await waitFor(() => expect(toast.push).toHaveBeenCalledTimes(1));
249+
});
250+
});

0 commit comments

Comments
 (0)