Skip to content

Commit 0874880

Browse files
committed
remove unwanted default values
1 parent 8cf856b commit 0874880

File tree

9 files changed

+209
-14
lines changed

9 files changed

+209
-14
lines changed

packages/components/src/components/DependencyProvider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function resolveFrameworkDependencies(frameworkConfig, role) {
9191
* @throws {Error} When the specified language is not supported.
9292
* @throws {Error} When the specified framework is not supported for the given language.
9393
*/
94-
function resolveDependencies(language, framework = '', role = '') {
94+
function resolveDependencies(language, framework, role) {
9595
const config = dependenciesConfig[language];
9696
const supportedLanguages = Object.keys(dependenciesConfig);
9797

packages/components/src/components/MethodGenerator.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ const resolveDocsAndLogic = ({ language, methodDocs, methodLogic, methodConfig,
3838

3939
if (framework && config[framework]) {
4040
const frameworkConfig = config[framework];
41-
docs = frameworkConfig.methodDocs ?? methodDocs ?? '';
42-
logic = frameworkConfig.methodLogic ?? methodLogic ?? '';
41+
docs = frameworkConfig.methodDocs ?? methodDocs;
42+
logic = frameworkConfig.methodLogic ?? methodLogic;
4343
} else if (config.methodLogic || config.methodDocs) {
44-
docs = config.methodDocs ?? methodDocs ?? '';
45-
logic = config.methodLogic ?? methodLogic ?? '';
44+
docs = config.methodDocs ?? methodDocs;
45+
logic = config.methodLogic ?? methodLogic;
4646
}
4747
}
4848

packages/components/src/components/OnClose.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const websocketOnCloseMethod = {
5656
* @returns {Function|undefined} The code generator function, or undefined if not found.
5757
*/
5858

59-
const resolveCloseConfig = (language, framework = '') => {
59+
const resolveCloseConfig = (language, framework) => {
6060
const config = websocketOnCloseMethod[language];
6161
if (typeof config === 'function') {
6262
return config;
@@ -108,7 +108,7 @@ export function OnClose({ language, framework = '', title }) {
108108
const generateOnCloseCode = resolveCloseConfig(language, framework);
109109

110110
if (typeof generateOnCloseCode !== 'function') {
111-
const supportedFrameworks = Object.keys(websocketOnCloseMethod[language] || {});
111+
const supportedFrameworks = Object.keys(websocketOnCloseMethod[language]);
112112
throw unsupportedFramework(language, framework, supportedFrameworks);
113113
}
114114

packages/components/src/components/OnOpen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void onOpen() {
4848
* @returns {Function|null} The code generator function, or null if not found.
4949
*/
5050

51-
const resolveOpenConfig = (language, framework = '') => {
51+
const resolveOpenConfig = (language, framework) => {
5252
const config = websocketOnOpenMethod[language];
5353
if (typeof config === 'function') {
5454
return config;

packages/components/src/components/QueryParamsVariables.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Text } from '@asyncapi/generator-react-sdk';
22
import { toCamelCase } from '@asyncapi/generator-helpers';
3+
import { unsupportedLanguage } from '../utils/ErrorHandling';
34

45
/**
56
* @typedef {'python' | 'java' | 'javascript'} Language
@@ -95,7 +96,7 @@ const queryParamLogicConfig = {
9596
* @param {string} [framework=''] - Optional framework (e.g., 'quarkus' for Java).
9697
* @returns {function | undefined} The configuration function for generating query parameter code.
9798
*/
98-
function resolveQueryParamLogic(language, framework = '') {
99+
function resolveQueryParamLogic(language, framework) {
99100
const config = queryParamLogicConfig[language];
100101
if (typeof config === 'function') {
101102
return config;
@@ -153,6 +154,11 @@ export function QueryParamsVariables({ language, framework = '', queryParams })
153154
return null;
154155
}
155156

157+
const supportedLanguages = Object.keys(queryParamLogicConfig);
158+
if (!supportedLanguages.includes(language)) {
159+
throw unsupportedLanguage(language, supportedLanguages);
160+
}
161+
156162
const generateParamCode = resolveQueryParamLogic(language, framework);
157163
if (!generateParamCode) {
158164
return null;

packages/components/test/components/MethodGenerator.test.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,111 @@ describe('MethodGenerator', () => {
148148
expect(result.trim()).toMatchSnapshot();
149149
});
150150

151+
test('renders with destructuring default values when customMethodConfig is partial', () => {
152+
const result = render(
153+
<MethodGenerator
154+
language="java"
155+
methodName="testMethod"
156+
methodParams={['String param1', 'int param2']}
157+
customMethodConfig={{
158+
returnType: 'public void',
159+
closingTag: '}',
160+
parameterWrap: false
161+
}}
162+
methodLogic="System.out.println('Testing parameterWrap');"
163+
/>
164+
);
165+
expect(result.trim()).toMatchSnapshot();
166+
});
167+
168+
test('renders with framework config falls back to methodDocs prop when frameworkDocs missing', () => {
169+
const result = render(
170+
<MethodGenerator
171+
language="java"
172+
methodName="testMethod"
173+
methodDocs="// Prop Docs"
174+
methodConfig={{
175+
java: {
176+
quarkus: {
177+
methodLogic: 'System.out.println("Logic");'
178+
}
179+
}
180+
}}
181+
framework="quarkus"
182+
/>
183+
);
184+
185+
expect(result.trim()).toMatchSnapshot();
186+
});
187+
188+
test('renders with framework config falls back to empty string when both docs missing', () => {
189+
const result = render(
190+
<MethodGenerator
191+
language="java"
192+
methodName="testMethod"
193+
methodConfig={{
194+
java: {
195+
quarkus: {}
196+
}
197+
}}
198+
framework="quarkus"
199+
/>
200+
);
201+
202+
expect(result.trim()).toMatchSnapshot();
203+
});
204+
205+
test('renders with language-level config falls back to methodLogic prop when config.methodLogic missing', () => {
206+
const result = render(
207+
<MethodGenerator
208+
language="python"
209+
methodName="testMethod"
210+
methodLogic="print('from prop')"
211+
methodConfig={{
212+
python: {
213+
methodDocs: 'Some docs'
214+
}
215+
}}
216+
/>
217+
);
218+
219+
expect(result.trim()).toMatchSnapshot();
220+
});
221+
222+
test('renders with language-level config falls back to empty string when both methodLogic sources missing', () => {
223+
const result = render(
224+
<MethodGenerator
225+
language="python"
226+
methodName="testMethod"
227+
methodConfig={{
228+
python: {
229+
methodDocs: 'Some docs'
230+
}
231+
}}
232+
/>
233+
);
234+
235+
expect(result.trim()).toMatchSnapshot();
236+
});
237+
238+
test('renders with language-level config falls back to methodLogic prop', () => {
239+
const result = render(
240+
<MethodGenerator
241+
language="python"
242+
methodName="testMethod"
243+
methodLogic="print('fallback')"
244+
methodConfig={{
245+
python: {
246+
methodDocs: undefined,
247+
methodLogic: undefined
248+
}
249+
}}
250+
/>
251+
);
252+
253+
expect(result.trim()).toMatchSnapshot();
254+
});
255+
151256
test('throws an error when unsupported language is provided', () => {
152257
expect(() =>
153258
render(

packages/components/test/components/Models.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,40 @@ describe('Integration Tests for models function', () => {
8888
const result = render(await component);
8989
expect(result).toMatchSnapshot();
9090
});
91+
92+
test('skips models with falsy content', async () => {
93+
let component;
94+
jest.isolateModules(() => {
95+
jest.doMock('@asyncapi/modelina', () => ({
96+
PythonGenerator: class {
97+
constructor() {}
98+
async generate() {
99+
return [
100+
{ modelName: 'Example', result: null },
101+
{ modelName: 'Other', result: 'valid content' }
102+
];
103+
}
104+
},
105+
JavaGenerator: class {},
106+
TypeScriptGenerator: class {},
107+
CSharpGenerator: class {},
108+
RustGenerator: class {},
109+
JavaScriptGenerator: class {},
110+
FormatHelpers: {
111+
toPascalCase: (s) => s,
112+
toCamelCase: (s) => s,
113+
toKebabCase: (s) => s,
114+
toSnakeCase: (s) => s
115+
}
116+
}));
117+
118+
const { Models: MockedModels } = require('../../src/index');
119+
component = MockedModels({ asyncapi: parsedAsyncAPIDocument });
120+
});
121+
122+
const result = render(await component);
123+
expect(result).toBe('valid content');
124+
125+
jest.resetModules();
126+
});
91127
});

packages/components/test/components/QueryParamsVariables.test.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,13 @@ describe('Testing of QueryParamsVariables component', () => {
7474
expect(result.trim()).toMatchSnapshot();
7575
});
7676

77-
test('returns null when language is unsupported', () => {
78-
const result = render(
77+
test('throws an error when the language is not supported', () => {
78+
expect(() => render(
7979
<QueryParamsVariables
8080
language="go"
8181
queryParams={[['token']]}
8282
/>
83-
);
84-
85-
expect(result.trim()).toBe('');
83+
)).toThrow(/Unsupported language "go". Supported languages:/);
8684
});
8785

8886
test('returns null when framework is invalid for java', () => {
@@ -108,4 +106,24 @@ describe('Testing of QueryParamsVariables component', () => {
108106

109107
expect(result.trim()).toMatchSnapshot();
110108
});
109+
110+
test('returns null when java framework is missing', () => {
111+
const result = render(
112+
<QueryParamsVariables
113+
language="java"
114+
queryParams={[['token']]}
115+
/>
116+
);
117+
118+
expect(result.trim()).toBe('');
119+
});
120+
test('directly covers resolveQueryParamLogic null branch', () => {
121+
const result = QueryParamsVariables({
122+
language: 'java',
123+
framework: '',
124+
queryParams: [['token']]
125+
});
126+
127+
expect(result).toBeNull();
128+
});
111129
});

packages/components/test/components/__snapshots__/MethodGenerator.test.js.snap

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,36 @@ exports[`MethodGenerator renders with customMethodConfig override 1`] = `
4242
}"
4343
`;
4444

45+
exports[`MethodGenerator renders with destructuring default values when customMethodConfig is partial 1`] = `
46+
"public void testMethodString param1, int param2
47+
System.out.println('Testing parameterWrap');
48+
}"
49+
`;
50+
51+
exports[`MethodGenerator renders with framework config falls back to empty string when both docs missing 1`] = `"testMethod"`;
52+
53+
exports[`MethodGenerator renders with framework config falls back to methodDocs prop when frameworkDocs missing 1`] = `
54+
"// Prop Docs
55+
testMethod
56+
System.out.println(\\"Logic\\");"
57+
`;
58+
59+
exports[`MethodGenerator renders with language-level config falls back to empty string when both methodLogic sources missing 1`] = `
60+
"Some docs
61+
def testMethod() :"
62+
`;
63+
64+
exports[`MethodGenerator renders with language-level config falls back to methodLogic prop 1`] = `
65+
"def testMethod() :
66+
print('fallback')"
67+
`;
68+
69+
exports[`MethodGenerator renders with language-level config falls back to methodLogic prop when config.methodLogic missing 1`] = `
70+
"Some docs
71+
def testMethod() :
72+
print('from prop')"
73+
`;
74+
4575
exports[`MethodGenerator renders with methodDocs 1`] = `
4676
"\\"\\"\\"Process the input data.\\"\\"\\"
4777
def processData() :

0 commit comments

Comments
 (0)