|
| 1 | +import { Directive, input, model, signal } from '@angular/core' |
| 2 | +import { describe, expectTypeOf, it } from 'vitest' |
| 3 | + |
| 4 | +import { InputsOf } from './inputs-of.type' |
| 5 | + |
| 6 | +describe('InputsOf', () => { |
| 7 | + it('extracts InputSignal properties with their unwrapped types', () => { |
| 8 | + @Directive() |
| 9 | + class TestClazz { |
| 10 | + readonly name = input.required<string>() |
| 11 | + readonly age = input<number>(0) |
| 12 | + readonly isActive = signal(true) |
| 13 | + } |
| 14 | + |
| 15 | + type Result = InputsOf<TestClazz> |
| 16 | + |
| 17 | + expectTypeOf<Result>().toEqualTypeOf<{ |
| 18 | + readonly name: string |
| 19 | + readonly age: number |
| 20 | + }>() |
| 21 | + }) |
| 22 | + |
| 23 | + it('extracts ModelSignal properties with their unwrapped types', () => { |
| 24 | + @Directive() |
| 25 | + class TestClazz { |
| 26 | + readonly count = model(0) |
| 27 | + readonly items = model<string[]>([]) |
| 28 | + readonly isVisible = signal(false) |
| 29 | + } |
| 30 | + |
| 31 | + type Result = InputsOf<TestClazz> |
| 32 | + |
| 33 | + expectTypeOf<Result>().toEqualTypeOf<{ |
| 34 | + readonly count: number |
| 35 | + readonly items: string[] |
| 36 | + }>() |
| 37 | + }) |
| 38 | + |
| 39 | + it('excludes regular signals and non-signal properties', () => { |
| 40 | + @Directive() |
| 41 | + class TestClazz { |
| 42 | + readonly name = input<string>('default') |
| 43 | + readonly regularSignal = signal(true) |
| 44 | + readonly plainProperty = 'test' |
| 45 | + readonly method = () => {} |
| 46 | + } |
| 47 | + |
| 48 | + type Result = InputsOf<TestClazz> |
| 49 | + |
| 50 | + expectTypeOf<Result>().toEqualTypeOf<{ |
| 51 | + readonly name: string |
| 52 | + }>() |
| 53 | + }) |
| 54 | + |
| 55 | + it('handles mixed InputSignal and ModelSignal', () => { |
| 56 | + @Directive() |
| 57 | + class TestClazz { |
| 58 | + readonly title = input.required<string>() |
| 59 | + readonly description = input<string>('default') |
| 60 | + readonly status = model<'active' | 'inactive'>('active') |
| 61 | + readonly count = model(0) |
| 62 | + readonly timestamp = signal(Date.now()) |
| 63 | + readonly regular = 'prop' |
| 64 | + } |
| 65 | + |
| 66 | + type Result = InputsOf<TestClazz> |
| 67 | + |
| 68 | + expectTypeOf<Result>().toEqualTypeOf<{ |
| 69 | + readonly title: string |
| 70 | + readonly description: string |
| 71 | + readonly status: 'active' | 'inactive' |
| 72 | + readonly count: number |
| 73 | + }>() |
| 74 | + }) |
| 75 | + |
| 76 | + it('handles complex types in signals', () => { |
| 77 | + interface User { |
| 78 | + id: string |
| 79 | + name: string |
| 80 | + email: string |
| 81 | + } |
| 82 | + |
| 83 | + @Directive() |
| 84 | + class TestClazz { |
| 85 | + readonly user = input.required<User>() |
| 86 | + readonly roles = input<string[]>([]) |
| 87 | + readonly metadata = model<Record<string, unknown>>({}) |
| 88 | + } |
| 89 | + |
| 90 | + type Result = InputsOf<TestClazz> |
| 91 | + |
| 92 | + expectTypeOf<Result>().toEqualTypeOf<{ |
| 93 | + readonly user: User |
| 94 | + readonly roles: string[] |
| 95 | + readonly metadata: Record<string, unknown> |
| 96 | + }>() |
| 97 | + }) |
| 98 | + |
| 99 | + it('handles empty component (no inputs or models)', () => { |
| 100 | + @Directive() |
| 101 | + class EmptyComponent { |
| 102 | + readonly counter = signal(0) |
| 103 | + readonly value = 42 |
| 104 | + } |
| 105 | + |
| 106 | + type Result = InputsOf<EmptyComponent> |
| 107 | + |
| 108 | + // eslint-disable-next-line @typescript-eslint/no-empty-object-type |
| 109 | + expectTypeOf<Result>().toEqualTypeOf<{}>() |
| 110 | + }) |
| 111 | + |
| 112 | + it('handles generic types', () => { |
| 113 | + @Directive() |
| 114 | + class TestClazz<T> { |
| 115 | + readonly items = input.required<T[]>() |
| 116 | + readonly selectedItem = model<T | null>(null) |
| 117 | + } |
| 118 | + |
| 119 | + type StringArrayResult = InputsOf<TestClazz<string>> |
| 120 | + |
| 121 | + expectTypeOf<StringArrayResult>().toEqualTypeOf<{ |
| 122 | + readonly items: string[] |
| 123 | + readonly selectedItem: string | null |
| 124 | + }>() |
| 125 | + }) |
| 126 | + |
| 127 | + it('handles optional input signals without default value as potentially undefined', () => { |
| 128 | + @Directive() |
| 129 | + class TestClazz { |
| 130 | + readonly required = input.required<string>() |
| 131 | + readonly optional = input<string>() |
| 132 | + } |
| 133 | + |
| 134 | + type Result = InputsOf<TestClazz> |
| 135 | + |
| 136 | + expectTypeOf<Result>().toEqualTypeOf<{ |
| 137 | + readonly required: string |
| 138 | + readonly optional: string | undefined |
| 139 | + }>() |
| 140 | + }) |
| 141 | + |
| 142 | + it('handles nullable types in signals', () => { |
| 143 | + @Directive() |
| 144 | + class TestClazz { |
| 145 | + readonly user = input<{ name: string } | null>(null) |
| 146 | + readonly items = model<string[] | null>(null) |
| 147 | + } |
| 148 | + |
| 149 | + type Result = InputsOf<TestClazz> |
| 150 | + |
| 151 | + expectTypeOf<Result>().toEqualTypeOf<{ |
| 152 | + readonly user: { name: string } | null |
| 153 | + readonly items: string[] | null |
| 154 | + }>() |
| 155 | + }) |
| 156 | +}) |
0 commit comments