github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/frontend-service/src/ui/components/FormattedDate/FormattedDate.test.tsx (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  import { render } from '@testing-library/react';
    17  import { FormattedDate } from './FormattedDate';
    18  
    19  describe('Relative Date Calculation', () => {
    20      // the test release date ===  18/06/2001 is constant across this test
    21      const testReleaseDate = new Date(2001, 5, 8);
    22  
    23      const data = [
    24          {
    25              name: 'now',
    26              systemTime: new Date(2001, 5, 8, 0, 0),
    27              expected: 'just now',
    28          },
    29          {
    30              name: '1 minute ago',
    31              systemTime: new Date(2001, 5, 8, 0, 1),
    32              expected: '1 minute ago',
    33          },
    34          {
    35              name: '5 minutes ago',
    36              systemTime: new Date(2001, 5, 8, 0, 5),
    37              expected: '5 minutes ago',
    38          },
    39          {
    40              name: '1 hour ago',
    41              systemTime: new Date(2001, 5, 8, 1, 1),
    42              expected: '1 hour ago',
    43          },
    44          {
    45              name: '5 hours ago',
    46              systemTime: new Date(2001, 5, 8, 5, 1),
    47              expected: '5 hours ago',
    48          },
    49          {
    50              name: 'little over 1 day ago',
    51              systemTime: new Date(2001, 5, 9, 1, 1),
    52              expected: '1 day ago',
    53          },
    54          {
    55              name: '3 days ago',
    56              systemTime: new Date(2001, 5, 11, 1, 1),
    57              expected: '3 days ago',
    58          },
    59          {
    60              name: '3 months ago',
    61              systemTime: new Date(2001, 8, 8, 5, 1),
    62              expected: '3 months ago',
    63          },
    64      ];
    65  
    66      describe.each(data)('calculates the right date and time', (testcase) => {
    67          it(testcase.name, () => {
    68              // given
    69              jest.useFakeTimers(); // fake time is now "0"
    70              jest.setSystemTime(testcase.systemTime.valueOf()); // time is now at the exact moment when release was created
    71              const { container } = render(<FormattedDate createdAt={testReleaseDate} />);
    72  
    73              // then
    74              expect(container.textContent).toContain(testcase.expected);
    75  
    76              // finally
    77              jest.runOnlyPendingTimers();
    78              jest.useRealTimers();
    79          });
    80      });
    81  });