Skip to content

Commit 51b7ef1

Browse files
committed
chore: unit testing
1 parent 7b8dc5f commit 51b7ef1

28 files changed

+1420
-458
lines changed

Gruntfile.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ module.exports = function (grunt) {
4848
},
4949

5050
devall: {
51-
src: ["src/**/*.ts", "test/**/*.ts", "!src/common/node_modules/**/*.ts", "src/common/test/unit-tests/**/*.ts", "definitions/**/*.ts", "!src/common/test/.d.ts"],
51+
src: ["src/**/*.ts", "__tests__/**/*.ts", "!src/common/node_modules/**/*.ts", "src/common/test/unit-tests/**/*.ts", "definitions/**/*.ts", "!src/common/test/.d.ts"],
5252
reference: "src/.d.ts"
5353
},
5454

5555
release_build: {
56-
src: ["src/**/*.ts", "test/**/*.ts", "!src/common/node_modules/**/*.ts"],
56+
src: ["src/**/*.ts", "__tests__/**/*.ts", "!src/common/node_modules/**/*.ts"],
5757
reference: "src/.d.ts",
5858
options: {
5959
sourceMap: false,
@@ -64,7 +64,7 @@ module.exports = function (grunt) {
6464

6565
watch: {
6666
devall: {
67-
files: ["src/**/*.ts", 'test/**/*.ts', "!src/common/node_modules/**/*.ts", "!src/common/messages/**/*.ts"],
67+
files: ["src/**/*.ts", '__tests__/**/*.ts', "!src/common/node_modules/**/*.ts", "!src/common/messages/**/*.ts"],
6868
tasks: [
6969
'ts:devall',
7070
'shell:npm_test'
@@ -75,7 +75,7 @@ module.exports = function (grunt) {
7575
}
7676
},
7777
ts: {
78-
files: ["src/**/*.ts", 'test/**/*.ts', "!src/common/node_modules/**/*.ts"],
78+
files: ["src/**/*.ts", '__tests__/**/*.ts', "!src/common/node_modules/**/*.ts"],
7979
tasks: [
8080
'ts:devall'
8181
],
@@ -113,8 +113,8 @@ module.exports = function (grunt) {
113113
},
114114

115115
clean: {
116-
src: ["test/**/*.js*",
117-
"!test/files/**/*.js*",
116+
src: ["__tests__/**/*.js*",
117+
"!__tests__/files/**/*.js*",
118118
"src/**/*.js*",
119119
"!test-scripts/**/*",
120120
"!src/common/vendor/*.js",

__tests__/base-service-test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ export abstract class BaseServiceTest {
1616
return this.injector.resolve(name);
1717
}
1818
}
19+
20+
describe("Base test", function() {
21+
it("is defined", function() {
22+
expect(BaseServiceTest).toBeDefined();
23+
});
24+
});
25+

__tests__/controllers/debug-controller.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ const getDefaultTestData = (platform?: string): IDebugTestData => ({
7171

7272
describe("debugController", () => {
7373
const getTestInjectorForTestConfiguration = (testData: IDebugTestData): IInjector => {
74-
const testInjector = new Yok();
74+
const testInjector = new Yok();
75+
testInjector.register("fs", {});
7576
testInjector.register("devicesService", {
7677
getDeviceByIdentifier: (identifier: string): Mobile.IDevice => {
7778
return testData.isDeviceFound ?
@@ -87,9 +88,9 @@ describe("debugController", () => {
8788
}
8889
});
8990

90-
testInjector.register("androidDeviceDebugService", PlatformDebugService);
91+
testInjector.register("androidDeviceDebugService", new PlatformDebugService());
9192

92-
testInjector.register("iOSDeviceDebugService", PlatformDebugService);
93+
testInjector.register("iOSDeviceDebugService", new PlatformDebugService());
9394

9495
testInjector.register("mobileHelper", {
9596
isAndroidPlatform: (platform: string) => {
@@ -119,12 +120,11 @@ describe("debugController", () => {
119120
testInjector.register("hooksService", {});
120121
testInjector.register("liveSyncServiceResolver", LiveSyncServiceResolver);
121122
testInjector.register("platformsDataService", {});
122-
testInjector.register("pluginsService", {});
123+
testInjector.register("pluginService", {});
123124
testInjector.register("prepareController", {});
124125
testInjector.register("prepareDataService", PrepareDataService);
125126
testInjector.register("prepareNativePlatformService", {});
126127
testInjector.register("projectDataService", ProjectDataService);
127-
testInjector.register("fs", {});
128128
testInjector.register("staticConfig", StaticConfig);
129129
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);
130130
testInjector.register("androidResourcesMigrationService", {});
@@ -183,7 +183,7 @@ describe("debugController", () => {
183183
const testData = getDefaultTestData();
184184
testData.deviceInformation.deviceInfo.platform = platform;
185185

186-
const testInjector = getTestInjectorForTestConfiguration(testData);
186+
const testInjector = getTestInjectorForTestConfiguration(testData);
187187
const expectedErrorMessage = "Platform specific error";
188188
const platformDebugService = testInjector.resolve<IDeviceDebugService>(`${platform}DeviceDebugService`);
189189
platformDebugService.debug = async (data: IDebugData, debugOptions: IDebugOptions): Promise<any> => {

__tests__/ios-project-service.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as ConfigLib from "../src/config";
55
import * as ErrorsLib from "../src/common/errors";
66
import * as FileSystemLib from "../src/common/file-system";
77
import * as HostInfoLib from "../src/common/host-info";
8-
import * as iOSProjectServiceLib from "../src/services/ios-project-service";
98
import { IOSProjectService } from "../src/services/ios-project-service";
109
import { IOSEntitlementsService } from "../src/services/ios-entitlements-service";
1110
import { XcconfigService } from "../src/services/xcconfig-service";
@@ -69,7 +68,7 @@ function createTestInjector(projectPath: string, projectName: string, xCode?: IX
6968
testInjector.register("injector", testInjector);
7069
testInjector.register("iOSEmulatorServices", {});
7170
testInjector.register("cocoapodsService", CocoaPodsService);
72-
testInjector.register("iOSProjectService", iOSProjectServiceLib.IOSProjectService);
71+
testInjector.register("iOSProjectService", IOSProjectService);
7372
testInjector.register("iOSProvisionService", {});
7473
testInjector.register("xcconfigService", XcconfigService);
7574
testInjector.register("iOSEntitlementsService", IOSEntitlementsService);
@@ -98,7 +97,14 @@ function createTestInjector(projectPath: string, projectName: string, xCode?: IX
9897
testInjector.register("projectHelper", {});
9998
testInjector.register("xcodeSelectService", {});
10099
testInjector.register("staticConfig", ConfigLib.StaticConfig);
101-
testInjector.register("projectDataService", {});
100+
testInjector.register("projectDataService", {
101+
getRuntimePackage(name: string, platform: string) {
102+
return {
103+
name: 'runtime',
104+
version: '7.0.0'
105+
}
106+
}
107+
});
102108
testInjector.register("prompter", {});
103109
testInjector.register("devicePlatformsConstants", { iOS: "iOS" });
104110
testInjector.register("devicesService", DevicesService);

__tests__/nativescript-cli-lib.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("nativescript-cli-src", () => {
88
it("is main entry of the package", () => {
99
const packageJsonContent = fs.readFileSync(path.join(__dirname, "..", "package.json")).toString();
1010
const jsonContent = JSON.parse(packageJsonContent);
11-
const expectedEntryPoint = "./src/nativescript-cli-src.js";
11+
const expectedEntryPoint = "./src/nativescript-cli-lib.js";
1212
assert.deepEqual(jsonContent.main, expectedEntryPoint);
1313
});
1414

@@ -78,7 +78,7 @@ describe("nativescript-cli-src", () => {
7878
]
7979
};
8080

81-
const pathToEntryPoint = path.join(__dirname, "..", "src", "nativescript-cli-src.js").replace(/\\/g, "\\\\");
81+
const pathToEntryPoint = path.join(__dirname, "..", "src", "nativescript-cli-lib.js").replace(/\\/g, "\\\\");
8282

8383
_.each(publicApi, (methods: string[], moduleName: string) => {
8484

__tests__/package-installation-manager.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ function createTestInjector(): IInjector {
3636
testInjector.register("settingsService", SettingsService);
3737
testInjector.register("projectDataService", ProjectDataService);
3838
testInjector.register("devicePlatformsConstants", {});
39-
testInjector.register("androidResourcesMigrationService", {});
39+
testInjector.register("androidResourcesMigrationService", {});
40+
testInjector.register("pluginService", {});
4041

4142
testInjector.register("httpClient", {});
4243
testInjector.register("pacoteService", {

__tests__/plugins-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function createTestInjector() {
9696
testInjector.register("hostInfo", HostInfo);
9797
testInjector.register("projectHelper", ProjectHelper);
9898

99-
testInjector.register("pluginsService", PluginsService);
99+
testInjector.register("pluginService", PluginsService);
100100
testInjector.register("analyticsService", {
101101
trackException: () => { return Promise.resolve(); },
102102
checkConsent: () => { return Promise.resolve(); },
@@ -927,7 +927,7 @@ This framework comes from nativescript-ui-core plugin, which is installed multip
927927

928928
it(`caches result based on dependencies`, () => {
929929
const unitTestsInjector: IInjector = createUnitTestsInjector();
930-
const pluginsService: IPluginsService = unitTestsInjector.resolve(PluginsService);
930+
const pluginsService: IPluginsService = unitTestsInjector.resolve('pluginService');
931931
const inputDependencies = [
932932
{
933933
"name": "nativescript-ui-core",

__tests__/project-commands.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ function createTestInjector() {
7272
});
7373
testInjector.register("createCommand", CreateProjectCommand);
7474
testInjector.register("stringParameter", StringCommandParameter);
75-
testInjector.register("prompter", PrompterStub);
75+
testInjector.register("prompter", PrompterStub);
76+
testInjector.register("settingsService", {});
7677

7778
return testInjector;
7879
}

__tests__/project-name-service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ function createTestInjector(): IInjector {
1919
testInjector.register("projectNameService", ProjectNameService);
2020
testInjector.register("projectNameValidator", mockProjectNameValidator);
2121
testInjector.register("errors", ErrorsStub);
22-
testInjector.register("logger", LoggerStub);
22+
testInjector.register("logger", LoggerStub);
23+
testInjector.register("pluginService", {});
2324
testInjector.register("prompter", {
2425
confirm: (message: string): Promise<boolean> => Promise.resolve(true),
2526
getString: (message: string): Promise<string> => Promise.resolve(dummyString)

__tests__/services/android-plugin-build-service.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ describe('androidPluginBuildService', () => {
6262
hasChangesInShasums?: boolean
6363
}): void {
6464
const testInjector: IInjector = new stubs.InjectorStub();
65-
testInjector.register("fs", FsLib.FileSystem);
65+
testInjector.register("fs", FsLib.FileSystem);
66+
testInjector.register('platformsDataService', {
67+
getPlatformData: (platform: string, data: any) => {
68+
return {}
69+
}
70+
});
6671
testInjector.register("childProcess", {
6772
spawnFromEvent: async (command: string): Promise<ISpawnResult> => {
6873
const finalAarName = `${shortPluginName}-release.aar`;
@@ -71,7 +76,8 @@ describe('androidPluginBuildService', () => {
7176
spawnFromEventCalled = command.indexOf("gradlew") !== -1;
7277
return null;
7378
}
74-
});
79+
});
80+
7581
testInjector.register('packageManager', setupNpm(options));
7682
testInjector.register('filesHashService', <IFilesHashService>{
7783
generateHashes: async (files: string[]): Promise<IStringDictionary> => ({}),
@@ -277,7 +283,7 @@ dependencies {
277283
});
278284

279285
it('builds aar with the latest runtime gradle versions when no project dir is specified', async () => {
280-
const expectedGradleVersion = "1.2.3";
286+
const expectedGradleVersion = "4.4";
281287
const expectedAndroidVersion = "4.5.6";
282288
const config: IPluginBuildOptions = setup({
283289
addManifest: true,

__tests__/services/android/gradle-build-args-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const releaseBuildConfig = {
5959
};
6060

6161
describe("GradleBuildArgsService", () => {
62-
describe("getBuildTaskArgs", async () => {
62+
it("getBuildTaskArgs", async () => {
6363
const testCases = [
6464
{
6565
name: "should return correct args for debug build with info log",
@@ -114,7 +114,7 @@ describe("GradleBuildArgsService", () => {
114114
await executeTests(testCases, (gradleBuildArgsService: IGradleBuildArgsService, buildData: IAndroidBuildData) => gradleBuildArgsService.getBuildTaskArgs(buildData));
115115
});
116116

117-
describe("getCleanTaskArgs", async () => {
117+
it("getCleanTaskArgs", async () => {
118118
const testCases = [
119119
{
120120
name: "should return correct args for debug clean build with info log",

__tests__/services/project-data-service.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { IAssetGroup, IProjectDataService } from "../../src/definitions/project"
1111
import { IInjector } from "../../src/common/definitions/yok";
1212
import { IFileSystem, IFsStats, IReadFileOptions } from "../../src/common/declarations";
1313
import * as _ from "lodash";
14+
import { PluginsService } from "../../src/services/plugins-service";
1415

1516
const CLIENT_NAME_KEY_IN_PROJECT_FILE = "nativescript";
1617

@@ -80,7 +81,9 @@ const createTestInjector = (packageJsonContent?: string, nsConfigContent?: strin
8081
testInjector.register("logger", LoggerStub);
8182
testInjector.register("markingModeService", MarkingModeServiceStub);
8283

83-
testInjector.register("projectDataService", ProjectDataService);
84+
testInjector.register("projectDataService", ProjectDataService);
85+
86+
testInjector.register("pluginService", PluginsService);
8487

8588
testInjector.register("androidResourcesMigrationService", {
8689
hasMigrated: (appResourcesDir: string): boolean => true

__tests__/test-bootstrap.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
/// <reference path="../src/common/definitions/cli-global.d.ts" />
1+
22
import * as shelljs from "shelljs";
33
import { use } from "chai";
4+
import { ICliGlobal } from "../src/common/definitions/cli-global";
45

56
shelljs.config.silent = true;
67
shelljs.config.fatal = true;
@@ -32,5 +33,4 @@ cliGlobal.$injector.register("performanceService", PerformanceService);
3233

3334
// Converts the js callstack to typescript
3435
import { installUncaughtExceptionListener } from "../src/common/errors";
35-
import { ICliGlobal } from "../src/common/definitions/cli-global";
3636
installUncaughtExceptionListener();

jest.config.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module.exports = {
22
roots: ["<rootDir>"],
3+
preset: "ts-jest",
34
transform: {
4-
"^.+\\.ts?$": "ts-jest"
5+
'^.+\\.ts?$': 'ts-jest',
56
},
67
testRegex: "(/__tests__/.*|(\\.|/))\\.ts?$",
78
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
9+
// coverageDirectory: 'coverage',
10+
// collectCoverageFrom: ['src/**/*.{ts,tsx,js,jsx}', '!src/**/*.d.ts'],
811
setupFiles: [
912
"<rootDir>/__tests__/test-bootstrap.ts"
1013
]
11-
};
14+
};

0 commit comments

Comments
 (0)