go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/common/components/relative_timestamp/relative_timestamp.test.tsx (about)

     1  // Copyright 2022 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import { act, render, screen } from '@testing-library/react';
    16  import { DateTime, Duration } from 'luxon';
    17  
    18  import './relative_timestamp';
    19  import { RelativeTimestamp } from './relative_timestamp';
    20  
    21  describe('RelativeTimestamp', () => {
    22    beforeEach(() => {
    23      jest.useFakeTimers();
    24    });
    25  
    26    afterEach(() => {
    27      jest.useRealTimers();
    28    });
    29  
    30    test('should display timestamp in the past correctly', async () => {
    31      const timestamp = DateTime.now().minus(
    32        Duration.fromObject({ seconds: 20, millisecond: 500 }),
    33      );
    34  
    35      render(<RelativeTimestamp timestamp={timestamp} />);
    36  
    37      expect(screen.queryByText('20 secs ago')).not.toBeNull();
    38    });
    39  
    40    test('should display timestamp in the future correctly', async () => {
    41      const timestamp = DateTime.now().plus(
    42        Duration.fromObject({ seconds: 20, millisecond: 500 }),
    43      );
    44  
    45      render(<RelativeTimestamp timestamp={timestamp} />);
    46  
    47      expect(screen.queryByText('in 20 secs')).not.toBeNull();
    48    });
    49  
    50    test('should update timestamp correctly', async () => {
    51      const timestamp = DateTime.now().plus(
    52        Duration.fromObject({ seconds: 2, millisecond: 500 }),
    53      );
    54  
    55      render(<RelativeTimestamp timestamp={timestamp} />);
    56  
    57      expect(screen.queryByText('in 2 secs')).not.toBeNull();
    58  
    59      await act(async () => {
    60        await jest.advanceTimersByTimeAsync(5 * 1000);
    61      });
    62  
    63      expect(screen.queryByText('2 secs ago')).not.toBeNull();
    64    });
    65  });