diff --git a/modules/entity/spec/types/entity_adapter.spec.ts b/modules/entity/spec/types/entity_adapter.spec.ts deleted file mode 100644 index 317b86c99b..0000000000 --- a/modules/entity/spec/types/entity_adapter.spec.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { expecter } from 'ts-snippet'; -import { compilerOptions } from './utils'; - -describe('EntityAdapter Types', () => { - const expectSnippet = expecter( - (code) => ` - import { EntityState, createEntityAdapter, EntityAdapter } from '@ngrx/entity'; - - interface EntityWithStringId { - id: string; - } - - interface EntityWithNumberId { - id: number; - } - - interface EntityWithoutId { - key: number; - } - - - ${code} - `, - compilerOptions() - ); - - it('sets the id type to string when the entity has a string id', () => { - expectSnippet(` - export const adapter: EntityAdapter = createEntityAdapter(); - `).toSucceed(); - }); - - it('sets the id type to number when the entity has a number id', () => { - expectSnippet(` - export const adapter: EntityAdapter = createEntityAdapter(); - `).toSucceed(); - }); - - it('sets the id type to string when selectId returns a string', () => { - expectSnippet(` - export const adapter: EntityAdapter = createEntityAdapter({ - selectId: (entity) => entity.id.toString(), - }); - `).toSucceed(); - }); - - it('sets the id type to string | number when the entity has no id and no selectId is provided', () => { - expectSnippet(` - export const adapter: EntityAdapter = createEntityAdapter(); - `).toSucceed(); - }); - - it('sets the id type to correct type if selectId is provided', () => { - expectSnippet(` - export const adapter: EntityAdapter = createEntityAdapter({ - selectId: (entity) => entity.key.toString(), - }); - `).toSucceed(); - }); - - it('sets the id type to string when selectId returns a string', () => { - expectSnippet(` - export const adapter: EntityAdapter = createEntityAdapter({ - selectId: (entity) => entity.id.toString(), - }); - `).toSucceed(); - }); -}, 8_000); diff --git a/modules/entity/spec/types/entity_adapter.test-d.ts b/modules/entity/spec/types/entity_adapter.test-d.ts new file mode 100644 index 0000000000..0d6a51fc05 --- /dev/null +++ b/modules/entity/spec/types/entity_adapter.test-d.ts @@ -0,0 +1,60 @@ +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(); + + expectTypeOf(adapter).toEqualTypeOf< + EntityAdapter + >(); + }); + + it('sets the id type to number when the entity has a number id', () => { + const adapter = createEntityAdapter(); + + expectTypeOf(adapter).toEqualTypeOf< + EntityAdapter + >(); + }); + + it('sets the id type to string when selectId returns a string', () => { + const adapter = createEntityAdapter({ + selectId: (entity) => entity.id.toString(), + }); + + expectTypeOf(adapter).toEqualTypeOf< + EntityAdapter + >(); + }); + + it('sets the id type to string | number when the entity has no id and no selectId is provided', () => { + const adapter = createEntityAdapter(); + + expectTypeOf(adapter).toEqualTypeOf< + EntityAdapter + >(); + }); + + it('sets the id type to correct type if selectId is provided', () => { + const adapter = createEntityAdapter({ + selectId: (entity) => entity.key.toString(), + }); + + expectTypeOf(adapter).toEqualTypeOf< + EntityAdapter + >(); + }); +}); diff --git a/modules/entity/spec/types/entity_adapter.types.spec.ts b/modules/entity/spec/types/entity_adapter.types.spec.ts new file mode 100644 index 0000000000..c7ccb70543 --- /dev/null +++ b/modules/entity/spec/types/entity_adapter.types.spec.ts @@ -0,0 +1,15 @@ +import { it, describe } from 'vitest'; +import { createEntityAdapter } from '../..'; + +interface EntityWithoutId { + key: number; +} + +describe('EntityAdapter Types', () => { + it('throws when selectId returns an invalid id type', () => { + createEntityAdapter({ + // @ts-expect-error Type 'boolean' is not assignable to type 'string | number' + selectId: (entity) => entity.key > 0, + }); + }); +}); diff --git a/modules/entity/spec/types/entity_selectors.test-d.ts b/modules/entity/spec/types/entity_selectors.test-d.ts new file mode 100644 index 0000000000..0f1bb02082 --- /dev/null +++ b/modules/entity/spec/types/entity_selectors.test-d.ts @@ -0,0 +1,20 @@ +import { Selector } from '@ngrx/store'; +import { expectTypeOf, describe, it } from 'vitest'; +import { EntitySelectors } from '../..'; + +describe('EntitySelectors', () => { + it('is compatible with a dictionary of selectors', () => { + type SelectorsDictionary = Record< + string, + | Selector, unknown> + | ((...args: any[]) => Selector, unknown>) + >; + type ExtendsSelectorsDictionary = T extends SelectorsDictionary + ? true + : false; + + expectTypeOf< + ExtendsSelectorsDictionary>> + >().toEqualTypeOf(); + }); +}); diff --git a/modules/entity/spec/types/entity_selectors.types.spec.ts b/modules/entity/spec/types/entity_selectors.types.spec.ts deleted file mode 100644 index 166f37d0e8..0000000000 --- a/modules/entity/spec/types/entity_selectors.types.spec.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { expecter } from 'ts-snippet'; -import { compilerOptions } from './utils'; - -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, unknown> - | ((...args: any[]) => Selector, unknown>) - >; - type ExtendsSelectorsDictionary = T extends SelectorsDictionary - ? true - : false; - - let result: ExtendsSelectorsDictionary< - EntitySelectors> - >; - `).toInfer('result', 'true'); - }); -}, 8_000); diff --git a/modules/entity/spec/types/entity_state.test-d.ts b/modules/entity/spec/types/entity_state.test-d.ts new file mode 100644 index 0000000000..08dd13a3d9 --- /dev/null +++ b/modules/entity/spec/types/entity_state.test-d.ts @@ -0,0 +1,42 @@ +import { expectTypeOf, describe, it } from 'vitest'; +import { createEntityAdapter, EntityAdapter, EntityState } from '../..'; + +interface Book { + id: string; + title: string; +} + +interface BookState extends EntityState { + selectedBookId: string | null; +} + +const adapter: EntityAdapter = createEntityAdapter(); + +describe('EntityState Types', () => { + describe('getInitialState', () => { + it('can set the initial state', () => { + const initialState: BookState = adapter.getInitialState({ + selectedBookId: '1', + }); + + expectTypeOf(initialState).toEqualTypeOf(); + }); + + it('can set the initial state with additional properties', () => { + const initialState: BookState = adapter.getInitialState({ + selectedBookId: '1', + }); + + expectTypeOf(initialState).toEqualTypeOf(); + }); + + it('can set the initial state with unknown properties when the state is untyped', () => { + const initialState = adapter.getInitialState({ + selectedBookId: '1', + otherProperty: 'value', + }); + + expectTypeOf(initialState).toExtend>(); + }); + }); +}); diff --git a/modules/entity/spec/types/entity_state.types.spec.ts b/modules/entity/spec/types/entity_state.types.spec.ts index 24d831bbf5..7ec8e016b9 100644 --- a/modules/entity/spec/types/entity_state.types.spec.ts +++ b/modules/entity/spec/types/entity_state.types.spec.ts @@ -1,63 +1,25 @@ -import { expecter } from 'ts-snippet'; -import { compilerOptions } from './utils'; +import { it, describe } 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 { - selectedBookId: string | null; - } +interface BookState extends EntityState { + selectedBookId: string | null; +} - export const adapter: EntityAdapter = createEntityAdapter(); - ${code} - `, - compilerOptions() - ); +const adapter: EntityAdapter = createEntityAdapter(); +describe('EntityState Types', () => { describe('getInitialState', () => { - it('can set the initial state', () => { - expectSnippet(` - export const initialState: BookState = adapter.getInitialState({ - selectedBookId: '1', - }); - - `).toSucceed(); - }); - - it('can set the initial state with additional properties', () => { - expectSnippet(` - export const initialState: BookState = adapter.getInitialState({ - selectedBookId: '1', - }); - - `).toSucceed(); - }); - 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>'/i - ); - }); - - 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(); + adapter.getInitialState({ + selectedBookId: '1', + // @ts-expect-error Object literal may only specify known properties, and 'otherProperty' does not exist in type 'Omit>' + otherProperty: 'value', + }); }); }); -}, 8_000); +}); diff --git a/modules/entity/spec/types/utils.ts b/modules/entity/spec/types/utils.ts deleted file mode 100644 index a1ddc21fa9..0000000000 --- a/modules/entity/spec/types/utils.ts +++ /dev/null @@ -1,13 +0,0 @@ -export const compilerOptions = () => ({ - module: 'preserve', - moduleResolution: 'bundler', - target: 'ES2022', - baseUrl: '.', - ignoreDeprecations: '6.0', - experimentalDecorators: true, - strict: true, - paths: { - '@ngrx/entity': ['./modules/entity'], - '@ngrx/store': ['./modules/store'], - }, -});