| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {RenderResult} from '@testing-library/react'; |
| 9 | |
| 10 | import {act, render, screen} from '@testing-library/react'; |
| 11 | import {Delayed} from '../Delayed'; |
| 12 | |
| 13 | describe('<Delayed />', () => { |
| 14 | let rendered: RenderResult | null = null; |
| 15 | |
| 16 | const renderDelayed = (hideUntil: Date) => { |
| 17 | rendered = render( |
| 18 | <Delayed hideUntil={hideUntil}> |
| 19 | <span>inner</span> |
| 20 | </Delayed>, |
| 21 | ); |
| 22 | }; |
| 23 | |
| 24 | beforeEach(() => { |
| 25 | jest.useFakeTimers(); |
| 26 | }); |
| 27 | |
| 28 | afterEach(() => { |
| 29 | if (rendered != null) { |
| 30 | rendered.unmount(); |
| 31 | rendered = null; |
| 32 | } |
| 33 | jest.useRealTimers(); |
| 34 | }); |
| 35 | |
| 36 | it('hides children with a future "hideUntil"', () => { |
| 37 | const future = new Date(Date.now() + 1); |
| 38 | renderDelayed(future); |
| 39 | expect(screen.queryByText('inner')).toBeNull(); |
| 40 | }); |
| 41 | |
| 42 | it('shows children with a past "hideUntil"', () => { |
| 43 | const past = new Date(Date.now() - 1); |
| 44 | renderDelayed(past); |
| 45 | expect(screen.queryByText('inner')).toBeInTheDocument(); |
| 46 | }); |
| 47 | |
| 48 | it('hides then shows children with a future "hideUntil"', () => { |
| 49 | const future = new Date(Date.now() + 1); |
| 50 | renderDelayed(future); |
| 51 | expect(screen.queryByText('inner')).toBeNull(); |
| 52 | act(() => { |
| 53 | jest.advanceTimersByTime(1); |
| 54 | }); |
| 55 | expect(screen.queryByText('inner')).toBeInTheDocument(); |
| 56 | }); |
| 57 | }); |
| 58 | |