|
| 1 | +import { create, is } from '@metamask/superstruct'; |
| 2 | +import { DateTime } from 'luxon'; |
| 3 | + |
| 4 | +import { ISO8601DateStruct } from './time'; |
| 5 | + |
| 6 | +describe('ISO8601DateStruct', () => { |
| 7 | + it('should return true for a valid ISO 8601 date', () => { |
| 8 | + const value = DateTime.now().toISO(); |
| 9 | + expect(is(value, ISO8601DateStruct)).toBe(true); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should return false for an invalid ISO 8601 date', () => { |
| 13 | + const value = 'Mon Mar 31 2025'; |
| 14 | + expect(is(value, ISO8601DateStruct)).toBe(false); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should return false for an ISO 8601 date without timezone information', () => { |
| 18 | + const value = '2025-03-31T12:00:00'; |
| 19 | + expect(is(value, ISO8601DateStruct)).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return an error message for invalid ISO 8601 date', () => { |
| 23 | + const value = 'Mon Mar 31 2025'; |
| 24 | + expect(() => create(value, ISO8601DateStruct)).toThrow( |
| 25 | + 'Not a valid ISO 8601 date', |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should return an error message for ISO 8601 date without timezone information', () => { |
| 30 | + const value = '2025-03-31T12:00:00'; |
| 31 | + expect(() => create(value, ISO8601DateStruct)).toThrow( |
| 32 | + 'ISO 8601 date must have timezone information', |
| 33 | + ); |
| 34 | + }); |
| 35 | +}); |
0 commit comments