Skip to content

Commit cdf7245

Browse files
committed
test: add missing test cases
1 parent af81566 commit cdf7245

1 file changed

Lines changed: 154 additions & 0 deletions

File tree

test/nuxt/composables/use-install-size-diff.spec.ts

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ describe('useInstallSizeDiff', () => {
182182

183183
await vi.waitFor(() => expect(diff.value).not.toBeNull())
184184
expect(diff.value).toEqual({
185+
direction: 'increase',
185186
comparisonVersion: '1.0.0',
186187
sizeRatio: 0.4,
187188
sizeIncrease: 2000,
@@ -218,6 +219,7 @@ describe('useInstallSizeDiff', () => {
218219

219220
await vi.waitFor(() => expect(diff.value).not.toBeNull())
220221
expect(diff.value).toEqual({
222+
direction: 'increase',
221223
comparisonVersion: '1.0.0',
222224
sizeRatio: 0.02,
223225
sizeIncrease: 100,
@@ -250,6 +252,7 @@ describe('useInstallSizeDiff', () => {
250252

251253
await vi.waitFor(() => expect(diff.value).not.toBeNull())
252254
expect(diff.value).toEqual({
255+
direction: 'increase',
253256
comparisonVersion: '1.0.0',
254257
sizeRatio: 1, // 100% increase
255258
sizeIncrease: 5000,
@@ -262,6 +265,157 @@ describe('useInstallSizeDiff', () => {
262265
depThresholdExceeded: true,
263266
})
264267
})
268+
269+
it('reports a decrease when size reduced, deps remained', async () => {
270+
const pkg = createPackage('pkg-size-win', {
271+
'0.9.0': '2019-01-01',
272+
'1.0.0': '2020-01-01',
273+
'1.1.0': '2021-01-01',
274+
})
275+
const current = createInstallSize('pkg-size-win', {
276+
version: '1.1.0',
277+
totalSize: 3000,
278+
dependencyCount: 3,
279+
})
280+
fetchSpy.mockResolvedValue(
281+
createInstallSize('pkg-size-win', {
282+
version: '1.0.0',
283+
totalSize: 5000,
284+
dependencyCount: 3,
285+
}),
286+
)
287+
288+
const { diff } = useInstallSizeDiff('pkg-size-win', '1.1.0', pkg, current)
289+
290+
await vi.waitFor(() => expect(diff.value).not.toBeNull())
291+
expect(diff.value).toEqual({
292+
direction: 'decrease',
293+
comparisonVersion: '1.0.0',
294+
sizeRatio: -0.4,
295+
sizeIncrease: -2000,
296+
currentSize: 3000,
297+
previousSize: 5000,
298+
depDiff: 0,
299+
currentDeps: 3,
300+
previousDeps: 3,
301+
sizeThresholdExceeded: true,
302+
depThresholdExceeded: false,
303+
})
304+
})
305+
306+
it('reports a decrease when deps reduced, size remained', async () => {
307+
const pkg = createPackage('pkg-deps-win', {
308+
'0.9.0': '2019-01-01',
309+
'1.0.0': '2020-01-01',
310+
'1.1.0': '2021-01-01',
311+
})
312+
const current = createInstallSize('pkg-deps-win', {
313+
version: '1.1.0',
314+
totalSize: 5000,
315+
dependencyCount: 3,
316+
})
317+
fetchSpy.mockResolvedValue(
318+
createInstallSize('pkg-deps-win', {
319+
version: '1.0.0',
320+
totalSize: 5000,
321+
dependencyCount: 10,
322+
}),
323+
)
324+
325+
const { diff } = useInstallSizeDiff('pkg-deps-win', '1.1.0', pkg, current)
326+
327+
await vi.waitFor(() => expect(diff.value).not.toBeNull())
328+
expect(diff.value).toEqual({
329+
direction: 'decrease',
330+
comparisonVersion: '1.0.0',
331+
sizeRatio: 0,
332+
sizeIncrease: 0,
333+
currentSize: 5000,
334+
previousSize: 5000,
335+
depDiff: -7,
336+
currentDeps: 3,
337+
previousDeps: 10,
338+
sizeThresholdExceeded: false,
339+
depThresholdExceeded: true,
340+
})
341+
})
342+
343+
it('does not celebrate when any metric increased', async () => {
344+
const pkg = createPackage('pkg-mixed', {
345+
'0.9.0': '2019-01-01',
346+
'1.0.0': '2020-01-01',
347+
'1.1.0': '2021-01-01',
348+
})
349+
const current = createInstallSize('pkg-mixed', {
350+
version: '1.1.0',
351+
totalSize: 5500,
352+
dependencyCount: 3,
353+
})
354+
fetchSpy.mockResolvedValue(
355+
createInstallSize('pkg-mixed', {
356+
version: '1.0.0',
357+
totalSize: 5000,
358+
dependencyCount: 10,
359+
}),
360+
)
361+
362+
const { diff } = useInstallSizeDiff('pkg-mixed', '1.1.0', pkg, current)
363+
364+
await vi.waitFor(() => expect(fetchSpy).toHaveBeenCalled())
365+
expect(diff.value).toBeNull()
366+
})
367+
368+
it('reports increases even when the other metric decreased', async () => {
369+
const pkg = createPackage('pkg-size-up-deps-down', {
370+
'0.9.0': '2019-01-01',
371+
'1.0.0': '2020-01-01',
372+
'1.1.0': '2021-01-01',
373+
})
374+
const current = createInstallSize('pkg-size-up-deps-down', {
375+
version: '1.1.0',
376+
totalSize: 7000,
377+
dependencyCount: 3,
378+
})
379+
fetchSpy.mockResolvedValue(
380+
createInstallSize('pkg-size-up-deps-down', {
381+
version: '1.0.0',
382+
totalSize: 5000,
383+
dependencyCount: 10,
384+
}),
385+
)
386+
387+
const { diff } = useInstallSizeDiff('pkg-size-up-deps-down', '1.1.0', pkg, current)
388+
389+
await vi.waitFor(() => expect(diff.value).not.toBeNull())
390+
expect(diff.value?.direction).toBe('increase')
391+
expect(diff.value?.sizeThresholdExceeded).toBe(true)
392+
expect(diff.value?.depThresholdExceeded).toBe(false)
393+
})
394+
395+
it('returns null when decreases are below the decrease threshold', async () => {
396+
const pkg = createPackage('pkg-small-wins', {
397+
'0.9.0': '2019-01-01',
398+
'1.0.0': '2020-01-01',
399+
'1.1.0': '2021-01-01',
400+
})
401+
const current = createInstallSize('pkg-small-wins', {
402+
version: '1.1.0',
403+
totalSize: 4500,
404+
dependencyCount: 3,
405+
})
406+
fetchSpy.mockResolvedValue(
407+
createInstallSize('pkg-small-wins', {
408+
version: '1.0.0',
409+
totalSize: 5000,
410+
dependencyCount: 5,
411+
}),
412+
)
413+
414+
const { diff } = useInstallSizeDiff('pkg-small-wins', '1.1.0', pkg, current)
415+
416+
await vi.waitFor(() => expect(fetchSpy).toHaveBeenCalled())
417+
expect(diff.value).toBeNull()
418+
})
265419
})
266420

267421
describe('fetch behavior', () => {

0 commit comments

Comments
 (0)