@@ -129,6 +129,110 @@ describe('getConfiguration', () => {
129
129
} )
130
130
} )
131
131
132
+ describe ( 'when config exists in parent directory' , ( ) => {
133
+ const originalCwd = process . cwd
134
+
135
+ beforeEach ( ( ) => {
136
+ jest . resetModules ( )
137
+ } )
138
+
139
+ beforeAll ( ( ) => {
140
+ process . cwd = jest . fn ( ( ) => '/home/user/project/subfolder' )
141
+
142
+ pathExistsSync . mockImplementation ( ( path ) => {
143
+ return path . includes ( '/home/user/project/.gitmojirc.json' )
144
+ } )
145
+
146
+ readFileSync . mockImplementation ( ( path ) => {
147
+ if ( path . includes ( '/home/user/project/.gitmojirc.json' ) ) {
148
+ return JSON . stringify ( { from : 'parent' } )
149
+ }
150
+ return ''
151
+ } )
152
+ } )
153
+
154
+ afterAll ( ( ) => {
155
+ process . cwd = originalCwd
156
+ } )
157
+
158
+ it ( 'should load config from parent directory' , ( ) => {
159
+ const configuration = getConfiguration ( )
160
+ expect ( configuration . get ( 'from' ) ) . toEqual ( 'parent' )
161
+ } )
162
+ } )
163
+
164
+ describe ( 'when config exists in grandparent directory' , ( ) => {
165
+ const originalCwd = process . cwd
166
+
167
+ beforeEach ( ( ) => {
168
+ jest . resetModules ( )
169
+ } )
170
+
171
+ beforeAll ( ( ) => {
172
+ process . cwd = jest . fn ( ( ) => '/home/user/project/subfolder' )
173
+
174
+ pathExistsSync . mockImplementation ( ( path ) => {
175
+ return path . includes ( '/home/user/.gitmojirc.json' )
176
+ } )
177
+
178
+ readFileSync . mockImplementation ( ( path ) => {
179
+ if ( path . includes ( '/home/user/.gitmojirc.json' ) ) {
180
+ return JSON . stringify ( { from : 'grandparent' } )
181
+ }
182
+ return ''
183
+ } )
184
+ } )
185
+
186
+ afterAll ( ( ) => {
187
+ process . cwd = originalCwd
188
+ } )
189
+
190
+ it ( 'should load config from grandparent directory' , ( ) => {
191
+ const configuration = getConfiguration ( )
192
+ expect ( configuration . get ( 'from' ) ) . toEqual ( 'grandparent' )
193
+ } )
194
+ } )
195
+
196
+ describe ( 'when config exists in current and parent directory' , ( ) => {
197
+ const originalCwd = process . cwd
198
+
199
+ beforeEach ( ( ) => {
200
+ jest . resetModules ( )
201
+ } )
202
+
203
+ beforeAll ( ( ) => {
204
+ process . cwd = jest . fn ( ( ) => '/home/user/project/subfolder' )
205
+
206
+ pathExistsSync . mockImplementation ( ( path ) => {
207
+ return (
208
+ path . includes ( '/home/user/project/subfolder/.gitmojirc.json' ) ||
209
+ path . includes ( '/home/user/project/.gitmojirc.json' )
210
+ )
211
+ } )
212
+
213
+ readFileSync . mockImplementation ( ( path ) => {
214
+ if ( path . includes ( '/home/user/project/subfolder/.gitmojirc.json' ) ) {
215
+ return JSON . stringify ( { from : 'current' } )
216
+ }
217
+
218
+ if ( path . includes ( '/home/user/project/.gitmojirc.json' ) ) {
219
+ return JSON . stringify ( { from : 'parent' } )
220
+ }
221
+
222
+ return ''
223
+ } )
224
+ } )
225
+
226
+ afterAll ( ( ) => {
227
+ process . cwd = originalCwd
228
+ } )
229
+
230
+ it ( 'should prefer config from current directory over parent' , ( ) => {
231
+ const configuration = getConfiguration ( )
232
+ expect ( configuration . get ( 'from' ) ) . toEqual ( 'current' )
233
+ } )
234
+ } )
235
+
132
236
describe ( 'when package.json and .gitmojirc are not available' , ( ) => {
133
237
beforeAll ( ( ) => {
134
238
pathExistsSync . mockReturnValue ( false )
0 commit comments