Skip to content

Commit ded4240

Browse files
feat(store): add createMockStore migration (#3810)
1 parent 193e232 commit ded4240

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Tree } from '@angular-devkit/schematics';
2+
import {
3+
SchematicTestRunner,
4+
UnitTestTree,
5+
} from '@angular-devkit/schematics/testing';
6+
import * as path from 'path';
7+
8+
describe('Store Migration 16_0_0-beta', () => {
9+
const collectionPath = path.join(__dirname, '../migration.json');
10+
11+
it(`should replace getMockStore with createMockStore`, async () => {
12+
const input = `
13+
import { getMockStore } from '@ngrx/store';
14+
import { SomethingElse } from '@ngrx/store';
15+
import {getMockStore} from '@ngrx/store';
16+
import {foo, getMockStore, bar} from '@ngrx/store';
17+
18+
const mockStore = getMockStore();
19+
20+
it('just a test', () => {
21+
const s =getMockStore();
22+
})
23+
`;
24+
25+
const expected = `
26+
import {createMockStore } from '@ngrx/store';
27+
import { SomethingElse } from '@ngrx/store';
28+
import {createMockStore} from '@ngrx/store';
29+
import {foo,createMockStore, bar} from '@ngrx/store';
30+
31+
const mockStore =createMockStore();
32+
33+
it('just a test', () => {
34+
const s =createMockStore();
35+
})
36+
`;
37+
const appTree = new UnitTestTree(Tree.empty());
38+
appTree.create('./fixture.ts', input);
39+
const runner = new SchematicTestRunner('schematics', collectionPath);
40+
41+
const newTree = await runner.runSchematic(
42+
`ngrx-store-migration-16-0-0-beta`,
43+
{},
44+
appTree
45+
);
46+
const file = newTree.readContent('fixture.ts');
47+
48+
expect(file).toBe(expected);
49+
});
50+
});
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import * as ts from 'typescript';
2+
import { Rule, chain, Tree } from '@angular-devkit/schematics';
3+
import {
4+
visitTSSourceFiles,
5+
RemoveChange,
6+
InsertChange,
7+
commitChanges,
8+
} from '../../schematics-core';
9+
10+
function updateGetMockStore(): Rule {
11+
return (tree: Tree) => {
12+
visitTSSourceFiles(tree, (sourceFile) => {
13+
const imports = sourceFile.statements
14+
.filter(ts.isImportDeclaration)
15+
.filter(
16+
(importDeclaration) =>
17+
importDeclaration.moduleSpecifier.getText(sourceFile) ===
18+
"'@ngrx/store'" ||
19+
importDeclaration.moduleSpecifier.getText(sourceFile) ===
20+
'"@ngrx/store"'
21+
)
22+
.flatMap((importDeclaration) => {
23+
return importDeclaration.importClause?.namedBindings ?? [];
24+
})
25+
.flatMap((binding) =>
26+
ts.isNamedImports(binding) ? binding.elements : []
27+
)
28+
.filter(
29+
(element) => element.name.getText(sourceFile) === 'getMockStore'
30+
);
31+
if (!imports.length) return;
32+
33+
const changes: (InsertChange | RemoveChange)[] = [];
34+
imports.forEach((binding) => {
35+
changes.push(
36+
new RemoveChange(sourceFile.fileName, binding.pos, binding.end)
37+
);
38+
changes.push(
39+
new InsertChange(sourceFile.fileName, binding.pos, 'createMockStore')
40+
);
41+
});
42+
43+
ts.forEachChild(sourceFile, crawl);
44+
return commitChanges(tree, sourceFile.fileName, changes);
45+
46+
function crawl(node: ts.Node) {
47+
ts.forEachChild(node, crawl);
48+
49+
if (!ts.isCallExpression(node)) return;
50+
if (!ts.isIdentifier(node.expression)) return;
51+
if (node.expression.text !== 'getMockStore') return;
52+
53+
changes.push(
54+
new RemoveChange(
55+
sourceFile.fileName,
56+
node.expression.pos,
57+
node.expression.end
58+
)
59+
);
60+
changes.push(
61+
new InsertChange(
62+
sourceFile.fileName,
63+
node.expression.pos,
64+
'createMockStore'
65+
)
66+
);
67+
}
68+
});
69+
};
70+
}
71+
72+
export default function (): Rule {
73+
return chain([updateGetMockStore]);
74+
}

modules/store/migrations/migration.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"description": "The road to v15.2",
3131
"version": "15.2.0",
3232
"factory": "./15_2_0/index"
33+
},
34+
"ngrx-store-migration-16-0-0-beta": {
35+
"description": "The road to v16.0-beta",
36+
"version": "16.0.0-beta",
37+
"factory": "./16_0_0-beta/index"
3338
}
3439
}
3540
}

0 commit comments

Comments
 (0)