@@ -2,10 +2,11 @@ import { Yok } from "../../lib/common/yok";
2
2
import { assert } from "chai" ;
3
3
import { ProjectDataService } from "../../lib/services/project-data-service" ;
4
4
import { LoggerStub , ProjectDataStub } from "../stubs" ;
5
- import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER , PACKAGE_JSON_FILE_NAME , AssetConstants , ProjectTypes } from '../../lib/constants' ;
5
+ import { NATIVESCRIPT_PROPS_INTERNAL_DELIMITER , PACKAGE_JSON_FILE_NAME , CONFIG_NS_FILE_NAME , AssetConstants , ProjectTypes } from '../../lib/constants' ;
6
6
import { DevicePlatformsConstants } from "../../lib/common/mobile/device-platforms-constants" ;
7
7
import { basename , join } from "path" ;
8
8
import { FileSystem } from "../../lib/common/file-system" ;
9
+ import { regExpEscape } from "../../lib/common/helpers" ;
9
10
10
11
const CLIENT_NAME_KEY_IN_PROJECT_FILE = "nativescript" ;
11
12
@@ -41,7 +42,7 @@ const testData: any = [
41
42
}
42
43
] ;
43
44
44
- const createTestInjector = ( readTextData ?: string ) : IInjector => {
45
+ const createTestInjector = ( packageJsonContent ?: string , nsConfigContent ?: string ) : IInjector => {
45
46
const testInjector = new Yok ( ) ;
46
47
testInjector . register ( "projectData" , ProjectDataStub ) ;
47
48
testInjector . register ( "staticConfig" , {
@@ -55,10 +56,14 @@ const createTestInjector = (readTextData?: string): IInjector => {
55
56
} ,
56
57
57
58
readText : ( filename : string , encoding ?: IReadFileOptions | string ) : string => {
58
- return readTextData ;
59
+ if ( filename . indexOf ( "package.json" ) > - 1 ) {
60
+ return packageJsonContent ;
61
+ } else if ( filename . indexOf ( "nsconfig.json" ) > - 1 ) {
62
+ return nsConfigContent ;
63
+ }
59
64
} ,
60
65
61
- exists : ( filePath : string ) : boolean => basename ( filePath ) === PACKAGE_JSON_FILE_NAME ,
66
+ exists : ( filePath : string ) : boolean => ( basename ( filePath ) === PACKAGE_JSON_FILE_NAME || basename ( filePath ) === CONFIG_NS_FILE_NAME ) ,
62
67
63
68
readJson : ( filePath : string ) : any => null ,
64
69
@@ -91,7 +96,7 @@ const createTestInjector = (readTextData?: string): IInjector => {
91
96
return testInjector ;
92
97
} ;
93
98
94
- describe ( "projectDataService" , ( ) => {
99
+ describe . only ( "projectDataService" , ( ) => {
95
100
const generateJsonDataFromTestData = ( currentTestData : any , skipNativeScriptKey ?: boolean ) => {
96
101
const props = currentTestData . propertyName . split ( NATIVESCRIPT_PROPS_INTERNAL_DELIMITER ) ;
97
102
const data : any = { } ;
@@ -245,6 +250,66 @@ describe("projectDataService", () => {
245
250
} ) ;
246
251
} ) ;
247
252
253
+ describe ( "removeNSConfigProperty" , ( ) => {
254
+
255
+ const generateExpectedDataFromTestData = ( currentTestData : any ) => {
256
+ const props = currentTestData . propertyName . split ( NATIVESCRIPT_PROPS_INTERNAL_DELIMITER ) ;
257
+ props . splice ( props . length - 1 , 1 ) ;
258
+
259
+ const data : any = { } ;
260
+ let currentData : any = data ;
261
+
262
+ _ . each ( props , ( prop ) => {
263
+ currentData = currentData [ prop ] = { } ;
264
+ } ) ;
265
+
266
+ return data ;
267
+ } ;
268
+
269
+ _ . each ( testData , currentTestData => {
270
+
271
+ it ( currentTestData . description , ( ) => {
272
+ const testInjector = createTestInjector ( null , generateFileContentFromTestData ( currentTestData , true ) ) ;
273
+ const fs : IFileSystem = testInjector . resolve ( "fs" ) ;
274
+
275
+ let dataPassedToWriteJson : any = null ;
276
+ fs . writeJson = ( filename : string , data : any , space ?: string , encoding ?: string ) : void => {
277
+ dataPassedToWriteJson = data ;
278
+ } ;
279
+
280
+ const projectDataService : IProjectDataService = testInjector . resolve ( "projectDataService" ) ;
281
+ const propDelimiterRegExp = new RegExp ( regExpEscape ( NATIVESCRIPT_PROPS_INTERNAL_DELIMITER ) , "g" ) ;
282
+ const propertySelector = currentTestData . propertyName . replace ( propDelimiterRegExp , "." ) ;
283
+ projectDataService . removeNSConfigProperty ( "projectDir" , propertySelector ) ;
284
+
285
+ assert . deepEqual ( dataPassedToWriteJson , generateExpectedDataFromTestData ( currentTestData ) ) ;
286
+ } ) ;
287
+
288
+ } ) ;
289
+
290
+ it ( "removes only the selected property" , ( ) => {
291
+ const initialData : any = { } ;
292
+ initialData [ CLIENT_NAME_KEY_IN_PROJECT_FILE ] = {
293
+ "root" : {
294
+ "id" : "1" ,
295
+ "constantItem" : "myValue"
296
+ }
297
+ } ;
298
+
299
+ const testInjector = createTestInjector ( JSON . stringify ( initialData ) ) ;
300
+ const fs : IFileSystem = testInjector . resolve ( "fs" ) ;
301
+
302
+ let dataPassedToWriteJson : any = null ;
303
+ fs . writeJson = ( filename : string , data : any , space ?: string , encoding ?: string ) : void => {
304
+ dataPassedToWriteJson = data ;
305
+ } ;
306
+
307
+ const projectDataService : IProjectDataService = testInjector . resolve ( "projectDataService" ) ;
308
+ projectDataService . removeNSProperty ( "projectDir" , getPropertyName ( [ "root" , "id" ] ) ) ;
309
+ assert . deepEqual ( dataPassedToWriteJson , { nativescript : { root : { constantItem : "myValue" } } } ) ;
310
+ } ) ;
311
+ } ) ;
312
+
248
313
describe ( "removeDependency" , ( ) => {
249
314
it ( "removes specified dependency from project file" , ( ) => {
250
315
const currentTestData = {
0 commit comments