-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathprepare-controller.ts
125 lines (102 loc) · 4.12 KB
/
prepare-controller.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
120
121
122
123
124
125
import { assert } from "chai";
import { PrepareController } from "../../lib/controllers/prepare-controller";
import { InjectorStub } from "../stubs";
import { PREPARE_READY_EVENT_NAME } from "../../lib/constants";
const projectDir = "/path/to/my/projecDir";
const prepareData = {
projectDir,
release: false,
hmr: false,
env: {},
watch: true
};
let isCompileWithWatchCalled = false;
let isCompileWithoutWatchCalled = false;
let isNativePrepareCalled = false;
let emittedEventNames: string[] = [];
let emittedEventData: any[] = [];
function createTestInjector(data: { hasNativeChanges: boolean }): IInjector {
const injector = new InjectorStub();
injector.register("platformController", {
addPlatformIfNeeded: () => ({})
});
injector.register("prepareNativePlatformService", ({
prepareNativePlatform: async () => {
isNativePrepareCalled = true;
return data.hasNativeChanges;
}
}));
injector.register("webpackCompilerService", ({
on: () => ({}),
emit: () => ({}),
compileWithWatch: async () => {
isCompileWithWatchCalled = true;
},
compileWithoutWatch: async () => {
isCompileWithoutWatchCalled = true;
}
}));
injector.register("prepareController", PrepareController);
injector.register("nodeModulesDependenciesBuilder", {
getProductionDependencies: () => (<any>[])
});
const prepareController: PrepareController = injector.resolve("prepareController");
prepareController.emit = (eventName: string, eventData: any) => {
emittedEventNames.push(eventName);
emittedEventData.push(eventData);
assert.isTrue(isCompileWithWatchCalled);
assert.isTrue(isNativePrepareCalled);
return true;
};
return injector;
}
describe("prepareController", () => {
afterEach(() => {
isNativePrepareCalled = false;
isCompileWithWatchCalled = false;
isCompileWithoutWatchCalled = false;
emittedEventNames = [];
emittedEventData = [];
});
describe("preparePlatform with watch", () => {
_.each(["iOS", "Android"], platform => {
_.each([true, false], hasNativeChanges => {
it(`should execute native prepare and webpack's compilation for ${platform} platform when hasNativeChanges is ${hasNativeChanges}`, async () => {
const injector = createTestInjector({ hasNativeChanges });
const prepareController: PrepareController = injector.resolve("prepareController");
await prepareController.prepare({ ...prepareData, platform });
assert.isTrue(isCompileWithWatchCalled);
assert.isTrue(isNativePrepareCalled);
});
});
it(`should respect native changes that are made before the initial preparation of the project had been done for ${platform}`, async () => {
const injector = createTestInjector({ hasNativeChanges: false });
const prepareController: PrepareController = injector.resolve("prepareController");
const prepareNativePlatformService = injector.resolve("prepareNativePlatformService");
prepareNativePlatformService.prepareNativePlatform = async () => {
const nativeFilesWatcher = (<any>prepareController).watchersData[projectDir][platform.toLowerCase()].nativeFilesWatcher;
nativeFilesWatcher.emit("all", "change", "my/project/App_Resources/some/file");
isNativePrepareCalled = true;
return false;
};
await prepareController.prepare({ ...prepareData, platform });
assert.lengthOf(emittedEventNames, 1);
assert.lengthOf(emittedEventData, 1);
assert.deepEqual(emittedEventNames[0], PREPARE_READY_EVENT_NAME);
assert.deepEqual(emittedEventData[0], { files: [], hasNativeChanges: true, hasOnlyHotUpdateFiles: false, hmrData: null, platform: platform.toLowerCase() });
});
});
});
describe("preparePlatform without watch", () => {
_.each(["ios", "android"], platform => {
it("shouldn't start the watcher when watch is false", async () => {
const injector = createTestInjector({ hasNativeChanges: false });
const prepareController: PrepareController = injector.resolve("prepareController");
await prepareController.prepare({ ...prepareData, watch: false, platform });
assert.isTrue(isNativePrepareCalled);
assert.isTrue(isCompileWithoutWatchCalled);
assert.isFalse(isCompileWithWatchCalled);
});
});
});
});