@@ -25,6 +25,7 @@ import { Messages } from "../lib/common/messages/messages";
2525import { SettingsService } from "../lib/common/test/unit-tests/stubs" ;
2626import { INFO_PLIST_FILE_NAME , MANIFEST_FILE_NAME } from "../lib/constants" ;
2727import { mkdir } from "shelljs" ;
28+ import * as constants from "../lib/constants" ;
2829
2930require ( "should" ) ;
3031const temp = require ( "temp" ) ;
@@ -121,6 +122,7 @@ function createTestInjector() {
121122 } ) ) ;
122123 }
123124 } ) ;
125+ testInjector . register ( "usbLiveSyncService" , ( ) => ( { } ) ) ;
124126
125127 return testInjector ;
126128}
@@ -1004,4 +1006,156 @@ describe('Platform Service Tests', () => {
10041006 } ) ;
10051007 } ) ;
10061008 } ) ;
1009+
1010+ describe ( "ensurePlatformInstalled" , ( ) => {
1011+ const platform = "android" ;
1012+ const platformTemplate = "testPlatformTemplate" ;
1013+ const appFilesUpdaterOptions = { bundle : true } ;
1014+ let isPlatformAdded = false ;
1015+ let areWebpackFilesPersisted = false ;
1016+
1017+ let projectData : IProjectData = null ;
1018+ let usbLiveSyncService : any = null ;
1019+ let projectChangesService : IProjectChangesService = null ;
1020+
1021+ beforeEach ( ( ) => {
1022+ reset ( ) ;
1023+
1024+ ( < any > platformService ) . addPlatform = ( ) => isPlatformAdded = true ;
1025+ ( < any > platformService ) . persistWebpackFiles = ( ) => areWebpackFilesPersisted = true ;
1026+
1027+ projectData = testInjector . resolve ( "projectData" ) ;
1028+ usbLiveSyncService = testInjector . resolve ( "usbLiveSyncService" ) ;
1029+ projectChangesService = testInjector . resolve ( "projectChangesService" ) ;
1030+
1031+ usbLiveSyncService . isInitialized = true ;
1032+ } ) ;
1033+
1034+ function reset ( ) {
1035+ isPlatformAdded = false ;
1036+ areWebpackFilesPersisted = false ;
1037+ }
1038+
1039+ function mockPrepareInfo ( prepareInfo : any ) {
1040+ projectChangesService . getPrepareInfo = ( ) => prepareInfo ;
1041+ }
1042+
1043+ const testCases = [
1044+ {
1045+ name : "should persist webpack files when prepareInfo is null (first execution of `tns run --bundle`)" ,
1046+ areWebpackFilesPersisted : true
1047+ } ,
1048+ {
1049+ name : "should persist webpack files when prepareInfo is null and skipNativePrepare is true (first execution of `tns preview --bundle`)" ,
1050+ nativePrepare : { skipNativePrepare : true } ,
1051+ areWebpackFilesPersisted : true
1052+ } ,
1053+ {
1054+ name : "should not persist webpack files when requires platform add" ,
1055+ prepareInfo : { nativePlatformStatus : constants . NativePlatformStatus . requiresPlatformAdd } ,
1056+ areWebpackFilesPersisted : true
1057+ } ,
1058+ {
1059+ name : "should persist webpack files when requires platform add and skipNativePrepare is true" ,
1060+ prepareInfo : { nativePlatformStatus : constants . NativePlatformStatus . requiresPlatformAdd } ,
1061+ nativePrepare : { skipNativePrepare : true } ,
1062+ areWebpackFilesPersisted : false
1063+ } ,
1064+ {
1065+ name : "should persist webpack files when platform is already prepared" ,
1066+ prepareInfo : { nativePlatformStatus : constants . NativePlatformStatus . alreadyPrepared } ,
1067+ areWebpackFilesPersisted : false
1068+ } ,
1069+ {
1070+ name : "should not persist webpack files when platform is already prepared and skipNativePrepare is true" ,
1071+ prepareInfo : { nativePlatformStatus : constants . NativePlatformStatus . alreadyPrepared } ,
1072+ areWebpackFilesPersisted : false
1073+ } ,
1074+ {
1075+ name : "should not persist webpack files when no webpack watcher is started (first execution of `tns build --bundle`)" ,
1076+ isWebpackWatcherStarted : false ,
1077+ areWebpackFilesPersisted : false
1078+ } ,
1079+ {
1080+ name : "should not persist webpack files when no webpack watcher is started and skipNativePrepare is true (local JS prepare from cloud command)" ,
1081+ isWebpackWatcherStarted : false ,
1082+ nativePrepare : { skipNativePrepare : true } ,
1083+ areWebpackFilesPersisted : false
1084+ }
1085+ ] ;
1086+
1087+ _ . each ( testCases , ( testCase : any ) => {
1088+ it ( `${ testCase . name } ` , async ( ) => {
1089+ usbLiveSyncService . isInitialized = testCase . isWebpackWatcherStarted === undefined ? true : testCase . isWebpackWatcherStarted ;
1090+ mockPrepareInfo ( testCase . prepareInfo ) ;
1091+
1092+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions , testCase . nativePrepare ) ;
1093+ assert . deepEqual ( areWebpackFilesPersisted , testCase . areWebpackFilesPersisted ) ;
1094+ } ) ;
1095+ } ) ;
1096+
1097+ it ( "should not persist webpack files after the second execution of `tns preview --bundle` or `tns cloud run --bundle`" , async ( ) => {
1098+ // First execution of `tns preview --bundle`
1099+ mockPrepareInfo ( null ) ;
1100+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions , { skipNativePrepare : true } ) ;
1101+ assert . isTrue ( areWebpackFilesPersisted ) ;
1102+
1103+ // Second execution of `tns preview --bundle`
1104+ reset ( ) ;
1105+ mockPrepareInfo ( { nativePlatformStatus : constants . NativePlatformStatus . requiresPlatformAdd } ) ;
1106+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions , { skipNativePrepare : true } ) ;
1107+ assert . isFalse ( areWebpackFilesPersisted ) ;
1108+ } ) ;
1109+
1110+ it ( "should not persist webpack files after the second execution of `tns run --bundle`" , async ( ) => {
1111+ // First execution of `tns run --bundle`
1112+ mockPrepareInfo ( null ) ;
1113+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions ) ;
1114+ assert . isTrue ( areWebpackFilesPersisted ) ;
1115+
1116+ // Second execution of `tns run --bundle`
1117+ reset ( ) ;
1118+ mockPrepareInfo ( { nativePlatformStatus : constants . NativePlatformStatus . alreadyPrepared } ) ;
1119+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions ) ;
1120+ assert . isFalse ( areWebpackFilesPersisted ) ;
1121+ } ) ;
1122+
1123+ it ( "should handle correctly the following sequence of commands: `tns preview --bundle`, `tns run --bundle` and `tns preview --bundle`" , async ( ) => {
1124+ // First execution of `tns preview --bundle`
1125+ mockPrepareInfo ( null ) ;
1126+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions , { skipNativePrepare : true } ) ;
1127+ assert . isTrue ( areWebpackFilesPersisted ) ;
1128+
1129+ // Execution of `tns run --bundle`
1130+ reset ( ) ;
1131+ mockPrepareInfo ( { nativePlatformStatus : constants . NativePlatformStatus . requiresPlatformAdd } ) ;
1132+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions ) ;
1133+ assert . isTrue ( areWebpackFilesPersisted ) ;
1134+
1135+ // Execution of `tns preview --bundle`
1136+ reset ( ) ;
1137+ mockPrepareInfo ( { nativePlatformStatus : constants . NativePlatformStatus . alreadyPrepared } ) ;
1138+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions , { skipNativePrepare : true } ) ;
1139+ assert . isFalse ( areWebpackFilesPersisted ) ;
1140+ } ) ;
1141+
1142+ it ( "should handle correctly the following sequence of commands: `tns preview --bundle`, `tns run --bundle` and `tns build --bundle`" , async ( ) => {
1143+ // Execution of `tns preview --bundle`
1144+ mockPrepareInfo ( null ) ;
1145+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions , { skipNativePrepare : true } ) ;
1146+ assert . isTrue ( areWebpackFilesPersisted ) ;
1147+
1148+ // Execution of `tns run --bundle`
1149+ reset ( ) ;
1150+ mockPrepareInfo ( { nativePlatformStatus : constants . NativePlatformStatus . requiresPlatformAdd } ) ;
1151+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions ) ;
1152+ assert . isTrue ( areWebpackFilesPersisted ) ;
1153+
1154+ // Execution of `tns build --bundle`
1155+ reset ( ) ;
1156+ mockPrepareInfo ( { nativePlatformStatus : constants . NativePlatformStatus . alreadyPrepared } ) ;
1157+ await ( < any > platformService ) . ensurePlatformInstalled ( platform , platformTemplate , projectData , config , appFilesUpdaterOptions ) ;
1158+ assert . isFalse ( areWebpackFilesPersisted ) ;
1159+ } ) ;
1160+ } ) ;
10071161} ) ;
0 commit comments