|
1 | 1 | import MockDate from 'mockdate' |
2 | 2 | import dayjs from '../../src' |
| 3 | +import customParseFormat from '../../src/plugin/customParseFormat' |
3 | 4 | import devHelper from '../../src/plugin/devHelper' |
| 5 | +import localeData from '../../src/plugin/localeData' |
4 | 6 |
|
5 | 7 | dayjs.extend(devHelper) |
6 | 8 |
|
@@ -38,3 +40,46 @@ it('Warning: Setting locale before loading locale', () => { |
38 | 40 | dayjs.locale('zh-cn') |
39 | 41 | expect(consoleSpy).toHaveBeenCalledWith('Guessing you may want to use locale zh-cn, you have to load it before using it. https://day.js.org/docs/en/i18n/loading-into-nodejs') |
40 | 42 | }) |
| 43 | + |
| 44 | +describe('dev-helper: diff() usage warnings', () => { |
| 45 | + const diffWarningMsg = 'Invalid usage: diff() requires a valid comparison date as the first argument. https://day.js.org/docs/en/display/difference' |
| 46 | + |
| 47 | + beforeAll(() => { |
| 48 | + dayjs.extend(customParseFormat) |
| 49 | + dayjs.extend(localeData) |
| 50 | + }) |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + jest.clearAllMocks() |
| 54 | + }) |
| 55 | + |
| 56 | + it('warns when diff() is called with no comparison date', () => { |
| 57 | + const consoleSpy = jest.spyOn(console, 'warn') |
| 58 | + dayjs('2025-01-10').diff() |
| 59 | + expect(consoleSpy).toHaveBeenCalledWith(diffWarningMsg) |
| 60 | + }) |
| 61 | + |
| 62 | + it('warns when diff() is called with just the unit', () => { |
| 63 | + const consoleSpy = jest.spyOn(console, 'warn') |
| 64 | + dayjs('2025-01-10').diff('days') |
| 65 | + expect(consoleSpy).toHaveBeenCalledWith(diffWarningMsg) |
| 66 | + }) |
| 67 | + |
| 68 | + it('warns when diff() is called with an invalid comparison date (unparsable string)', () => { |
| 69 | + const consoleSpy = jest.spyOn(console, 'warn') |
| 70 | + dayjs('2025-01-10').diff('invalid-date', 'days') |
| 71 | + expect(consoleSpy).toHaveBeenCalledWith(diffWarningMsg) |
| 72 | + }) |
| 73 | + |
| 74 | + it('does NOT warn when diff() is called with a valid string date', () => { |
| 75 | + const consoleSpy = jest.spyOn(console, 'warn') |
| 76 | + dayjs('2025-01-10').diff('2025-01-09', 'days') |
| 77 | + expect(consoleSpy).not.toHaveBeenCalledWith(diffWarningMsg) |
| 78 | + }) |
| 79 | + |
| 80 | + it('does NOT warn when diff() is called with a valid Day.js instance', () => { |
| 81 | + const consoleSpy = jest.spyOn(console, 'warn') |
| 82 | + dayjs('2025-01-10').diff(dayjs(), 'days') |
| 83 | + expect(consoleSpy).not.toHaveBeenCalledWith(diffWarningMsg) |
| 84 | + }) |
| 85 | +}) |
0 commit comments