|
| 1 | +import type { IEnum, IEnumItems } from '@enum-plus'; |
| 2 | +import type { localeEN, StandardWeekConfig } from '../data/week-config'; |
| 3 | +import type TestEngineBase from '../engines/base'; |
| 4 | + |
| 5 | +const testTyping = (engine: TestEngineBase) => { |
| 6 | + engine.describe('Enum typings', () => { |
| 7 | + engine.test( |
| 8 | + 'the test codes should not report any TypeScript errors, even including @ts-expect-error comments', |
| 9 | + ({ EnumPlus: { Enum, defaultLocalize }, WeekConfig: { StandardWeekConfig, setLang, getLocales, localeEN } }) => { |
| 10 | + setLang('en-US', Enum, getLocales, defaultLocalize); |
| 11 | + const WeekConfig = StandardWeekConfig; |
| 12 | + const weekEnum = Enum(StandardWeekConfig); |
| 13 | + return { weekEnum, Enum, WeekConfig, localeEN }; |
| 14 | + }, |
| 15 | + ({ weekEnum, Enum, WeekConfig, localeEN }) => { |
| 16 | + const value = weekEnum as typeof weekEnum | number | string | undefined; |
| 17 | + if (Enum.isEnum(value)) { |
| 18 | + engine.expect(value).toBe(weekEnum); |
| 19 | + } else { |
| 20 | + // @ts-expect-error: because val is narrowed off Enum type, type casting will raise error |
| 21 | + console.log(value as typeof weekEnum); |
| 22 | + } |
| 23 | + |
| 24 | + validateEnum(engine, weekEnum, localeEN, WeekConfig); |
| 25 | + validateEnum(engine, weekEnum.items, localeEN, WeekConfig); |
| 26 | + } |
| 27 | + ); |
| 28 | + }); |
| 29 | +}; |
| 30 | + |
| 31 | +export default testTyping; |
| 32 | + |
| 33 | +/* |
| 34 | +!---------------------------------------------------------------------- |
| 35 | + The below function should not contain any TypeScript errors, even the |
| 36 | + @ts-expect-error comments should not report any errors. |
| 37 | + If you see any TypeScript errors, it means the typing is incorrect! |
| 38 | +!---------------------------------------------------------------------- |
| 39 | +*/ |
| 40 | +function validateEnum< |
| 41 | + T extends |
| 42 | + | IEnum< |
| 43 | + typeof StandardWeekConfig, |
| 44 | + keyof typeof StandardWeekConfig, |
| 45 | + (typeof StandardWeekConfig)[keyof typeof StandardWeekConfig]['value'] |
| 46 | + > |
| 47 | + | IEnumItems< |
| 48 | + typeof StandardWeekConfig, |
| 49 | + keyof typeof StandardWeekConfig, |
| 50 | + (typeof StandardWeekConfig)[keyof typeof StandardWeekConfig]['value'] |
| 51 | + >, |
| 52 | +>(engine: TestEngineBase, weekEnum: T, locale: typeof localeEN, WeekConfig: typeof StandardWeekConfig) { |
| 53 | + const value2 = 1 as 1 | { foo: number }; |
| 54 | + const value3 = 'Monday' as 'Monday' | { foo: number }; |
| 55 | + if (value2 instanceof weekEnum) { |
| 56 | + engine.expect(value2.toFixed(1)).toBe('1.0'); |
| 57 | + } else { |
| 58 | + console.log(value2 as { foo: number }); |
| 59 | + } |
| 60 | + if (value3 instanceof weekEnum) { |
| 61 | + engine.expect(value3.trim()).toBe('Monday'); |
| 62 | + } else { |
| 63 | + console.log(value3 as { foo: number }); |
| 64 | + } |
| 65 | + |
| 66 | + engine.expect(weekEnum.key(1).trim()).toBe('Monday'); |
| 67 | + // @ts-expect-error: because key returns nullable value, should use optional chaining (?.) operator |
| 68 | + engine.expect(weekEnum.key(1 as number).trim()).toBe('Monday'); |
| 69 | + // @ts-expect-error: because key returns nullable value, should use optional chaining (?.) operator |
| 70 | + engine.expect(() => weekEnum.key(8 as number).trim()).toThrow(); |
| 71 | + // @ts-expect-error: because out-of-range literal values return undefined only, cannot be cased to number |
| 72 | + engine.expect(weekEnum.key(8) as number).toBe(undefined); |
| 73 | + |
| 74 | + engine.expect(weekEnum.label(1)?.trim()).toBe(locale.Monday); |
| 75 | + // @ts-expect-error: because label returns nullable value, should use optional chaining (?.) operator |
| 76 | + engine.expect(weekEnum.label(1 as number).trim()).toBe(locale.Monday); |
| 77 | + // @ts-expect-error: because label returns nullable value, should use optional chaining (?.) operator |
| 78 | + engine.expect(() => weekEnum.label(8 as number).trim()).toThrow(); |
| 79 | + // @ts-expect-error: because out-of-range literal values return undefined only, cannot be cased to string |
| 80 | + engine.expect(weekEnum.label(8) as string).toBe(undefined); |
| 81 | + |
| 82 | + engine.expect(weekEnum.raw('Monday')).toBe(WeekConfig.Monday); |
| 83 | + // @ts-expect-error: because raw returns nullable object, should use optional chaining (?.) operator |
| 84 | + engine.expect(weekEnum.raw('Monday' as string).value).toBe(1); |
| 85 | + // @ts-expect-error: because raw returns nullable object, should use optional chaining (?.) operator |
| 86 | + engine.expect(() => weekEnum.raw('January' as string).value).toThrow(); |
| 87 | + // @ts-expect-error: because out-of-range literal values return undefined only, cannot be cased to object |
| 88 | + engine.expect(weekEnum.raw('January') as { value: number }).toBe(undefined); |
| 89 | + |
| 90 | + engine.expect(weekEnum.findBy('key', 'Monday').key).toBe('Monday'); |
| 91 | + // @ts-expect-error: because findBy returns nullable object, should use optional chaining (?.) operator |
| 92 | + engine.expect(weekEnum.findBy('key', 'Monday' as string).key).toBe('Monday'); |
| 93 | + // @ts-expect-error: because findBy returns nullable object, should use optional chaining (?.) operator |
| 94 | + engine.expect(() => weekEnum.findBy('key', 'January' as string).key).toThrow(); |
| 95 | + engine.expect(weekEnum.findBy('value', 1).key).toBe('Monday'); |
| 96 | + // @ts-expect-error: because findBy returns nullable object, should use optional chaining (?.) operator |
| 97 | + engine.expect(weekEnum.findBy('value', 1 as number).key).toBe('Monday'); |
| 98 | + // @ts-expect-error: because findBy returns nullable object, should use optional chaining (?.) operator |
| 99 | + engine.expect(() => weekEnum.findBy('value', 8 as number).key).toThrow(); |
| 100 | + // @ts-expect-error: because findBy label always return nullable string |
| 101 | + engine.expect(weekEnum.findBy('label', 'Monday').key).toBe('Monday'); |
| 102 | + // @ts-expect-error: because findBy label always return nullable string |
| 103 | + engine.expect(() => weekEnum.findBy('label', 'January' as string).key).toThrow(); |
| 104 | + engine.expect(weekEnum.findBy('status', 'warning').key).toBe('Monday'); |
| 105 | + // @ts-expect-error: because findBy findBy returns nullable object, should use optional chaining (?.) operator |
| 106 | + engine.expect(weekEnum.findBy('status', 'warning' as string).key).toBe('Monday'); |
| 107 | + // @ts-expect-error: because findBy returns nullable object, should use optional chaining (?.) operator |
| 108 | + engine.expect(() => weekEnum.findBy('status', 'foo' as string).key).toThrow(); |
| 109 | +} |
0 commit comments