|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import { isBannerActive } from '../banner.mjs'; |
| 5 | + |
| 6 | +const PAST = new Date(Date.now() - 86_400_000).toISOString(); // yesterday |
| 7 | +const FUTURE = new Date(Date.now() + 86_400_000).toISOString(); // tomorrow |
| 8 | + |
| 9 | +const banner = (overrides = {}) => ({ |
| 10 | + text: 'Test banner', |
| 11 | + ...overrides, |
| 12 | +}); |
| 13 | + |
| 14 | +describe('isBannerActive', () => { |
| 15 | + describe('no startDate, no endDate', () => { |
| 16 | + it('is always active', () => { |
| 17 | + assert.equal(isBannerActive(banner()), true); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('startDate only', () => { |
| 22 | + it('is active when startDate is in the past', () => { |
| 23 | + assert.equal(isBannerActive(banner({ startDate: PAST })), true); |
| 24 | + }); |
| 25 | + |
| 26 | + it('is not active when startDate is in the future', () => { |
| 27 | + assert.equal(isBannerActive(banner({ startDate: FUTURE })), false); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('endDate only', () => { |
| 32 | + it('is active when endDate is in the future', () => { |
| 33 | + assert.equal(isBannerActive(banner({ endDate: FUTURE })), true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('is not active when endDate is in the past', () => { |
| 37 | + assert.equal(isBannerActive(banner({ endDate: PAST })), false); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('startDate and endDate', () => { |
| 42 | + it('is active when now is within the range', () => { |
| 43 | + assert.equal( |
| 44 | + isBannerActive(banner({ startDate: PAST, endDate: FUTURE })), |
| 45 | + true |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it('is not active when now is before the range', () => { |
| 50 | + assert.equal( |
| 51 | + isBannerActive(banner({ startDate: FUTURE, endDate: FUTURE })), |
| 52 | + false |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('is not active when now is after the range', () => { |
| 57 | + assert.equal( |
| 58 | + isBannerActive(banner({ startDate: PAST, endDate: PAST })), |
| 59 | + false |
| 60 | + ); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments