-
-
Notifications
You must be signed in to change notification settings - Fork 2k
test(entity): refactor to Vitest type tests #5146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3d1d0e4
test(entity): refactor to Vitest type tests
timdeschryver 3f977f1
test: update to vitest
timdeschryver f51f8a2
Merge branch 'main' into vitest/entity
markostanimirovic ac64e1b
test(entity): split type tests into .test-d.ts and hybrid .spec.ts files
timdeschryver ccef81a
Merge branch 'main' into vitest/entity
timdeschryver d5a7ddc
Merge branch 'main' into vitest/entity
timdeschryver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { expectTypeOf, describe, it } from 'vitest'; | ||
| import { createEntityAdapter, EntityAdapter } from '../..'; | ||
|
|
||
| interface EntityWithStringId { | ||
| id: string; | ||
| } | ||
|
|
||
| interface EntityWithNumberId { | ||
| id: number; | ||
| } | ||
|
|
||
| interface EntityWithoutId { | ||
| key: number; | ||
| } | ||
|
|
||
| describe('EntityAdapter Types', () => { | ||
| it('sets the id type to string when the entity has a string id', () => { | ||
| const adapter = createEntityAdapter<EntityWithStringId>(); | ||
|
|
||
| expectTypeOf(adapter).toEqualTypeOf< | ||
| EntityAdapter<EntityWithStringId, string> | ||
| >(); | ||
| }); | ||
|
|
||
| it('sets the id type to number when the entity has a number id', () => { | ||
| const adapter = createEntityAdapter<EntityWithNumberId>(); | ||
|
|
||
| expectTypeOf(adapter).toEqualTypeOf< | ||
| EntityAdapter<EntityWithNumberId, number> | ||
| >(); | ||
| }); | ||
|
|
||
| it('sets the id type to string when selectId returns a string', () => { | ||
| const adapter = createEntityAdapter<EntityWithNumberId>({ | ||
| selectId: (entity) => entity.id.toString(), | ||
| }); | ||
|
|
||
| expectTypeOf(adapter).toEqualTypeOf< | ||
| EntityAdapter<EntityWithNumberId, string> | ||
| >(); | ||
| }); | ||
|
|
||
| it('sets the id type to string | number when the entity has no id and no selectId is provided', () => { | ||
| const adapter = createEntityAdapter<EntityWithoutId>(); | ||
|
|
||
| expectTypeOf(adapter).toEqualTypeOf< | ||
| EntityAdapter<EntityWithoutId, string | number> | ||
| >(); | ||
| }); | ||
|
|
||
| it('sets the id type to correct type if selectId is provided', () => { | ||
| const adapter = createEntityAdapter<EntityWithoutId>({ | ||
| selectId: (entity) => entity.key.toString(), | ||
| }); | ||
|
|
||
| expectTypeOf(adapter).toEqualTypeOf< | ||
| EntityAdapter<EntityWithoutId, string> | ||
| >(); | ||
| }); | ||
|
|
||
| it('throws when selectId returns an invalid id type', () => { | ||
| createEntityAdapter<EntityWithoutId>({ | ||
| // @ts-expect-error Type 'boolean' is not assignable to type 'string | number' | ||
| selectId: (entity) => entity.key > 0, | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,20 @@ | ||
| import { expecter } from 'ts-snippet'; | ||
| import { compilerOptions } from './utils'; | ||
| import { Selector } from '@ngrx/store'; | ||
| import { expectTypeOf, describe, it } from 'vitest'; | ||
| import { EntitySelectors } from '../..'; | ||
|
|
||
| describe('EntitySelectors', () => { | ||
| const expectSnippet = expecter( | ||
| (code) => ` | ||
| import { Selector } from '@ngrx/store'; | ||
| import { EntitySelectors } from './modules/entity/src/models'; | ||
|
|
||
| ${code} | ||
| `, | ||
| compilerOptions() | ||
| ); | ||
|
|
||
| it('is compatible with a dictionary of selectors', () => { | ||
| expectSnippet(` | ||
| type SelectorsDictionary = Record< | ||
| string, | ||
| | Selector<Record<string, any>, unknown> | ||
| | ((...args: any[]) => Selector<Record<string, any>, unknown>) | ||
| >; | ||
| type ExtendsSelectorsDictionary<T> = T extends SelectorsDictionary | ||
| ? true | ||
| : false; | ||
| type SelectorsDictionary = Record< | ||
| string, | ||
| | Selector<Record<string, any>, unknown> | ||
| | ((...args: any[]) => Selector<Record<string, any>, unknown>) | ||
| >; | ||
| type ExtendsSelectorsDictionary<T> = T extends SelectorsDictionary | ||
| ? true | ||
| : false; | ||
|
|
||
| let result: ExtendsSelectorsDictionary< | ||
| EntitySelectors<unknown, Record<string, any>> | ||
| >; | ||
| `).toInfer('result', 'true'); | ||
| expectTypeOf< | ||
| ExtendsSelectorsDictionary<EntitySelectors<unknown, Record<string, any>>> | ||
| >().toEqualTypeOf<true>(); | ||
| }); | ||
| }, 8_000); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,63 +1,50 @@ | ||
| import { expecter } from 'ts-snippet'; | ||
| import { compilerOptions } from './utils'; | ||
| import { expectTypeOf, describe, it } from 'vitest'; | ||
| import { createEntityAdapter, EntityAdapter, EntityState } from '../..'; | ||
|
|
||
| describe('EntityState Types', () => { | ||
| const expectSnippet = expecter( | ||
| (code) => ` | ||
| import { EntityState, createEntityAdapter, EntityAdapter } from '@ngrx/entity'; | ||
|
|
||
| interface Book { | ||
| id: string; | ||
| title: string; | ||
| } | ||
| interface Book { | ||
| id: string; | ||
| title: string; | ||
| } | ||
|
|
||
| interface BookState extends EntityState<Book> { | ||
| selectedBookId: string | null; | ||
| } | ||
| interface BookState extends EntityState<Book> { | ||
| selectedBookId: string | null; | ||
| } | ||
|
|
||
| export const adapter: EntityAdapter<Book> = createEntityAdapter<Book>(); | ||
| ${code} | ||
| `, | ||
| compilerOptions() | ||
| ); | ||
| const adapter: EntityAdapter<Book> = createEntityAdapter<Book>(); | ||
|
|
||
| describe('EntityState Types', () => { | ||
| describe('getInitialState', () => { | ||
| it('can set the initial state', () => { | ||
| expectSnippet(` | ||
| export const initialState: BookState = adapter.getInitialState({ | ||
| selectedBookId: '1', | ||
| }); | ||
| const initialState: BookState = adapter.getInitialState({ | ||
| selectedBookId: '1', | ||
| }); | ||
|
|
||
| `).toSucceed(); | ||
| expectTypeOf(initialState).toEqualTypeOf<BookState>(); | ||
| }); | ||
|
|
||
| it('can set the initial state with additional properties', () => { | ||
| expectSnippet(` | ||
| export const initialState: BookState = adapter.getInitialState({ | ||
| selectedBookId: '1', | ||
| }); | ||
| const initialState: BookState = adapter.getInitialState({ | ||
| selectedBookId: '1', | ||
| }); | ||
|
|
||
| `).toSucceed(); | ||
| expectTypeOf(initialState).toEqualTypeOf<BookState>(); | ||
| }); | ||
|
|
||
| it('throws when setting the initial state with unknown properties', () => { | ||
| expectSnippet(` | ||
| export const initialState: BookState = adapter.getInitialState({ | ||
| selectedBookId: '1', | ||
| otherProperty: 'value', | ||
| }); | ||
| `).toFail( | ||
| /Object literal may only specify known properties, and 'otherProperty' does not exist in type 'Omit<BookState, keyof EntityState<T>>'/i | ||
| ); | ||
| adapter.getInitialState<BookState>({ | ||
| selectedBookId: '1', | ||
| // @ts-expect-error Object literal may only specify known properties, and 'otherProperty' does not exist in type 'Omit<BookState, keyof EntityState<T>>' | ||
| otherProperty: 'value', | ||
| }); | ||
| }); | ||
|
|
||
| it('can set the initial state with unknown properties when the state is untyped', () => { | ||
| expectSnippet(` | ||
| export const initialState = adapter.getInitialState({ | ||
| selectedBookId: '1', | ||
| otherProperty: 'value', | ||
| }); | ||
| `).toSucceed(); | ||
| const initialState = adapter.getInitialState({ | ||
| selectedBookId: '1', | ||
| otherProperty: 'value', | ||
| }); | ||
|
|
||
| expectTypeOf(initialState).toExtend<EntityState<Book>>(); | ||
| }); | ||
| }); | ||
| }, 8_000); | ||
| }); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tim would it make sense to move the type-only tests into a
test-d.tsfile? I think it would be good if we would have some those files which can serve as a template for future tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right. I resolved the merge conflicts, but didn't pay attention to it.
This will also help for cases as mentioned in #5127 (comment)