Skip to content

Commit

Permalink
test(types): [Entries, Values] add union tests
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Jul 4, 2023
1 parent bc2a3d8 commit f884e81
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
30 changes: 27 additions & 3 deletions src/types/__tests__/entries.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type EmptyArray from '../empty-array'
import type EmptyObject from '../empty-object'
import type TestSubject from '../entries'
import type Fn from '../fn'
import type Nilable from '../nilable'
import type Nullable from '../nullable'
import type Numeric from '../numeric'

Expand Down Expand Up @@ -46,9 +47,11 @@ describe('unit-d:types/Entries', () => {
>()
})

it('should equal EmptyArray if EmptyObject extends T', () => {
expectTypeOf<TestSubject<{}>>().toEqualTypeOf<EmptyArray>()
expectTypeOf<TestSubject<EmptyObject>>().toEqualTypeOf<EmptyArray>()
describe('EmptyObject extends T', () => {
it('should equal EmptyArray if EmptyObject extends T', () => {
expectTypeOf<TestSubject<{}>>().toEqualTypeOf<EmptyArray>()
expectTypeOf<TestSubject<EmptyObject>>().toEqualTypeOf<EmptyArray>()
})
})
})

Expand Down Expand Up @@ -92,4 +95,25 @@ describe('unit-d:types/Entries', () => {
})
})
})

describe('unions', () => {
it('should distribute over unions', () => {
expectTypeOf<TestSubject<Nilable<Book | Vehicle>>>().toEqualTypeOf<
| (
| ['authors', Book['authors']]
| ['isbn', Book['isbn']]
| ['publisher', Book['publisher']]
| ['readers', Book['readers']]
| ['title', Book['title']]
)[]
| (
| ['make', Vehicle['make']]
| ['model', Vehicle['model']]
| ['vin', Vehicle['vin']]
| ['year', Vehicle['year']]
)[]
| []
>()
})
})
})
21 changes: 18 additions & 3 deletions src/types/__tests__/values.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
*/

import type Book from '#fixtures/book.interface'
import type Publisher from '#fixtures/publisher.interface'
import type Vehicle from '#fixtures/vehicle'
import type EmptyArray from '../empty-array'
import type EmptyObject from '../empty-object'
import type Fn from '../fn'
import type Nilable from '../nilable'
import type Nullable from '../nullable'
import type NumberString from '../number-string'
import type TestSubject from '../values'
Expand Down Expand Up @@ -38,9 +40,11 @@ describe('unit-d:types/Values', () => {
expectTypeOf<TestSubject<T>>().toEqualTypeOf<T[keyof T][]>()
})

it('should equal EmptyArray if EmptyObject extends T', () => {
expectTypeOf<TestSubject<{}>>().toEqualTypeOf<EmptyArray>()
expectTypeOf<TestSubject<EmptyObject>>().toEqualTypeOf<EmptyArray>()
describe('EmptyObject extends T', () => {
it('should equal EmptyArray if EmptyObject extends T', () => {
expectTypeOf<TestSubject<{}>>().toEqualTypeOf<EmptyArray>()
expectTypeOf<TestSubject<EmptyObject>>().toEqualTypeOf<EmptyArray>()
})
})
})

Expand Down Expand Up @@ -82,4 +86,15 @@ describe('unit-d:types/Values', () => {
})
})
})

describe('unions', () => {
it('should distribute over unions', () => {
// Arrange
type T = Nilable<Book | Publisher>
type Expect = Book[keyof Book][] | Publisher[keyof Publisher][] | []

// Expect
expectTypeOf<TestSubject<T>>().toEqualTypeOf<Expect>()
})
})
})

0 comments on commit f884e81

Please sign in to comment.