/
transfer_state_spec.ts
119 lines (94 loc) 路 3.82 KB
/
transfer_state_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {getSystemPath, normalize, virtualFs} from '@angular-devkit/core';
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing';
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import {runfiles} from '@bazel/runfiles';
import shx from 'shelljs';
describe('TransferState migration', () => {
let runner: SchematicTestRunner;
let host: TempScopedNodeJsSyncHost;
let tree: UnitTestTree;
let tmpDirPath: string;
let previousWorkingDir: string;
function writeFile(filePath: string, contents: string) {
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents));
}
function runMigration() {
return runner.runSchematic('migration-transfer-state', {}, tree);
}
beforeEach(() => {
runner = new SchematicTestRunner('test', runfiles.resolvePackageRelative('../migrations.json'));
host = new TempScopedNodeJsSyncHost();
tree = new UnitTestTree(new HostTree(host));
writeFile('/tsconfig.json', JSON.stringify({
compilerOptions: {
lib: ['es2015'],
strictNullChecks: true,
},
}));
writeFile('/angular.json', JSON.stringify({
version: 1,
projects: {t: {root: '', architect: {build: {options: {tsConfig: './tsconfig.json'}}}}}
}));
previousWorkingDir = shx.pwd();
tmpDirPath = getSystemPath(host.root);
// Switch into the temporary directory path. This allows us to run
// the schematic against our custom unit test tree.
shx.cd(tmpDirPath);
});
afterEach(() => {
shx.cd(previousWorkingDir);
shx.rm('-r', tmpDirPath);
});
it('should change imports', async () => {
writeFile('/index.ts', `import { TransferState } from '@angular/platform-browser';`);
await runMigration();
const content = tree.readContent('/index.ts');
expect(content).toContain(`import { TransferState } from '@angular/core'`);
});
it('should change imports', async () => {
writeFile(
'/index.ts',
`import { TransferState, StateKey, makeStateKey } from '@angular/platform-browser';`);
await runMigration();
const content = tree.readContent('/index.ts');
expect(content).not.toContain(`@angular/platform-browser`);
expect(content).toContain(
`import { makeStateKey, StateKey, TransferState } from '@angular/core'`);
});
it('should change imports with existing core import', async () => {
writeFile('/index.ts', `
import { TransferState, StateKey, makeStateKey } from '@angular/platform-browser';
import { NgOnInit } from '@angular/core';
`);
await runMigration();
const content = tree.readContent('/index.ts');
expect(content).toContain(
`import { NgOnInit, makeStateKey, StateKey, TransferState } from '@angular/core'`);
});
it('should change imports but keep others ', async () => {
writeFile(
'/index.ts',
`import { TransferState, StateKey, makeStateKey, bootstrapApplication } from '@angular/platform-browser';`);
await runMigration();
const content = tree.readContent('/index.ts');
expect(content).toContain(
`import { makeStateKey, StateKey, TransferState } from '@angular/core'`);
expect(content).toContain(`import { bootstrapApplication } from '@angular/platform-browser'`);
});
it('should not change imports', async () => {
writeFile('/index.ts', `
import { TransferState } from '@not-angular/platform-browser'
`);
await runMigration();
const content = tree.readContent('/index.ts');
expect(content).toContain(`import { TransferState } from '@not-angular/platform-browser'`);
});
});