Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions modules/entity/spec/types/entity_adapter.spec.ts

This file was deleted.

67 changes: 67 additions & 0 deletions modules/entity/spec/types/entity_adapter.types.spec.ts
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<

Copy link
Copy Markdown
Contributor

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.ts file? I think it would be good if we would have some those files which can serve as a template for future tests.

Copy link
Copy Markdown
Member Author

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)

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,
});
});
});
41 changes: 15 additions & 26 deletions modules/entity/spec/types/entity_selectors.types.spec.ts
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);
});
75 changes: 31 additions & 44 deletions modules/entity/spec/types/entity_state.types.spec.ts
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);
});
13 changes: 0 additions & 13 deletions modules/entity/spec/types/utils.ts

This file was deleted.

Loading