Skip to content

Commit c894e1d

Browse files
KunalNasaderberg
andauthored
test: add test for listfiles util (#1607)
Co-authored-by: KunalNasa <kunalnasa.dev@gmail.com> Co-authored-by: Lukasz Gornicki <lpgornicki@gmail.com>
1 parent d1e0b37 commit c894e1d

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

packages/helpers/test/utils.test.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22
const { Parser, fromFile } = require('@asyncapi/parser');
3-
const { getClientName, getInfo, getTitle, toSnakeCase } = require('@asyncapi/generator-helpers');
3+
const { getClientName, getInfo, getTitle, toSnakeCase, listFiles } = require('@asyncapi/generator-helpers');
4+
const fs = require('fs/promises');
45

56
const parser = new Parser();
67
const asyncapi_v3_path = path.resolve(__dirname, './__fixtures__/asyncapi-websocket-query.yml');
@@ -153,4 +154,35 @@ describe('toSnakeCase integration test with AsyncAPI', () => {
153154
const expectedOperationId = '';
154155
expect(actualOperationId).toBe(expectedOperationId);
155156
});
156-
});
157+
});
158+
159+
jest.mock('fs/promises');
160+
describe('listFiles', () => {
161+
afterEach(() => {
162+
jest.clearAllMocks();
163+
});
164+
165+
it('should return only file names from the directory', async () => {
166+
const mockDirents = [
167+
{ name: 'file1.txt', isFile: () => true },
168+
{ name: 'file2.js', isFile: () => true },
169+
{ name: 'subdir', isFile: () => false },
170+
];
171+
172+
fs.readdir.mockResolvedValue(mockDirents);
173+
const mockPath = '/mock/path';
174+
175+
const result = await listFiles(mockPath);
176+
expect(fs.readdir).toHaveBeenCalledWith(mockPath, { withFileTypes: true });
177+
expect(result).toEqual(['file1.txt', 'file2.js']);
178+
});
179+
180+
it('should return an empty array if no files exist', async () => {
181+
fs.readdir.mockResolvedValue([
182+
{ name: 'folder', isFile: () => false },
183+
]);
184+
const mockPath = '/mock/path';
185+
const result = await listFiles(mockPath);
186+
expect(result).toEqual([]);
187+
});
188+
});

0 commit comments

Comments
 (0)