1
1
import { afterEach , describe , expect , it , vi } from 'vitest' ;
2
2
import { StoreServiceImpl } from '../store.service.js' ;
3
3
4
- const { mockDiskCacheService } = await vi . hoisted ( async ( ) => {
5
- const cacheServiceMockModule = await import (
6
- '../../cache/__mocks__/cache-service.mock.js'
7
- ) ;
4
+ const { diskCacheServiceConstructorSpy, mockDiskCacheServiceClass } =
5
+ await vi . hoisted ( async ( ) => {
6
+ const cacheServiceMockModule = await import (
7
+ '../../cache/__mocks__/cache-service.mock.js'
8
+ ) ;
8
9
9
- return {
10
- mockDiskCacheService : cacheServiceMockModule . CacheServiceMock ,
11
- } ;
12
- } ) ;
10
+ const diskCacheServiceConstructorSpy = vi . fn ( ) ;
11
+ const mockDiskCacheService = new cacheServiceMockModule . CacheServiceMock ( ) ;
13
12
14
- vi . mock ( '../../cache/disk-cache.service.js' , ( ) => {
15
- return { DiskCacheServiceImpl : mockDiskCacheService } ;
16
- } ) ;
13
+ return {
14
+ diskCacheServiceConstructorSpy,
15
+ mockDiskCacheServiceClass : class {
16
+ constructor ( ...args : any ) {
17
+ diskCacheServiceConstructorSpy ( ...args ) ;
18
+ return mockDiskCacheService ;
19
+ }
20
+ } ,
21
+ } ;
22
+ } ) ;
17
23
18
- vi . mock ( 'electron' , async ( ) => {
24
+ vi . mock ( '../../cache/disk-cache.service.js' , ( ) => {
19
25
return {
20
- app : {
21
- getPath : vi . fn ( ) . mockImplementation ( ( ) => 'userData' ) ,
22
- } ,
26
+ DiskCacheServiceImpl : mockDiskCacheServiceClass ,
23
27
} ;
24
28
} ) ;
25
29
@@ -29,8 +33,35 @@ describe('store-instance', () => {
29
33
vi . clearAllTimers ( ) ;
30
34
} ) ;
31
35
32
- it ( 'is a store service' , async ( ) => {
33
- const Store = ( await import ( '../store.instance.js' ) ) . Store ;
34
- expect ( Store ) . toBeInstanceOf ( StoreServiceImpl ) ;
36
+ describe ( '#getStoreForPath' , ( ) => {
37
+ it ( 'returns a store for a path' , async ( ) => {
38
+ const { getStoreForPath } = await import ( '../store.instance.js' ) ;
39
+
40
+ const store = getStoreForPath ( 'config.json' ) ;
41
+
42
+ expect ( store ) . toBeInstanceOf ( StoreServiceImpl ) ;
43
+
44
+ expect ( diskCacheServiceConstructorSpy ) . toHaveBeenCalledWith ( {
45
+ filepath : 'config.json' ,
46
+ } ) ;
47
+ } ) ;
48
+
49
+ it ( 'reuses stores for the same paths' , async ( ) => {
50
+ const { getStoreForPath } = await import ( '../store.instance.js' ) ;
51
+
52
+ const storeA = getStoreForPath ( 'config.json' ) ;
53
+ const storeB = getStoreForPath ( 'config.json' ) ;
54
+
55
+ expect ( storeA ) . toBe ( storeB ) ;
56
+ } ) ;
57
+
58
+ it ( 'creates new stores for different paths' , async ( ) => {
59
+ const { getStoreForPath } = await import ( '../store.instance.js' ) ;
60
+
61
+ const storeA = getStoreForPath ( 'pathA.json' ) ;
62
+ const storeB = getStoreForPath ( 'pathB.json' ) ;
63
+
64
+ expect ( storeA ) . not . toBe ( storeB ) ;
65
+ } ) ;
35
66
} ) ;
36
67
} ) ;
0 commit comments