@@ -2,22 +2,26 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22import type { SlimPackument , InstallSizeResult } from '#shared/types'
33
44function createPackage (
5+ name : string ,
56 time : Record < string , string > ,
67 distTags : Record < string , string > = { } ,
78) : SlimPackument {
89 return {
9- '_id' : 'test' ,
10- 'name' : 'test' ,
10+ '_id' : name ,
11+ 'name' : name ,
1112 'dist-tags' : { latest : '1.0.0' , ...distTags } ,
1213 'time' : { created : '2020-01-01' , ...time } ,
1314 'versions' : { } ,
1415 'requestedVersion' : null ,
1516 } as SlimPackument
1617}
1718
18- function createInstallSize ( overrides : Partial < InstallSizeResult > = { } ) : InstallSizeResult {
19+ function createInstallSize (
20+ name : string ,
21+ overrides : Partial < InstallSizeResult > = { } ,
22+ ) : InstallSizeResult {
1923 return {
20- package : 'test' ,
24+ package : name ,
2125 version : '1.0.0' ,
2226 selfSize : 1000 ,
2327 totalSize : 5000 ,
@@ -31,7 +35,7 @@ describe('useInstallSizeDiff', () => {
3135 let fetchSpy : ReturnType < typeof vi . fn >
3236
3337 beforeEach ( ( ) => {
34- fetchSpy = vi . fn ( ) . mockResolvedValue ( createInstallSize ( ) )
38+ fetchSpy = vi . fn ( ) . mockResolvedValue ( createInstallSize ( 'test' ) )
3539 vi . stubGlobal ( '$fetch' , fetchSpy )
3640 } )
3741
@@ -41,7 +45,7 @@ describe('useInstallSizeDiff', () => {
4145
4246 describe ( 'comparisonVersion' , ( ) => {
4347 it ( 'selects the semver-previous stable version' , ( ) => {
44- const pkg = createPackage ( {
48+ const pkg = createPackage ( 'test' , {
4549 '1.0.0' : '2020-01-01' ,
4650 '1.1.0' : '2021-01-01' ,
4751 '1.2.0' : '2022-01-01' ,
@@ -52,14 +56,14 @@ describe('useInstallSizeDiff', () => {
5256 } )
5357
5458 it ( 'returns null for the first ever stable version' , ( ) => {
55- const pkg = createPackage ( { '1.0.0' : '2020-01-01' } )
59+ const pkg = createPackage ( 'test' , { '1.0.0' : '2020-01-01' } )
5660
5761 const { comparisonVersion } = useInstallSizeDiff ( 'test' , '1.0.0' , pkg , null )
5862 expect ( comparisonVersion . value ) . toBeNull ( )
5963 } )
6064
6165 it ( 'skips prerelease versions when finding previous stable' , ( ) => {
62- const pkg = createPackage ( {
66+ const pkg = createPackage ( 'test' , {
6367 '1.0.0' : '2020-01-01' ,
6468 '2.0.0-beta.1' : '2021-01-01' ,
6569 '2.0.0-beta.2' : '2021-06-01' ,
@@ -72,6 +76,7 @@ describe('useInstallSizeDiff', () => {
7276
7377 it ( 'uses the latest dist-tag for a prerelease version' , ( ) => {
7478 const pkg = createPackage (
79+ 'test' ,
7580 { '1.0.0' : '2020-01-01' , '2.0.0-beta.1' : '2021-01-01' } ,
7681 { latest : '1.0.0' , next : '2.0.0-beta.1' } ,
7782 )
@@ -81,14 +86,18 @@ describe('useInstallSizeDiff', () => {
8186 } )
8287
8388 it ( 'returns null when the prerelease version is already latest' , ( ) => {
84- const pkg = createPackage ( { '2.0.0-beta.1' : '2021-01-01' } , { latest : '2.0.0-beta.1' } )
89+ const pkg = createPackage (
90+ 'test' ,
91+ { '2.0.0-beta.1' : '2021-01-01' } ,
92+ { latest : '2.0.0-beta.1' } ,
93+ )
8594
8695 const { comparisonVersion } = useInstallSizeDiff ( 'test' , '2.0.0-beta.1' , pkg , null )
8796 expect ( comparisonVersion . value ) . toBeNull ( )
8897 } )
8998
9099 it ( 'returns null for a prerelease when there is no latest tag' , ( ) => {
91- const pkg = createPackage ( { '2.0.0-beta.1' : '2021-01-01' } , { } )
100+ const pkg = createPackage ( 'test' , { '2.0.0-beta.1' : '2021-01-01' } , { } )
92101 pkg [ 'dist-tags' ] = { }
93102
94103 const { comparisonVersion } = useInstallSizeDiff ( 'test' , '2.0.0-beta.1' , pkg , null )
@@ -98,8 +107,8 @@ describe('useInstallSizeDiff', () => {
98107
99108 describe ( 'diff' , ( ) => {
100109 it ( 'returns null when no comparison version exists' , ( ) => {
101- const pkg = createPackage ( { '1.0.0' : '2020-01-01' } )
102- const current = createInstallSize ( { version : '1.0.0' } )
110+ const pkg = createPackage ( 'test' , { '1.0.0' : '2020-01-01' } )
111+ const current = createInstallSize ( 'test' , { version : '1.0.0' } )
103112
104113 const { diff } = useInstallSizeDiff ( 'test' , '1.0.0' , pkg , current )
105114
@@ -108,10 +117,21 @@ describe('useInstallSizeDiff', () => {
108117 } )
109118
110119 it ( 'returns null when both thresholds are not met' , async ( ) => {
111- const pkg = createPackage ( { '1.0.0' : '2020-01-01' , '1.1.0' : '2021-01-01' } )
112- const current = createInstallSize ( { version : '1.1.0' , totalSize : 5100 , dependencyCount : 4 } )
120+ const pkg = createPackage ( 'pkg-no-threshold' , {
121+ '1.0.0' : '2020-01-01' ,
122+ '1.1.0' : '2021-01-01' ,
123+ } )
124+ const current = createInstallSize ( 'pkg-no-threshold' , {
125+ version : '1.1.0' ,
126+ totalSize : 5100 ,
127+ dependencyCount : 4 ,
128+ } )
113129 fetchSpy . mockResolvedValue (
114- createInstallSize ( { version : '1.0.0' , totalSize : 5000 , dependencyCount : 3 } ) ,
130+ createInstallSize ( 'pkg-no-threshold' , {
131+ version : '1.0.0' ,
132+ totalSize : 5000 ,
133+ dependencyCount : 3 ,
134+ } ) ,
115135 )
116136
117137 const { diff } = useInstallSizeDiff ( 'pkg-no-threshold' , '1.1.0' , pkg , current )
@@ -121,10 +141,18 @@ describe('useInstallSizeDiff', () => {
121141 } )
122142
123143 it ( 'sets sizeThresholdExceeded when size increased by more than 25%' , async ( ) => {
124- const pkg = createPackage ( { '1.0.0' : '2020-01-01' , '1.1.0' : '2021-01-01' } )
125- const current = createInstallSize ( { version : '1.1.0' , totalSize : 7000 , dependencyCount : 4 } )
144+ const pkg = createPackage ( 'pkg-size-only' , { '1.0.0' : '2020-01-01' , '1.1.0' : '2021-01-01' } )
145+ const current = createInstallSize ( 'pkg-size-only' , {
146+ version : '1.1.0' ,
147+ totalSize : 7000 ,
148+ dependencyCount : 4 ,
149+ } )
126150 fetchSpy . mockResolvedValue (
127- createInstallSize ( { version : '1.0.0' , totalSize : 5000 , dependencyCount : 3 } ) ,
151+ createInstallSize ( 'pkg-size-only' , {
152+ version : '1.0.0' ,
153+ totalSize : 5000 ,
154+ dependencyCount : 3 ,
155+ } ) ,
128156 )
129157
130158 const { diff } = useInstallSizeDiff ( 'pkg-size-only' , '1.1.0' , pkg , current )
@@ -145,10 +173,18 @@ describe('useInstallSizeDiff', () => {
145173 } )
146174
147175 it ( 'sets depThresholdExceeded when more than 5 dependencies were added' , async ( ) => {
148- const pkg = createPackage ( { '1.0.0' : '2020-01-01' , '1.1.0' : '2021-01-01' } )
149- const current = createInstallSize ( { version : '1.1.0' , totalSize : 5100 , dependencyCount : 10 } )
176+ const pkg = createPackage ( 'pkg-deps-only' , { '1.0.0' : '2020-01-01' , '1.1.0' : '2021-01-01' } )
177+ const current = createInstallSize ( 'pkg-deps-only' , {
178+ version : '1.1.0' ,
179+ totalSize : 5100 ,
180+ dependencyCount : 10 ,
181+ } )
150182 fetchSpy . mockResolvedValue (
151- createInstallSize ( { version : '1.0.0' , totalSize : 5000 , dependencyCount : 3 } ) ,
183+ createInstallSize ( 'pkg-deps-only' , {
184+ version : '1.0.0' ,
185+ totalSize : 5000 ,
186+ dependencyCount : 3 ,
187+ } ) ,
152188 )
153189
154190 const { diff } = useInstallSizeDiff ( 'pkg-deps-only' , '1.1.0' , pkg , current )
@@ -169,10 +205,14 @@ describe('useInstallSizeDiff', () => {
169205 } )
170206
171207 it ( 'returns correct diff values' , async ( ) => {
172- const pkg = createPackage ( { '1.0.0' : '2020-01-01' , '1.1.0' : '2021-01-01' } )
173- const current = createInstallSize ( { version : '1.1.0' , totalSize : 10000 , dependencyCount : 15 } )
208+ const pkg = createPackage ( 'pkg-both' , { '1.0.0' : '2020-01-01' , '1.1.0' : '2021-01-01' } )
209+ const current = createInstallSize ( 'pkg-both' , {
210+ version : '1.1.0' ,
211+ totalSize : 10000 ,
212+ dependencyCount : 15 ,
213+ } )
174214 fetchSpy . mockResolvedValue (
175- createInstallSize ( { version : '1.0.0' , totalSize : 5000 , dependencyCount : 5 } ) ,
215+ createInstallSize ( 'pkg-both' , { version : '1.0.0' , totalSize : 5000 , dependencyCount : 5 } ) ,
176216 )
177217
178218 const { diff } = useInstallSizeDiff ( 'pkg-both' , '1.1.0' , pkg , current )
@@ -195,9 +235,9 @@ describe('useInstallSizeDiff', () => {
195235
196236 describe ( 'fetch behavior' , ( ) => {
197237 it ( 'calls the correct API endpoint for the comparison version' , async ( ) => {
198- const pkg = createPackage ( { '1.0.0' : '2020-01-01' , '1.1.0' : '2021-01-01' } )
199- const current = createInstallSize ( { version : '1.1.0' , totalSize : 7000 } )
200- fetchSpy . mockResolvedValue ( createInstallSize ( { version : '1.0.0' , totalSize : 5000 } ) )
238+ const pkg = createPackage ( 'my-pkg' , { '1.0.0' : '2020-01-01' , '1.1.0' : '2021-01-01' } )
239+ const current = createInstallSize ( 'my-pkg' , { version : '1.1.0' , totalSize : 7000 } )
240+ fetchSpy . mockResolvedValue ( createInstallSize ( 'my-pkg' , { version : '1.0.0' , totalSize : 5000 } ) )
201241
202242 useInstallSizeDiff ( 'my-pkg' , '1.1.0' , pkg , current )
203243
@@ -206,7 +246,7 @@ describe('useInstallSizeDiff', () => {
206246 } )
207247
208248 it ( 'does not fetch when there is no comparison version' , ( ) => {
209- const pkg = createPackage ( { '1.0.0' : '2020-01-01' } )
249+ const pkg = createPackage ( 'my-pkg' , { '1.0.0' : '2020-01-01' } )
210250
211251 useInstallSizeDiff ( 'my-pkg' , '1.0.0' , pkg , null )
212252
0 commit comments