@@ -10,28 +10,26 @@ import EntryItemRepository from './entry-item-repository'
10
10
import ItemNotFound from '../../../exceptions/item-not-found'
11
11
12
12
test . group ( 'entry-item-repository (repository)' , ( group ) => {
13
- const memory = new InMemoryDrive ( )
14
-
15
- const manager = new DriveManager ( { memory } , 'memory' )
16
-
17
- const repository = new EntryItemRepository ( manager )
13
+ const drive = new InMemoryDrive ( )
18
14
19
15
const collection = CollectionFactory . create ( )
20
16
17
+ const repository = new EntryItemRepository ( collection , drive )
18
+
21
19
function saveMetas ( metas : any [ ] ) {
22
- memory . createFile ( DirectoryEntry . normalize ( collection . path , '.is' , 'metas.json' ) , metas )
20
+ drive . createFile ( DirectoryEntry . normalize ( collection . path , '.is' , 'metas.json' ) , metas )
23
21
}
24
22
25
23
function getMetas ( ) {
26
- return memory . readArray ( DirectoryEntry . normalize ( collection . path , '.is' , 'metas.json' ) )
24
+ return drive . readArray ( DirectoryEntry . normalize ( collection . path , '.is' , 'metas.json' ) )
27
25
}
28
26
29
- group . each . teardown ( ( ) => memory . clear ( ) )
27
+ group . each . teardown ( ( ) => drive . clear ( ) )
30
28
31
29
test ( 'should list entries as items' , async ( { expect } ) => {
32
30
const items = Array . from ( { length : 20 } )
33
31
. map ( ( _ , i ) => String ( i ) )
34
- . map ( ( i ) => memory . createDir ( collection . path , i ) )
32
+ . map ( ( i ) => drive . createDir ( collection . path , i ) )
35
33
. map ( ( { name } ) => {
36
34
const data = {
37
35
gender : faker . name . gender ( ) ,
@@ -43,15 +41,15 @@ test.group('entry-item-repository (repository)', (group) => {
43
41
44
42
saveMetas ( items )
45
43
46
- const result = await repository . list ( collection )
44
+ const result = await repository . list ( )
47
45
48
46
expect ( result ) . toEqual ( items )
49
47
} )
50
48
51
49
test ( 'should show method return a item by id' , async ( { expect } ) => {
52
- const entry = memory . createDir ( collection . path , 'hello-word' )
50
+ const entry = drive . createDir ( collection . path , 'hello-word' )
53
51
54
- const result = await repository . show ( collection , entry . name )
52
+ const result = await repository . show ( entry . name )
55
53
56
54
expect ( result ) . toEqual ( new Item ( { } , entry . name ) )
57
55
} )
@@ -60,46 +58,45 @@ test.group('entry-item-repository (repository)', (group) => {
60
58
expect . assertions ( 1 )
61
59
62
60
await repository
63
- . show ( collection , 'invalid' )
61
+ . show ( 'invalid' )
64
62
. catch ( ( err ) => expect ( err ) . toEqual ( new ItemNotFound ( collection . path , 'invalid' ) ) )
65
63
} )
66
64
67
65
test ( 'should create a item' , async ( { expect } ) => {
68
66
const item = new Item ( { age : faker . random . numeric ( ) } , 'hello' )
69
67
70
- await repository . create ( collection , item )
68
+ await repository . create ( item )
71
69
72
- const [ result ] = await repository . list ( collection )
70
+ const [ result ] = await repository . list ( )
73
71
74
72
expect ( result . id ) . toEqual ( item . id )
75
73
expect ( result . age ) . toEqual ( item . age )
76
74
77
- expect ( manager . exists ( collection . path , 'hello' ) )
75
+ const folderExists = await drive . exists ( DirectoryEntry . normalize ( collection . path , 'hello' ) )
76
+
77
+ expect ( folderExists ) . toBe ( true )
78
78
} )
79
79
80
80
test ( 'should update method throw an error if item not exists' , async ( { expect } ) => {
81
81
expect . assertions ( 1 )
82
82
83
83
await repository
84
- . update ( collection , 'invalid' , {
84
+ . update ( 'invalid' , {
85
85
hello : 'word' ,
86
86
} )
87
87
. catch ( ( err ) => expect ( err ) . toEqual ( new ItemNotFound ( collection . path , 'invalid' ) ) )
88
88
} )
89
89
90
90
test ( 'should update a item' , async ( { expect } ) => {
91
- const item = await repository . create (
92
- collection ,
93
- new Item ( { age : faker . random . numeric ( ) } , 'hello' )
94
- )
91
+ const item = await repository . create ( new Item ( { age : faker . random . numeric ( ) } , 'hello' ) )
95
92
96
93
saveMetas ( [ item ] )
97
94
98
- await repository . update ( collection , item . id , {
95
+ await repository . update ( item . id , {
99
96
hello : 'word' ,
100
97
} )
101
98
102
- const [ result ] = await repository . list ( collection )
99
+ const [ result ] = await repository . list ( )
103
100
104
101
expect ( result . id ) . toBe ( item . id )
105
102
expect ( result . age ) . toBe ( item . age )
@@ -109,20 +106,19 @@ test.group('entry-item-repository (repository)', (group) => {
109
106
} )
110
107
111
108
test ( 'should update method move item folder when id is defined' , async ( { expect } ) => {
112
- const item = await repository . create (
113
- collection ,
114
- new Item ( { age : faker . random . numeric ( ) } , 'hello' )
115
- )
109
+ const item = await repository . create ( new Item ( { age : faker . random . numeric ( ) } , 'hello' ) )
116
110
117
111
saveMetas ( [ item ] )
118
112
119
- await repository . update ( collection , item . id , {
113
+ await repository . update ( item . id , {
120
114
id : 'new-dir' ,
121
115
} )
122
116
123
- const newItem = await repository . show ( collection , 'new-dir' )
117
+ const newItem = await repository . show ( 'new-dir' )
124
118
125
- const folderExists = await manager . exists ( collection . path , 'new-dir' )
119
+ const folderExists = await drive . exists (
120
+ DirectoryEntry . normalize ( collection . path , 'new-dir' )
121
+ )
126
122
127
123
expect ( folderExists ) . toBe ( true )
128
124
expect ( newItem . age ) . toBe ( item . age )
@@ -132,21 +128,18 @@ test.group('entry-item-repository (repository)', (group) => {
132
128
expect . assertions ( 1 )
133
129
134
130
await repository
135
- . destroy ( collection , 'invalid' )
131
+ . destroy ( 'invalid' )
136
132
. catch ( ( err ) => expect ( err ) . toEqual ( new ItemNotFound ( collection . path , 'invalid' ) ) )
137
133
} )
138
134
139
135
test ( 'should delete an item' , async ( { expect } ) => {
140
- const item = await repository . create (
141
- collection ,
142
- new Item ( { age : faker . random . numeric ( ) } , 'hello' )
143
- )
136
+ const item = await repository . create ( new Item ( { age : faker . random . numeric ( ) } , 'hello' ) )
144
137
145
138
saveMetas ( [ item ] )
146
139
147
- await repository . destroy ( collection , item . id )
140
+ await repository . destroy ( item . id )
148
141
149
- const items = await repository . list ( collection )
142
+ const items = await repository . list ( )
150
143
const metas = await getMetas ( )
151
144
152
145
expect ( items . length ) . toBe ( 0 )
0 commit comments