|
| 1 | +import { act, render } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | +import Notification from '../src/Notification'; |
| 4 | + |
| 5 | +function advanceFrames(count: number) { |
| 6 | + for (let frame = 0; frame < count; frame += 1) { |
| 7 | + act(() => vi.advanceTimersByTime(16)); |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +describe('Notification timer rendering', () => { |
| 12 | + beforeEach(() => vi.useFakeTimers()); |
| 13 | + afterEach(() => vi.useRealTimers()); |
| 14 | + |
| 15 | + it.each([undefined, false])( |
| 16 | + 'does not render hidden progress on timer ticks (%s)', |
| 17 | + (showProgress) => { |
| 18 | + const onRender = vi.fn(); |
| 19 | + const onClose = vi.fn(); |
| 20 | + const { queryByRole, unmount } = render( |
| 21 | + <React.Profiler id="notification" onRender={onRender}> |
| 22 | + <Notification |
| 23 | + prefixCls="rc-notification" |
| 24 | + description="Notice" |
| 25 | + duration={1} |
| 26 | + showProgress={showProgress} |
| 27 | + onClose={onClose} |
| 28 | + /> |
| 29 | + </React.Profiler>, |
| 30 | + ); |
| 31 | + const initialRenderCount = onRender.mock.calls.length; |
| 32 | + |
| 33 | + advanceFrames(32); |
| 34 | + |
| 35 | + expect(onRender).toHaveBeenCalledTimes(initialRenderCount); |
| 36 | + expect(queryByRole('progressbar')).toBeNull(); |
| 37 | + expect(onClose).not.toHaveBeenCalled(); |
| 38 | + |
| 39 | + advanceFrames(32); |
| 40 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 41 | + unmount(); |
| 42 | + }, |
| 43 | + ); |
| 44 | + |
| 45 | + it('continues rendering visible progress and closes at the original duration', () => { |
| 46 | + const onClose = vi.fn(); |
| 47 | + const { getByRole, unmount } = render( |
| 48 | + <Notification prefixCls="rc-notification" duration={1} showProgress onClose={onClose} />, |
| 49 | + ); |
| 50 | + |
| 51 | + advanceFrames(32); |
| 52 | + expect((getByRole('progressbar') as HTMLProgressElement).value).toBeCloseTo(48.8, 1); |
| 53 | + expect(onClose).not.toHaveBeenCalled(); |
| 54 | + |
| 55 | + advanceFrames(32); |
| 56 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 57 | + unmount(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('updates progress when enabled during an existing countdown without restarting it', () => { |
| 61 | + const onClose = vi.fn(); |
| 62 | + const onRender = vi.fn(); |
| 63 | + const notice = (showProgress: boolean) => ( |
| 64 | + <React.Profiler id="notification" onRender={onRender}> |
| 65 | + <Notification |
| 66 | + prefixCls="rc-notification" |
| 67 | + duration={1} |
| 68 | + showProgress={showProgress} |
| 69 | + onClose={onClose} |
| 70 | + /> |
| 71 | + </React.Profiler> |
| 72 | + ); |
| 73 | + const { rerender, getByRole, unmount } = render(notice(false)); |
| 74 | + |
| 75 | + advanceFrames(16); |
| 76 | + rerender(notice(true)); |
| 77 | + advanceFrames(16); |
| 78 | + expect((getByRole('progressbar') as HTMLProgressElement).value).toBeCloseTo(48.8, 1); |
| 79 | + |
| 80 | + rerender(notice(false)); |
| 81 | + const renderCount = onRender.mock.calls.length; |
| 82 | + advanceFrames(16); |
| 83 | + expect(onRender).toHaveBeenCalledTimes(renderCount); |
| 84 | + expect(onClose).not.toHaveBeenCalled(); |
| 85 | + |
| 86 | + advanceFrames(16); |
| 87 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 88 | + unmount(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('shows elapsed progress when enabled while the countdown is paused', () => { |
| 92 | + const onClose = vi.fn(); |
| 93 | + const notice = (showProgress: boolean, hovering: boolean) => ( |
| 94 | + <Notification |
| 95 | + prefixCls="rc-notification" |
| 96 | + duration={1} |
| 97 | + showProgress={showProgress} |
| 98 | + hovering={hovering} |
| 99 | + onClose={onClose} |
| 100 | + /> |
| 101 | + ); |
| 102 | + const { rerender, getByRole, unmount } = render(notice(false, false)); |
| 103 | + |
| 104 | + advanceFrames(16); |
| 105 | + rerender(notice(false, true)); |
| 106 | + advanceFrames(32); |
| 107 | + rerender(notice(true, true)); |
| 108 | + |
| 109 | + expect((getByRole('progressbar') as HTMLProgressElement).value).toBeCloseTo(74.4, 1); |
| 110 | + advanceFrames(16); |
| 111 | + expect((getByRole('progressbar') as HTMLProgressElement).value).toBeCloseTo(74.4, 1); |
| 112 | + expect(onClose).not.toHaveBeenCalled(); |
| 113 | + |
| 114 | + rerender(notice(true, false)); |
| 115 | + advanceFrames(48); |
| 116 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 117 | + unmount(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments