Skip to content
Closed
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
272 changes: 119 additions & 153 deletions modules/signals/entities/spec/types/entity-config.types.spec.ts
Original file line number Diff line number Diff line change
@@ -1,177 +1,143 @@
import { expecter } from 'ts-snippet';
import { compilerOptions } from './helpers';
import { type } from '@ngrx/signals';
import { entityConfig, type SelectEntityId } from '@ngrx/signals/entities';
import { describe, expect, it } from 'tstyche';

describe('entityConfig', () => {
const expectSnippet = expecter(
(code) => `
import { type } from '@ngrx/signals';
import { entityConfig, SelectEntityId } from '@ngrx/signals/entities';

type User = { key: number; name: string };

${code}
`,
compilerOptions()
);
type User = { key: number; name: string };

describe('entityConfig', () => {
it('fails when empty object is passed', () => {
expectSnippet('entityConfig({})').toFail(
/Property 'entity' is missing in type '{}'/
);
expect(entityConfig).type.not.toBeCallableWith({});
});

it('succeeds when entity is passed', () => {
const snippet = `
const userConfig = entityConfig({ entity: type<User>() });
`;

const result = expectSnippet(snippet);
const userConfig = entityConfig({ entity: type<User>() });

result.toInfer('userConfig', '{ entity: User; }');
expect(userConfig).type.toBe<{ entity: User }>();
});

it('succeeds when entity and collection are passed', () => {
const snippet = `
const userConfig = entityConfig({
entity: type<User>(),
collection: 'user',
});
`;

const result = expectSnippet(snippet);
const userConfig = entityConfig({
entity: type<User>(),
collection: 'user',
});

result.toInfer('userConfig', '{ entity: User; collection: "user"; }');
expect(userConfig).type.toBe<{ entity: User; collection: 'user' }>();
});

it('succeeds when entity and selectId are passed', () => {
const snippet = `
const userConfig1 = entityConfig({
entity: type<User>(),
selectId: (user) => user.key,
});

const selectId2 = (user: User) => user.key;
const userConfig2 = entityConfig({
entity: type<User>(),
selectId: selectId2,
});

const selectId3: SelectEntityId<User> = (user) => user.key;
const userConfig3 = entityConfig({
entity: type<User>(),
selectId: selectId3,
});
`;

const result = expectSnippet(snippet);

result.toInfer(
'userConfig1',
'{ entity: User; selectId: SelectEntityId<NoInfer<User>>; }'
);
result.toInfer(
'userConfig2',
'{ entity: User; selectId: SelectEntityId<NoInfer<User>>; }'
);
result.toInfer(
'userConfig3',
'{ entity: User; selectId: SelectEntityId<NoInfer<User>>; }'
);
const userConfig1 = entityConfig({
entity: type<User>(),
selectId: (user) => user.key,
});

const selectId2 = (user: User) => user.key;
const userConfig2 = entityConfig({
entity: type<User>(),
selectId: selectId2,
});

const selectId3: SelectEntityId<User> = (user) => user.key;
const userConfig3 = entityConfig({
entity: type<User>(),
selectId: selectId3,
});

expect(userConfig1).type.toBe<{
entity: User;
selectId: SelectEntityId<NoInfer<User>>;
}>();
expect(userConfig2).type.toBe<{
entity: User;
selectId: SelectEntityId<NoInfer<User>>;
}>();
expect(userConfig3).type.toBe<{
entity: User;
selectId: SelectEntityId<NoInfer<User>>;
}>();
});

it('fails when entity and wrong selectId are passed', () => {
expectSnippet(`
const userConfig = entityConfig({
entity: type<User>(),
selectId: (user) => user.id,
});
`).toFail(/Property 'id' does not exist on type 'User'/);

expectSnippet(`
const selectId = (entity: { key: number; foo: string }) => entity.key;

const userConfig = entityConfig({
entity: type<User>(),
selectId,
});
`).toFail(/No overload matches this call/);

expectSnippet(`
const selectId: SelectEntityId<{ key: string }> = (entity) => entity.key;

const userConfig = entityConfig({
entity: type<User>(),
selectId,
});
`).toFail(/No overload matches this call/);
entityConfig({
entity: type<User>(),
// @ts-expect-error Property 'id' does not exist on type 'User'
selectId: (user) => user.id,
});

const selectId1 = (entity: { key: number; foo: string }) => entity.key;
entityConfig({
entity: type<User>(),
// @ts-expect-error No overload matches this call
selectId1,
});

const selectId2: SelectEntityId<{ key: string }> = (entity) => entity.key;
entityConfig({
entity: type<User>(),
// @ts-expect-error No overload matches this call
selectId2,
});
});

it('succeeds when entity, collection, and selectId are passed', () => {
const snippet = `
const userConfig1 = entityConfig({
entity: type<User>(),
collection: 'user',
selectId: (user) => user.key,
});

const selectId2 = (user: { key: number }) => user.key;
const userConfig2 = entityConfig({
entity: type<User>(),
collection: 'user',
selectId: selectId2,
});

const selectId3: SelectEntityId<User> = (user) => user.key;
const userConfig3 = entityConfig({
entity: type<User>(),
collection: 'user',
selectId: selectId3,
});
`;

const result = expectSnippet(snippet);

result.toInfer(
'userConfig1',
'{ entity: User; collection: "user"; selectId: SelectEntityId<NoInfer<User>>; }'
);
result.toInfer(
'userConfig2',
'{ entity: User; collection: "user"; selectId: SelectEntityId<NoInfer<User>>; }'
);
result.toInfer(
'userConfig3',
'{ entity: User; collection: "user"; selectId: SelectEntityId<NoInfer<User>>; }'
);
const userConfig1 = entityConfig({
entity: type<User>(),
collection: 'user',
selectId: (user) => user.key,
});

const selectId2 = (user: { key: number }) => user.key;
const userConfig2 = entityConfig({
entity: type<User>(),
collection: 'user',
selectId: selectId2,
});

const selectId3: SelectEntityId<User> = (user) => user.key;
const userConfig3 = entityConfig({
entity: type<User>(),
collection: 'user',
selectId: selectId3,
});

expect(userConfig1).type.toBe<{
entity: User;
collection: 'user';
selectId: SelectEntityId<NoInfer<User>>;
}>();
expect(userConfig2).type.toBe<{
entity: User;
collection: 'user';
selectId: SelectEntityId<NoInfer<User>>;
}>();
expect(userConfig3).type.toBe<{
entity: User;
collection: 'user';
selectId: SelectEntityId<NoInfer<User>>;
}>();
});

it('fails when entity, collection, and wrong selectId are passed', () => {
expectSnippet(`
const userConfig = entityConfig({
entity: type<User>(),
collection: 'user',
selectId: (user) => user.id,
});
`).toFail(/Property 'id' does not exist on type 'User'/);

expectSnippet(`
const selectId = (entity: { key: number; foo: string }) => entity.key;

const userConfig = entityConfig({
entity: type<User>(),
collection: 'user',
selectId,
});
`).toFail(/No overload matches this call/);

expectSnippet(`
const selectId: SelectEntityId<{ key: string }> = (entity) => entity.key;

const userConfig = entityConfig({
entity: type<User>(),
collection: 'user',
selectId,
});
`).toFail(/No overload matches this call/);
entityConfig({
entity: type<User>(),
collection: 'user',
// @ts-expect-error Property 'id' does not exist on type 'User'
selectId: (user) => user.id,
});

const selectId1 = (entity: { key: number; foo: string }) => entity.key;
entityConfig({
entity: type<User>(),
// @ts-expect-error No overload matches this call
collection: 'user',
selectId1,
});

const selectId2: SelectEntityId<{ key: string }> = (entity) => entity.key;
entityConfig({
entity: type<User>(),
// @ts-expect-error No overload matches this call
collection: 'user',
selectId2,
});
});
}, 8_000);
});
13 changes: 0 additions & 13 deletions modules/signals/entities/spec/types/helpers.ts

This file was deleted.

11 changes: 11 additions & 0 deletions modules/signals/entities/spec/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../../../tsconfig.json",
"compilerOptions": {
"exactOptionalPropertyTypes": true,
"noErrorTruncation": true,
"noEmit": true,
"types": []
},
"include": ["**/*"],
"exclude": []
}
Loading
Loading