Skip to content

Commit 8ba602f

Browse files
harshhgithubHarsh Shukladerberg
authored
test: add tests for hasNestedConfig (#1673)
Co-authored-by: Harsh Shukla <you@example.com> Co-authored-by: Harsh Shukla <harshshuklaa1104@gmail.com> Co-authored-by: Lukasz Gornicki <lpgornicki@gmail.com>
1 parent a18846d commit 8ba602f

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

packages/helpers/src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { getMessageExamples, getOperationMessages } = require('./operations');
22
const { getServerUrl, getServer, getServerHost } = require('./servers');
33
const { getClientName, getInfo, toSnakeCase, getTitle} = require('./utils');
44
const { getQueryParams } = require('./bindings');
5-
const { cleanTestResultPaths, verifyDirectoryStructure, getDirElementsRecursive, buildParams, listFiles} = require('./testing');
5+
const { cleanTestResultPaths, verifyDirectoryStructure, getDirElementsRecursive, buildParams, listFiles, hasNestedConfig } = require('./testing');
66

77
module.exports = {
88
getServerUrl,
@@ -19,5 +19,6 @@ module.exports = {
1919
cleanTestResultPaths,
2020
verifyDirectoryStructure,
2121
getDirElementsRecursive,
22-
buildParams
22+
buildParams,
23+
hasNestedConfig
2324
};

packages/helpers/src/testing.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,6 @@ module.exports = {
129129
getDirElementsRecursive,
130130
verifyDirectoryStructure,
131131
buildParams,
132-
listFiles
132+
listFiles,
133+
hasNestedConfig
133134
};

packages/helpers/test/testing.test.js

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
const { listFiles, buildParams } = require('@asyncapi/generator-helpers');
1+
const { listFiles, buildParams, hasNestedConfig } = require('@asyncapi/generator-helpers');
22
const fs = require('fs/promises');
33

4-
jest.mock('fs/promises');
4+
jest.mock('fs/promises', () => ({
5+
rm: jest.fn(),
6+
readdir: jest.fn(),
7+
}));
58

69
describe('listFiles', () => {
710
afterEach(() => {
@@ -33,6 +36,57 @@ describe('listFiles', () => {
3336
});
3437
});
3538

39+
describe('hasNestedConfig', () => {
40+
it('should return false for an empty object', () => {
41+
expect(hasNestedConfig({})).toBe(false);
42+
});
43+
44+
it('should return false when all values are primitives', () => {
45+
const config = { host: 'localhost', port: 8080, secure: false };
46+
expect(hasNestedConfig(config)).toBe(false);
47+
});
48+
49+
it('should return true when there is a nested object', () => {
50+
const config = { db: { user: 'admin', pass: 'secret' } };
51+
expect(hasNestedConfig(config)).toBe(true);
52+
});
53+
54+
it('should return true when there are multiple nested objects', () => {
55+
const config = {
56+
db: { user: 'admin' },
57+
cache: { enabled: true }
58+
};
59+
expect(hasNestedConfig(config)).toBe(true);
60+
});
61+
62+
it('should return true when a value is an array', () => {
63+
const config = { servers: ['s1', 's2'] };
64+
expect(hasNestedConfig(config)).toBe(true);
65+
});
66+
67+
it('should return false when a value is null', () => {
68+
const config = { host: null, port: 3000 };
69+
expect(hasNestedConfig(config)).toBe(false);
70+
});
71+
72+
it('should returns true when config contains both nested objects and primitive values', () => {
73+
const config = {
74+
host: 'localhost',
75+
db: { name: 'testdb' },
76+
retries: 3
77+
};
78+
expect(hasNestedConfig(config)).toBe(true);
79+
});
80+
81+
it('should handle nested arrays inside objects', () => {
82+
const config = {
83+
metadata: {
84+
tags: ['tag1', 'tag2']
85+
}
86+
};
87+
expect(hasNestedConfig(config)).toBe(true);
88+
});
89+
});
3690
describe('buildParams', () => {
3791
it('should include clientFileName when language is not java', () => {
3892
const config = { clientFileName: 'myClient.js' };
@@ -85,4 +139,4 @@ describe('buildParams', () => {
85139
clientFileName: 'client.js',
86140
});
87141
});
88-
});
142+
});

0 commit comments

Comments
 (0)