computeCustomDateTime.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { computeCustomDateTime } from '@superset-ui/core';
  20. const TODAY = '2024-06-03';
  21. // Mock Date to always return 2024-06-03
  22. beforeEach(() => {
  23. jest.useFakeTimers();
  24. jest.setSystemTime(new Date(TODAY).getTime());
  25. });
  26. afterEach(() => {
  27. jest.useRealTimers();
  28. });
  29. test('should return the current date for "now"', () => {
  30. expect(computeCustomDateTime('now', 'day', 0)).toEqual(new Date(TODAY));
  31. });
  32. test('should return the current date for "today"', () => {
  33. expect(computeCustomDateTime('today', 'day', 0)).toEqual(new Date(TODAY));
  34. });
  35. test('should return the date for "2024-06-03" with grain "day" and grainValue 2', () => {
  36. expect(computeCustomDateTime(TODAY, 'day', 2)).toEqual(
  37. new Date('2024-06-05T00:00:00Z'),
  38. );
  39. });
  40. test('should return the date for "2024-06-03" with grain "week" and grainValue 1', () => {
  41. expect(computeCustomDateTime(TODAY, 'week', 1)).toEqual(
  42. new Date('2024-06-10T00:00:00Z'),
  43. );
  44. });
  45. test('should return the date for "2024-06-03" with grain "month" and grainValue 1', () => {
  46. expect(computeCustomDateTime(TODAY, 'month', 1)).toEqual(
  47. new Date('2024-07-03T00:00:00Z'),
  48. );
  49. });
  50. test('should return the date for "2024-06-03" with grain "quarter" and grainValue 1', () => {
  51. expect(computeCustomDateTime(TODAY, 'quarter', 1)).toEqual(
  52. new Date('2024-09-03T00:00:00Z'),
  53. );
  54. });
  55. test('should return the date for "2024-06-03" with grain "year" and grainValue 1', () => {
  56. expect(computeCustomDateTime(TODAY, 'year', 1)).toEqual(
  57. new Date('2025-06-03T00:00:00Z'),
  58. );
  59. });
  60. test('should return null for an invalid date', () => {
  61. expect(computeCustomDateTime('invalid', 'day', 1)).toBeNull();
  62. });
  63. test('should return the date for "2024-06-03" with an invalid grain', () => {
  64. expect(computeCustomDateTime(TODAY, 'invalid', 1)).toEqual(
  65. new Date('2024-06-03T00:00:00Z'),
  66. );
  67. });