Skip to content
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

feat(signals): add migration for withProps #4612

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 1 addition & 13 deletions modules/signals/migrations/18_0_0-rc_3-protected-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import * as ts from 'typescript';
import { commitChanges } from '../../schematics-core';
import { visitImportDeclaration } from '../../schematics-core/utility/visitors';

export default function migrateWritableStateSource(): Rule {
return (tree: Tree, ctx: SchematicContext) => {
Expand Down Expand Up @@ -89,19 +90,6 @@ function visitCallExpression(
});
}

function visitImportDeclaration(
node: ts.Node,
callback: (importDeclaration: ts.ImportDeclaration) => void
) {
if (ts.isImportDeclaration(node)) {
callback(node);
}

ts.forEachChild(node, (child) => {
visitImportDeclaration(child, callback);
});
}

function findImportedName(source: ts.SourceFile) {
let importedName = '';
visitImportDeclaration(source, (importDeclaration) => {
Expand Down
259 changes: 259 additions & 0 deletions modules/signals/migrations/19_0_0-rc_1-props/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
import * as path from 'path';
import {
SchematicTestRunner,
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import { createWorkspace } from '@ngrx/schematics-core/testing';
import { tags } from '@angular-devkit/core';
import { LogEntry } from '@angular-devkit/core/src/logger';

describe('migrate to props', () => {
const collectionPath = path.join(__dirname, '../migration.json');
const schematicRunner = new SchematicTestRunner('schematics', collectionPath);

let appTree: UnitTestTree;

beforeEach(async () => {
appTree = await createWorkspace(schematicRunner, appTree);
});

const verifySchematic = async (input: string, output: string) => {
appTree.create('main.ts', input);

const logEntries: string[] = [];
schematicRunner.logger.subscribe((logEntry) =>
logEntries.push(logEntry.message)
);

const tree = await schematicRunner.runSchematic(
`19_0_0-rc_1-props`,
{},
appTree
);

const actual = tree.readContent('main.ts');

expect(actual).toBe(output);

return logEntries;
};

it('renames (Named)EntityComputed imports to (Named)EntityProps', async () => {
const input = tags.stripIndent`
import { EntityComputed, NamedEntityComputed } from '@ngrx/signals/entities';
`;

const output = tags.stripIndent`
import { EntityProps, NamedEntityProps } from '@ngrx/signals/entities';
`;

const logEntries = await verifySchematic(input, output);
expect(logEntries).toEqual([
"[@ngrx/signals] Renamed '(Named)EntityComputed' to '(Named)EntityProps' in /main.ts",
]);
});

it('replaces property `computed` in `SignalStoreFeature` to `props`', async () => {
const input = tags.stripIndent`
import { signalStoreFeature, type } from '@ngrx/signals';
import { Signal } from '@angular/core';

export function withMyFeature() {
return signalStoreFeature({ computed: type<{ num: Signal<number> }>() });
}
`;

const output = tags.stripIndent`
import { signalStoreFeature, type } from '@ngrx/signals';
import { Signal } from '@angular/core';

export function withMyFeature() {
return signalStoreFeature({ props: type<{ num: Signal<number> }>() });
}
`;

const logEntries = await verifySchematic(input, output);
expect(logEntries).toEqual([
"[@ngrx/signals] Renamed 'computed' to 'props' in signalStoreFeature() in /main.ts",
]);
});

it('replaces property `computed` in `type` with `signalStoreFeature` to `props`', async () => {
const input = tags.stripIndent`
export function withMyFeature() {
return signalStoreFeature(
type<{ computed: { num: Signal<number> } }>()
);
}
`;

const output = tags.stripIndent`
export function withMyFeature() {
return signalStoreFeature(
type<{ props: { num: Signal<number> } }>()
);
}
`;

await verifySchematic(input, output);
});

it('replaces `computed` in `SignalStoreFeature` to `props`', async () => {
const input = tags.stripIndent`
import { SignalStoreFeature } from '@ngrx/signals';
import { Signal } from '@angular/core';

declare function withMyFeature(): SignalStoreFeature<
{ state: {}; computed: { num1: Signal<number> }; methods: {} },
{ state: {}; computed: { num2: Signal<number> }; methods: {} }
>;
`;

const output = tags.stripIndent`
import { SignalStoreFeature } from '@ngrx/signals';
import { Signal } from '@angular/core';

declare function withMyFeature(): SignalStoreFeature<
{ state: {}; props: { num1: Signal<number> }; methods: {} },
{ state: {}; props: { num2: Signal<number> }; methods: {} }
>;
`;

const logEntries = await verifySchematic(input, output);
expect(logEntries).toEqual([
"[@ngrx/signals] Renamed 'computed' to 'props' in SignalStoreFeature<> in /main.ts",
]);
});

test('kitchen sink', async () => {
const input = tags.stripIndent`
import { EntityComputed, NamedEntityComputed } from '@ngrx/signals/entities';
import {
EmptyFeatureResult,
SignalStoreFeature,
signalStoreFeature,
type,
withHooks,
withMethods,
} from '@ngrx/signals';
import { rxMethod } from '@ngrx/signals/rxjs-interop';
import { tap } from 'rxjs/operators';
import { Signal } from '@angular/core';

declare function withNamedDataService<
E extends { id: number },
Collection extends string
>(): SignalStoreFeature<
EmptyFeatureResult & { computed: NamedEntityComputed<E, Collection> }
>;

declare function withDataService<
E extends { id: number },
Collection extends string
>(): SignalStoreFeature<EmptyFeatureResult & { computed: EntityComputed<E> }>;

export function withConsoleLogger() {
return signalStoreFeature(
{ computed: type<{ pretty: Signal<string> }>() },
withMethods(() => ({
log: rxMethod<string>(tap((message) => console.log(message))),
})),
withHooks((store) => ({
onInit() {
store.log(store.pretty());
},
}))
);
}`;

const output = tags.stripIndent`
import { EntityProps, NamedEntityProps } from '@ngrx/signals/entities';
import {
EmptyFeatureResult,
SignalStoreFeature,
signalStoreFeature,
type,
withHooks,
withMethods,
} from '@ngrx/signals';
import { rxMethod } from '@ngrx/signals/rxjs-interop';
import { tap } from 'rxjs/operators';
import { Signal } from '@angular/core';

declare function withNamedDataService<
E extends { id: number },
Collection extends string
>(): SignalStoreFeature<
EmptyFeatureResult & { props: NamedEntityProps<E, Collection> }
>;

declare function withDataService<
E extends { id: number },
Collection extends string
>(): SignalStoreFeature<EmptyFeatureResult & { props: EntityProps<E> }>;

export function withConsoleLogger() {
return signalStoreFeature(
{ props: type<{ pretty: Signal<string> }>() },
withMethods(() => ({
log: rxMethod<string>(tap((message) => console.log(message))),
})),
withHooks((store) => ({
onInit() {
store.log(store.pretty());
},
}))
);
}`;

const logEntries = await verifySchematic(input, output);
expect(logEntries).toEqual([
"[@ngrx/signals] Renamed '(Named)EntityComputed' to '(Named)EntityProps' in /main.ts",
"[@ngrx/signals] Renamed 'computed' to 'props' in SignalStoreFeature<> in /main.ts",
"[@ngrx/signals] Renamed 'computed' to 'props' in signalStoreFeature() in /main.ts",
]);
});

it('creates two files with minimal changes and checks both files', async () => {
const input1 = tags.stripIndent`
import { EntityComputed } from '@ngrx/signals/entities';
`;

const output1 = tags.stripIndent`
import { EntityProps } from '@ngrx/signals/entities';
`;

const input2 = tags.stripIndent`
import { NamedEntityComputed } from '@ngrx/signals/entities';
`;

const output2 = tags.stripIndent`
import { NamedEntityProps } from '@ngrx/signals/entities';
`;

appTree.create('file1.ts', input1);
appTree.create('file2.ts', input2);

const logEntries: string[] = [];
schematicRunner.logger.subscribe((logEntry) =>
logEntries.push(logEntry.message)
);

const tree = await schematicRunner.runSchematic(
`19_0_0-rc_1-props`,
{},
appTree
);

const actual1 = tree.readContent('file1.ts');
const actual2 = tree.readContent('file2.ts');

expect(actual1).toBe(output1);
expect(actual2).toBe(output2);

expect(logEntries).toEqual([
"[@ngrx/signals] Renamed '(Named)EntityComputed' to '(Named)EntityProps' in /file1.ts",
"[@ngrx/signals] Renamed '(Named)EntityComputed' to '(Named)EntityProps' in /file2.ts",
]);
});
});
Loading
Loading