addons/isl/src/__tests__/Delayed.test.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {RenderResult} from '@testing-library/react';
b69ab319
b69ab3110import {act, render, screen} from '@testing-library/react';
b69ab3111import {Delayed} from '../Delayed';
b69ab3112
b69ab3113describe('<Delayed />', () => {
b69ab3114 let rendered: RenderResult | null = null;
b69ab3115
b69ab3116 const renderDelayed = (hideUntil: Date) => {
b69ab3117 rendered = render(
b69ab3118 <Delayed hideUntil={hideUntil}>
b69ab3119 <span>inner</span>
b69ab3120 </Delayed>,
b69ab3121 );
b69ab3122 };
b69ab3123
b69ab3124 beforeEach(() => {
b69ab3125 jest.useFakeTimers();
b69ab3126 });
b69ab3127
b69ab3128 afterEach(() => {
b69ab3129 if (rendered != null) {
b69ab3130 rendered.unmount();
b69ab3131 rendered = null;
b69ab3132 }
b69ab3133 jest.useRealTimers();
b69ab3134 });
b69ab3135
b69ab3136 it('hides children with a future "hideUntil"', () => {
b69ab3137 const future = new Date(Date.now() + 1);
b69ab3138 renderDelayed(future);
b69ab3139 expect(screen.queryByText('inner')).toBeNull();
b69ab3140 });
b69ab3141
b69ab3142 it('shows children with a past "hideUntil"', () => {
b69ab3143 const past = new Date(Date.now() - 1);
b69ab3144 renderDelayed(past);
b69ab3145 expect(screen.queryByText('inner')).toBeInTheDocument();
b69ab3146 });
b69ab3147
b69ab3148 it('hides then shows children with a future "hideUntil"', () => {
b69ab3149 const future = new Date(Date.now() + 1);
b69ab3150 renderDelayed(future);
b69ab3151 expect(screen.queryByText('inner')).toBeNull();
b69ab3152 act(() => {
b69ab3153 jest.advanceTimersByTime(1);
b69ab3154 });
b69ab3155 expect(screen.queryByText('inner')).toBeInTheDocument();
b69ab3156 });
b69ab3157});