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