@@ -50,65 +50,110 @@ function mockNpm(testInjector: IInjector, versions: string[], latestVersion: str
50
50
} ) ;
51
51
}
52
52
53
- describe ( "Npm installation manager tests" , ( ) => {
54
- it ( "returns correct latest compatible version when only one exists" , ( ) => {
55
- let testInjector = createTestInjector ( ) ;
56
-
57
- let versions = [ "1.4.0" ] ;
58
- let latestVersion = "1.4.0" ;
59
-
60
- mockNpm ( testInjector , versions , latestVersion ) ;
53
+ interface ITestData {
54
+ /**
55
+ * All versions of the package, including the ones from another tags.
56
+ */
57
+ versions : string [ ] ;
58
+
59
+ /**
60
+ * The version under latest tag.
61
+ */
62
+ packageLatestVersion : string ;
63
+
64
+ /**
65
+ * Version of nativescript-cli, based on which the version of the package that will be installed is detected.
66
+ */
67
+ cliVersion : string ;
68
+
69
+ /**
70
+ * Expected result
71
+ */
72
+ expectedResult : string ;
73
+ }
61
74
62
- // Mock staticConfig.version
63
- let staticConfig = testInjector . resolve ( "staticConfig" ) ;
64
- staticConfig . version = "1.4.0" ;
75
+ describe ( "Npm installation manager tests" , ( ) => {
76
+ let testData : IDictionary < ITestData > = {
77
+ "when there's only one available version and it matches CLI's version" : {
78
+ versions : [ "1.4.0" ] ,
79
+ packageLatestVersion : "1.4.0" ,
80
+ cliVersion : "1.4.0" ,
81
+ expectedResult : "1.4.0"
82
+ } ,
65
83
66
- // Mock npmInstallationManager.getLatestVersion
67
- let npmInstallationManager = testInjector . resolve ( "npmInstallationManager" ) ;
68
- npmInstallationManager . getLatestVersion = ( packageName : string ) => Future . fromResult ( latestVersion ) ;
84
+ "when there's only one available version and it is higher than match CLI's version" : {
85
+ versions : [ "1.4.0" ] ,
86
+ packageLatestVersion : "1.4.0" ,
87
+ cliVersion : "1.2.0" ,
88
+ expectedResult : "1.4.0"
89
+ } ,
69
90
70
- let actualLatestCompatibleVersion = npmInstallationManager . getLatestCompatibleVersion ( "" ) . wait ( ) ;
71
- let expectedLatestCompatibleVersion = "1.4.0" ;
72
- assert . equal ( actualLatestCompatibleVersion , expectedLatestCompatibleVersion ) ;
73
- } ) ;
91
+ "when there's only one available version and it is lower than CLI's version" : {
92
+ versions : [ "1.4.0" ] ,
93
+ packageLatestVersion : "1.4.0" ,
94
+ cliVersion : "1.6.0" ,
95
+ expectedResult : "1.4.0"
96
+ } ,
74
97
75
- it ( "returns correct latest compatible version" , ( ) => {
76
- let testInjector = createTestInjector ( ) ;
98
+ "when there are multiple package versions and the latest one matches ~<cli-version>" :{
99
+ versions : [ "1.2.0" , "1.3.0" , "1.3.1" , "1.3.2" , "1.3.3" , "1.4.0" ] ,
100
+ packageLatestVersion : "1.3.3" ,
101
+ cliVersion : "1.3.0" ,
102
+ expectedResult : "1.3.3"
103
+ } ,
77
104
78
- let versions = [ "1.2.0" , "1.3.0" , "1.3.1" , "1.3.2" , "1.3.3" , "1.4.0" ] ;
79
- let latestVersion = "1.3.3" ;
105
+ "when there are multiple package versions and the latest one matches ~<cli-version> when there are newer matching versions but they are not under latest tag" :{
106
+ versions : [ "1.2.0" , "1.3.0" , "1.3.1" , "1.3.2" , "1.3.3" , "1.4.0" ] ,
107
+ packageLatestVersion : "1.3.2" ,
108
+ cliVersion : "1.3.0" ,
109
+ expectedResult : "1.3.2"
110
+ } ,
80
111
81
- mockNpm ( testInjector , versions , latestVersion ) ;
112
+ "when there are multiple package versions and the latest one is lower than ~<cli-version>" : {
113
+ versions : [ "1.2.0" , "1.3.0" , "1.3.1" , "1.3.2" , "1.3.3" , "1.4.0" ] ,
114
+ packageLatestVersion : "1.4.0" ,
115
+ cliVersion : "1.5.0" ,
116
+ expectedResult : "1.4.0"
117
+ } ,
82
118
83
- // Mock staticConfig.version
84
- let staticConfig = testInjector . resolve ( "staticConfig" ) ;
85
- staticConfig . version = "1.3.0" ;
119
+ "when there are multiple package versions and there's beta version matching CLI's semver" : {
120
+ versions : [ "1.2.0" , "1.3.0" , "1.3.1" , "1.4.0" , "1.5.0-2016-02-25-182" ] ,
121
+ packageLatestVersion : "1.4.0" ,
122
+ cliVersion : "1.5.0" ,
123
+ expectedResult : "1.4.0"
124
+ } ,
86
125
87
- // Mock npmInstallationManager.getLatestVersion
88
- let npmInstallationManager = testInjector . resolve ( "npmInstallationManager" ) ;
89
- npmInstallationManager . getLatestVersion = ( packageName : string ) => Future . fromResult ( latestVersion ) ;
126
+ "when there are multiple package versions and package's latest version is greater than CLI's version" : {
127
+ versions : [ "1.2.0" , "1.3.0" , "1.3.1" , "1.4.0" , "1.5.0-2016-02-25-182" , "1.5.0" , "1.6.0" ] ,
128
+ packageLatestVersion : "1.6.0" ,
129
+ cliVersion : "1.5.0" ,
130
+ expectedResult : "1.5.0"
131
+ } ,
90
132
91
- let actualLatestCompatibleVersion = npmInstallationManager . getLatestCompatibleVersion ( "" ) . wait ( ) ;
92
- let expectedLatestCompatibleVersion = "1.3.3" ;
93
- assert . equal ( actualLatestCompatibleVersion , expectedLatestCompatibleVersion ) ;
94
- } ) ;
133
+ "when there are multiple versions latest one does not match CLI's semver and other versions are not matching either" : {
134
+ versions : [ "1.0.0" , "1.0.1" , "1.2.0" , "1.3.1" , "1.4.0" , "1.5.0-2016-02-25-182" , "1.5.0" ] ,
135
+ packageLatestVersion : "1.0.0" ,
136
+ cliVersion : "1.1.0" ,
137
+ expectedResult : "1.0.0"
138
+ }
139
+ } ;
95
140
96
- it ( "returns correct latest compatible version" , ( ) => {
97
- let testInjector = createTestInjector ( ) ;
141
+ _ . each ( testData , ( currentTestData : ITestData , testName : string ) => {
142
+ it ( `returns correct latest compatible version, ${ testName } ` , ( ) => {
143
+ let testInjector = createTestInjector ( ) ;
98
144
99
- let versions = [ "1.2.0" , "1.3.0" , "1.3.1" , "1.3.2" , "1.3.3" , "1.4.0" ] ;
100
- let latestVersion = _ . last ( versions ) ;
101
- mockNpm ( testInjector , versions , latestVersion ) ;
145
+ mockNpm ( testInjector , currentTestData . versions , currentTestData . packageLatestVersion ) ;
102
146
103
- // Mock staticConfig.version
104
- let staticConfig = testInjector . resolve ( "staticConfig" ) ;
105
- staticConfig . version = "1.5.0" ;
147
+ // Mock staticConfig.version
148
+ let staticConfig = testInjector . resolve ( "staticConfig" ) ;
149
+ staticConfig . version = currentTestData . cliVersion ;
106
150
107
- // Mock npmInstallationManager.getLatestVersion
108
- let npmInstallationManager = testInjector . resolve ( "npmInstallationManager" ) ;
109
- npmInstallationManager . getLatestVersion = ( packageName : string ) => Future . fromResult ( latestVersion ) ;
151
+ // Mock npmInstallationManager.getLatestVersion
152
+ let npmInstallationManager = testInjector . resolve ( "npmInstallationManager" ) ;
153
+ npmInstallationManager . getLatestVersion = ( packageName : string ) => Future . fromResult ( currentTestData . packageLatestVersion ) ;
110
154
111
- let actualLatestCompatibleVersion = npmInstallationManager . getLatestCompatibleVersion ( "" ) . wait ( ) ;
112
- assert . equal ( actualLatestCompatibleVersion , latestVersion ) ;
155
+ let actualLatestCompatibleVersion = npmInstallationManager . getLatestCompatibleVersion ( "" ) . wait ( ) ;
156
+ assert . equal ( actualLatestCompatibleVersion , currentTestData . expectedResult ) ;
157
+ } ) ;
113
158
} ) ;
114
159
} ) ;
0 commit comments