Skip to content

Commit d12d51a

Browse files
NalinDalalAdi-204
andauthored
test: add unit tests for getDirElementsRecursive (#1679)
Co-authored-by: Nalin <nalindalal2004@gmail.com> Co-authored-by: Adi Boghawala <adiboghawala@gmail.com>
1 parent e5b8169 commit d12d51a

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

packages/helpers/test/testing.test.js

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { listFiles, buildParams, hasNestedConfig, cleanTestResultPaths } = require('@asyncapi/generator-helpers');
1+
const { listFiles, buildParams, hasNestedConfig, cleanTestResultPaths, getDirElementsRecursive } = require('@asyncapi/generator-helpers');
2+
const path = require('path');
23
const { rm, readdir } = require('fs/promises');
34

45
jest.mock('fs/promises', () => ({
@@ -202,4 +203,85 @@ describe('buildParams', () => {
202203
clientFileName: 'client.js',
203204
});
204205
});
205-
});
206+
});
207+
208+
describe('getDirElementsRecursive', () => {
209+
beforeEach(() => {
210+
jest.clearAllMocks();
211+
});
212+
213+
it('should return files and directories recursively', async () => {
214+
// Mock directory structure
215+
readdir
216+
.mockResolvedValueOnce([
217+
{ name: 'file1.txt', isDirectory: () => false, isFile: () => true },
218+
{ name: 'subdir', isDirectory: () => true, isFile: () => false }
219+
])
220+
.mockResolvedValueOnce([
221+
{ name: 'file2.js', isDirectory: () => false, isFile: () => true }
222+
]);
223+
224+
const result = await getDirElementsRecursive('/root');
225+
expect(result).toEqual([
226+
{
227+
type: 'file',
228+
name: 'file1.txt',
229+
path: path.join('/root', 'file1.txt')
230+
},
231+
{
232+
type: 'directory',
233+
name: 'subdir',
234+
path: path.join('/root', 'subdir'),
235+
children: [
236+
{
237+
type: 'file',
238+
name: 'file2.js',
239+
path: path.join('/root', 'subdir', 'file2.js')
240+
}
241+
]
242+
}
243+
]);
244+
});
245+
246+
it('should return empty array for empty directory', async () => {
247+
readdir.mockResolvedValue([]);
248+
const result = await getDirElementsRecursive('/empty');
249+
expect(result).toEqual([]);
250+
});
251+
252+
it('should handle deeply nested directories', async () => {
253+
readdir
254+
.mockResolvedValueOnce([
255+
{ name: 'b', isDirectory: () => true, isFile: () => false }
256+
])
257+
.mockResolvedValueOnce([
258+
{ name: 'c', isDirectory: () => true, isFile: () => false }
259+
])
260+
.mockResolvedValueOnce([
261+
{ name: 'file.txt', isDirectory: () => false, isFile: () => true }
262+
]);
263+
const result = await getDirElementsRecursive('/a');
264+
expect(result).toEqual([
265+
{
266+
type: 'directory',
267+
name: 'b',
268+
path: path.join('/a', 'b'),
269+
children: [
270+
{
271+
type: 'directory',
272+
name: 'c',
273+
path: path.join('/a', 'b', 'c'),
274+
children: [
275+
{
276+
type: 'file',
277+
name: 'file.txt',
278+
path: path.join('/a', 'b', 'c', 'file.txt')
279+
}
280+
]
281+
}
282+
]
283+
}
284+
]);
285+
});
286+
});
287+

0 commit comments

Comments
 (0)