Skip to content

Commit

Permalink
test(Toast): reduce test run time (#12538)
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Dec 30, 2023
1 parent 2f9f961 commit 329cfcd
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/vant/src/toast/test/function.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import {
} from '../function-call';

test('toast disappeared after duration', async () => {
vi.useFakeTimers();
const onClose = vi.fn();
showToast({
duration: 10,
onClose,
});

expect(onClose).toHaveBeenCalledTimes(0);
await later(50);
expect(onClose).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(100);
expect(onClose).toHaveBeenCalledTimes(1);
vi.useRealTimers();
});

test('show loading toast', async () => {
Expand All @@ -31,17 +33,19 @@ test('show loading toast', async () => {
});

test('show html toast', async () => {
vi.useFakeTimers();
showToast({
type: 'html',
className: 'html-toast',
message: '<div>Message</div>',
});
await vi.runAllTimersAsync();

await later(1000);
const toastText = document.querySelector(
'.html-toast .van-toast__text',
) as HTMLDivElement;
expect(toastText.innerHTML).toEqual('<div>Message</div>');
vi.useRealTimers();
});

test('icon prop', async () => {
Expand Down Expand Up @@ -108,15 +112,15 @@ test('clear multiple toast', async () => {
});

test('remove toast DOM when cleared in multiple mode', async () => {
vi.useFakeTimers();
allowMultipleToast();
closeToast(true);
const toast = showToast({ className: 'remove-toast' });
await later();

await toast.close();
await later(100);
toast.close();
await vi.advanceTimersByTimeAsync(100);
expect(document.querySelector('.remove-toast')).toBeNull();
allowMultipleToast(false);
vi.useRealTimers();
});

test('set default options', async () => {
Expand Down Expand Up @@ -151,13 +155,15 @@ test('set default options by type', async () => {
});

test('toast duration 0', async () => {
vi.useFakeTimers();
allowMultipleToast();
const onClose = vi.fn();
showToast({ duration: 0, onClose });

await later(2100);
expect(onClose).toHaveBeenCalledTimes(0);
await vi.advanceTimersByTimeAsync(100);
expect(onClose).not.toHaveBeenCalled();
allowMultipleToast(false);
vi.useRealTimers();
});

test('should trigger onClose callback after closed', async () => {
Expand Down

0 comments on commit 329cfcd

Please sign in to comment.