Skip to content
Open
Show file tree
Hide file tree
Changes from all 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.

60 changes: 60 additions & 0 deletions modules/entity/spec/types/entity_adapter.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<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>
>();
});
});
15 changes: 15 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,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<EntityWithoutId>({
// @ts-expect-error Type 'boolean' is not assignable to type 'string | number'
selectId: (entity) => entity.key > 0,
});
});
});
20 changes: 20 additions & 0 deletions modules/entity/spec/types/entity_selectors.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Record<string, any>, unknown>
| ((...args: any[]) => Selector<Record<string, any>, unknown>)
>;
type ExtendsSelectorsDictionary<T> = T extends SelectorsDictionary
? true
: false;

expectTypeOf<
ExtendsSelectorsDictionary<EntitySelectors<unknown, Record<string, any>>>
>().toEqualTypeOf<true>();
});
});
31 changes: 0 additions & 31 deletions modules/entity/spec/types/entity_selectors.types.spec.ts

This file was deleted.

42 changes: 42 additions & 0 deletions modules/entity/spec/types/entity_state.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Book> {
selectedBookId: string | null;
}

const adapter: EntityAdapter<Book> = createEntityAdapter<Book>();

describe('EntityState Types', () => {
describe('getInitialState', () => {
it('can set the initial state', () => {
const initialState: BookState = adapter.getInitialState({
selectedBookId: '1',
});

expectTypeOf(initialState).toEqualTypeOf<BookState>();
});

it('can set the initial state with additional properties', () => {
const initialState: BookState = adapter.getInitialState({
selectedBookId: '1',
});

expectTypeOf(initialState).toEqualTypeOf<BookState>();
});

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<EntityState<Book>>();
});
});
});
72 changes: 17 additions & 55 deletions modules/entity/spec/types/entity_state.types.spec.ts
Original file line number Diff line number Diff line change
@@ -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<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',
});

`).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<BookState, keyof EntityState<T>>'/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<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',
});
});
});
}, 8_000);
});
13 changes: 0 additions & 13 deletions modules/entity/spec/types/utils.ts

This file was deleted.

Loading