1- import { beforeEach , describe , expect , it , vi } from 'vitest'
1+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
22import { ref } from 'vue'
33
44import type { RecentItem } from '../../../../app/composables/useRecentlyViewed'
@@ -10,44 +10,47 @@ vi.mock('@vueuse/core', () => ({
1010} ) )
1111
1212// Must import after mock is set up
13- const { trackRecentView, useRecentlyViewed } =
14- await import ( '../../../../app/composables/useRecentlyViewed' )
13+ const { useRecentlyViewed } = await import ( '../../../../app/composables/useRecentlyViewed' )
1514
1615describe ( 'useRecentlyViewed' , ( ) => {
1716 beforeEach ( ( ) => {
1817 storageRef . value = [ ]
1918 } )
2019
20+ afterEach ( ( ) => {
21+ vi . restoreAllMocks ( )
22+ } )
23+
2124 it ( 'returns an empty list initially' , ( ) => {
2225 const { items } = useRecentlyViewed ( )
2326 expect ( items . value ) . toEqual ( [ ] )
2427 } )
2528
2629 it ( 'adds an item to an empty list' , ( ) => {
30+ const { trackRecentView, items } = useRecentlyViewed ( )
2731 trackRecentView ( { type : 'package' , name : 'vue' , label : 'vue' } )
28- const { items } = useRecentlyViewed ( )
2932 expect ( items . value ) . toHaveLength ( 1 )
3033 expect ( items . value [ 0 ] ) . toMatchObject ( { type : 'package' , name : 'vue' , label : 'vue' } )
3134 expect ( items . value [ 0 ] ! . viewedAt ) . toBeTypeOf ( 'number' )
3235 } )
3336
3437 it ( 'deduplicates by type and name, bumping to front' , ( ) => {
38+ const { trackRecentView, items } = useRecentlyViewed ( )
3539 trackRecentView ( { type : 'package' , name : 'vue' , label : 'vue' } )
3640 trackRecentView ( { type : 'package' , name : 'react' , label : 'react' } )
3741 trackRecentView ( { type : 'package' , name : 'vue' , label : 'vue' } )
3842
39- const { items } = useRecentlyViewed ( )
4043 expect ( items . value ) . toHaveLength ( 2 )
4144 expect ( items . value [ 0 ] ! . name ) . toBe ( 'vue' )
4245 expect ( items . value [ 1 ] ! . name ) . toBe ( 'react' )
4346 } )
4447
4548 it ( 'caps at 5 items, evicting the oldest' , ( ) => {
49+ const { trackRecentView, items } = useRecentlyViewed ( )
4650 for ( let i = 1 ; i <= 6 ; i ++ ) {
4751 trackRecentView ( { type : 'package' , name : `pkg-${ i } ` , label : `pkg-${ i } ` } )
4852 }
4953
50- const { items } = useRecentlyViewed ( )
5154 expect ( items . value ) . toHaveLength ( 5 )
5255 expect ( items . value [ 0 ] ! . name ) . toBe ( 'pkg-6' )
5356 expect ( items . value [ 4 ] ! . name ) . toBe ( 'pkg-2' )
@@ -56,42 +59,42 @@ describe('useRecentlyViewed', () => {
5659 } )
5760
5861 it ( 'allows different entity types to coexist' , ( ) => {
62+ const { trackRecentView, items } = useRecentlyViewed ( )
5963 trackRecentView ( { type : 'package' , name : 'vue' , label : 'vue' } )
6064 trackRecentView ( { type : 'org' , name : 'nuxt' , label : '@nuxt' } )
6165 trackRecentView ( { type : 'user' , name : 'sindresorhus' , label : '~sindresorhus' } )
6266
63- const { items } = useRecentlyViewed ( )
6467 expect ( items . value ) . toHaveLength ( 3 )
6568 expect ( items . value . map ( i => i . type ) ) . toEqual ( [ 'user' , 'org' , 'package' ] )
6669 } )
6770
6871 it ( 'does not deduplicate items with the same name but different type' , ( ) => {
72+ const { trackRecentView, items } = useRecentlyViewed ( )
6973 trackRecentView ( { type : 'package' , name : 'nuxt' , label : 'nuxt' } )
7074 trackRecentView ( { type : 'org' , name : 'nuxt' , label : '@nuxt' } )
7175
72- const { items } = useRecentlyViewed ( )
7376 expect ( items . value ) . toHaveLength ( 2 )
7477 } )
7578
7679 it ( 'sets viewedAt on new entries' , ( ) => {
80+ const { trackRecentView, items } = useRecentlyViewed ( )
7781 const before = Date . now ( )
7882 trackRecentView ( { type : 'package' , name : 'vue' , label : 'vue' } )
7983 const after = Date . now ( )
8084
81- const { items } = useRecentlyViewed ( )
8285 expect ( items . value [ 0 ] ! . viewedAt ) . toBeGreaterThanOrEqual ( before )
8386 expect ( items . value [ 0 ] ! . viewedAt ) . toBeLessThanOrEqual ( after )
8487 } )
8588
8689 it ( 'updates viewedAt when deduplicating' , ( ) => {
90+ const { trackRecentView, items } = useRecentlyViewed ( )
8791 trackRecentView ( { type : 'package' , name : 'vue' , label : 'vue' } )
88- const firstViewedAt = useRecentlyViewed ( ) . items . value [ 0 ] ! . viewedAt
92+ const firstViewedAt = items . value [ 0 ] ! . viewedAt
8993
9094 // Small delay to ensure timestamp differs
9195 vi . spyOn ( Date , 'now' ) . mockReturnValueOnce ( firstViewedAt + 1000 )
9296 trackRecentView ( { type : 'package' , name : 'vue' , label : 'vue' } )
9397
94- const { items } = useRecentlyViewed ( )
9598 expect ( items . value [ 0 ] ! . viewedAt ) . toBeGreaterThan ( firstViewedAt )
9699 } )
97100} )
0 commit comments