Skip to content
Snippets Groups Projects
Select Git revision
  • 1fe7ab7b68b07b35e752d7273aff2aaa3145f39c
  • main default protected
  • renovate/main-ghcr.io-renovatebot-base-image-11.x
  • chore/update-pr-template
  • chore/maintainers-rarkins
  • refactor/pin-new-value
  • fix/user-agent
  • feat/37517-base64-private-key
  • next
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • 41.82.6
  • 41.82.5
  • 41.82.4
  • 41.82.3
  • 41.82.2
  • 41.82.1
  • 41.82.0
  • 41.81.6
  • 41.81.5
  • 41.81.4
  • 41.81.3
  • 41.81.2
  • 41.81.1
  • 41.81.0
  • 41.80.0
  • 41.79.0
  • 41.78.1
  • 41.78.0
  • 41.77.0
  • 41.76.1
41 results

pretty-time.spec.ts

Blame
  • user avatar
    Sergei Zharinov authored and GitHub committed
    3d2445f9
    History
    pretty-time.spec.ts 3.39 KiB
    import { DateTime } from 'luxon';
    import { satisfiesDateRange, toMs } from './pretty-time';
    
    describe('util/pretty-time', () => {
      it.each`
        input                | expected
        ${'1h'}              | ${1 * 60 * 60 * 1000}
        ${' 1 h '}           | ${1 * 60 * 60 * 1000}
        ${'1 h'}             | ${1 * 60 * 60 * 1000}
        ${'1 hour'}          | ${1 * 60 * 60 * 1000}
        ${'1hour'}           | ${1 * 60 * 60 * 1000}
        ${'1h 1m'}           | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000}
        ${'1hour 1minute'}   | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000}
        ${'1 hour 1 minute'} | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000}
        ${'1h 1m 1s'}        | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000}
        ${'1h 1 m 1s'}       | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000}
        ${'1hour 1 min 1s'}  | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000}
        ${'1h 1m 1s 1ms'}    | ${1 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1000 + 1}
        ${'1d2h3m'}          | ${24 * 60 * 60 * 1000 + 2 * 60 * 60 * 1000 + 3 * 60 * 1000}
        ${'1 day'}           | ${24 * 60 * 60 * 1000}
        ${'3 days'}          | ${3 * 24 * 60 * 60 * 1000}
        ${'1 week'}          | ${7 * 24 * 60 * 60 * 1000}
        ${'1 month'}         | ${30 * 24 * 60 * 60 * 1000}
        ${'1 M'}             | ${30 * 24 * 60 * 60 * 1000}
        ${'2 months'}        | ${2 * 30 * 24 * 60 * 60 * 1000}
        ${'1month'}          | ${30 * 24 * 60 * 60 * 1000}
        ${'1M'}              | ${30 * 24 * 60 * 60 * 1000}
        ${'2months'}         | ${2 * 30 * 24 * 60 * 60 * 1000}
        ${'1 year'}          | ${365.25 * 24 * 60 * 60 * 1000}
        ${'0'.repeat(100)}   | ${0}
        ${'0'.repeat(101)}   | ${null}
        ${'1 whatever'}      | ${null}
        ${'whatever'}        | ${null}
        ${''}                | ${null}
        ${' '}               | ${null}
        ${'  \t\n   '}       | ${null}
        ${'minute'}          | ${null}
        ${'m'}               | ${null}
        ${'hour'}            | ${null}
        ${'h'}               | ${null}
      `(`toMs('$input') === $expected`, ({ input, expected }) => {
        expect(toMs(input)).toBe(expected);
      });
    
      it('returns null for error', () => {
        expect(toMs(null as never)).toBeNull();
      });
    
      describe('satisfiesDateRange()', () => {
        const t0 = DateTime.fromISO('2023-07-07T12:00:00Z');
    
        beforeAll(() => {
          jest.useFakeTimers();
        });
    
        beforeEach(() => {
          jest.setSystemTime(t0.toMillis());
        });
    
        it.each`
          date                                  | range              | expected
          ${'2023-01-01'}                       | ${'< 1 Y'}         | ${true}
          ${'2023-07-07'}                       | ${'< 1 day'}       | ${true}
          ${'2023-06-09'}                       | ${'<=1M'}          | ${true}
          ${'2020-01-01'}                       | ${'>= 1hrs'}       | ${true}
          ${'2023-07-07T11:12:00'}              | ${'<= 1hrs'}       | ${true}
          ${'2020-01-01'}                       | ${'< 2years'}      | ${false}
          ${new Date(Date.now()).toISOString()} | ${'< 3 days'}      | ${true}
          ${new Date(Date.now()).toISOString()} | ${'> 3 months'}    | ${false}
          ${'2020-01-01'}                       | ${'> 1 millenial'} | ${null}
          ${'invalid-date'}                     | ${'> 1 year'}      | ${null}
          ${'2020-01-01'}                       | ${'1 year'}        | ${null}
        `(
          `satisfiesRange('$date', '$range') === $expected`,
          ({ date, range, expected }) => {
            expect(satisfiesDateRange(date, range)).toBe(expected);
          },
        );
      });
    });