|
| 1 | +import { create, is } from '@metamask/superstruct'; |
| 2 | +import { DateTime } from 'luxon'; |
| 3 | + |
| 4 | +import { ISO8601DateStruct, ISO8601DurationStruct } 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 | +}); |
| 36 | + |
| 37 | +describe('ISO8601DurationStruct', () => { |
| 38 | + it('should return true for a valid ISO 8601 duration', () => { |
| 39 | + const value = 'P3Y6M4DT12H30M5S'; |
| 40 | + expect(is(value, ISO8601DurationStruct)).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return false for an invalid ISO 8601 duration', () => { |
| 44 | + const value = 'Millisecond'; |
| 45 | + expect(is(value, ISO8601DurationStruct)).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return an error message for invalid ISO 8601 duration', () => { |
| 49 | + const value = '1Millisecond'; |
| 50 | + expect(() => create(value, ISO8601DurationStruct)).toThrow( |
| 51 | + 'Not a valid ISO 8601 duration', |
| 52 | + ); |
| 53 | + }); |
| 54 | +}); |
0 commit comments