Skip to content

Commit 33e9b9b

Browse files
authored
Merge branch 'master' into test/add-tests-python-RegisterReceiveOperations-component
2 parents d358765 + e43f7e9 commit 33e9b9b

File tree

6 files changed

+240
-1
lines changed

6 files changed

+240
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { render } from '@asyncapi/generator-react-sdk';
2+
import { CoreMethods } from '../../src/components/readme/CoreMethods';
3+
4+
describe('CoreMethods component', () => {
5+
test('renders core methods for javascript', () => {
6+
const result = render(<CoreMethods language="javascript" />);
7+
expect(result.trim()).toMatchSnapshot();
8+
});
9+
10+
test('renders core methods for python', () => {
11+
const result = render(<CoreMethods language="python" />);
12+
expect(result.trim()).toMatchSnapshot();
13+
});
14+
15+
test('throws error for unsupported or missing language', () => {
16+
expect(() => {
17+
render(<CoreMethods />);
18+
}).toThrow('Unsupported language');
19+
});
20+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { render } from '@asyncapi/generator-react-sdk';
2+
import { Installation } from '../../src/components/readme/Installation';
3+
4+
describe('Installation component', () => {
5+
test('renders javascript installation command', () => {
6+
const result = render(<Installation language="javascript" />);
7+
expect(result.trim()).toMatchSnapshot();
8+
});
9+
10+
test('renders python installation command', () => {
11+
const result = render(<Installation language="python" />);
12+
expect(result.trim()).toMatchSnapshot();
13+
});
14+
15+
test('renders installation section when language is undefined', () => {
16+
const result = render(<Installation />);
17+
const output = result.trim();
18+
19+
// Explicitly document current behavior
20+
expect(output).toContain('## Installation');
21+
expect(output).toContain('Install dependencies');
22+
expect(output).toContain('undefined');
23+
});
24+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`CoreMethods component renders core methods for javascript 1`] = `
4+
"## API
5+
6+
### \`connect()\`
7+
Establishes a WebSocket connection.
8+
9+
### \`registerMessageHandler(handlerFunction)\`
10+
Registers a callback for incoming messages.
11+
12+
### \`registerErrorHandler(handlerFunction)\`
13+
Registers a callback for connection errors.
14+
15+
### \`close()\`
16+
Closes the WebSocket connection."
17+
`;
18+
19+
exports[`CoreMethods component renders core methods for python 1`] = `
20+
"## API
21+
22+
### \`connect()\`
23+
Establishes a WebSocket connection.
24+
25+
### \`register_message_handler(handler_function)\`
26+
Registers a callback for incoming messages.
27+
28+
### \`register_error_handler(handler_function)\`
29+
Registers a callback for connection errors.
30+
31+
### \`close()\`
32+
Closes the WebSocket connection."
33+
`;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Installation component renders javascript installation command 1`] = `
4+
"## Installation
5+
6+
Install dependencies:
7+
8+
\`\`\`bash
9+
npm install
10+
\`\`\`"
11+
`;
12+
13+
exports[`Installation component renders python installation command 1`] = `
14+
"## Installation
15+
16+
Install dependencies:
17+
18+
\`\`\`bash
19+
pip install -r requirements.txt
20+
\`\`\`"
21+
`;

packages/helpers/src/ModelsPresets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const websocketJavaPreset = {
1111
// Solution to handle imports dynamically
1212
const requiredImports = new Set();
1313
requiredImports.add('import java.util.Objects;');
14-
Object.values(model.properties).forEach(property => {
14+
Object.values(model.properties || {}).forEach(property => {
1515
const type = property.property.type;
1616

1717
if (type === 'Map<String, Object>') {
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
const { JavaModelsPresets } = require('../src/ModelsPresets');
2+
const { JAVA_COMMON_PRESET } = require('@asyncapi/modelina');
3+
4+
const PACKAGE_DECLARATION = 'package com.asyncapi.models;';
5+
const IMPORT_OBJECTS = 'import java.util.Objects;';
6+
const IMPORT_MAP = 'import java.util.Map;';
7+
const TEST_CLASS_CONTENT = 'public class TestClass {}';
8+
const MAP_STRING_OBJECT_TYPE = 'Map<String, Object>';
9+
const PACKAGE_WITH_NEWLINES = 'package com.asyncapi.models;\n\n';
10+
11+
describe('ModelsPresets', () => {
12+
describe('JavaModelsPresets structure', () => {
13+
it('should export JavaModelsPresets as an array', () => {
14+
expect(Array.isArray(JavaModelsPresets)).toBe(true);
15+
expect(JavaModelsPresets).toHaveLength(2);
16+
});
17+
18+
it('should contain JAVA_COMMON_PRESET with correct options', () => {
19+
const commonPreset = JavaModelsPresets[0];
20+
expect(commonPreset.preset).toBe(JAVA_COMMON_PRESET);
21+
expect(commonPreset.options).toEqual({
22+
equal: true,
23+
hashCode: true,
24+
classToString: true,
25+
marshalling: false
26+
});
27+
});
28+
29+
it('should contain websocketJavaPreset as second element', () => {
30+
const websocketPreset = JavaModelsPresets[1];
31+
expect(websocketPreset).toBeDefined();
32+
expect(websocketPreset.class).toBeDefined();
33+
expect(websocketPreset.enum).toBeDefined();
34+
expect(websocketPreset.union).toBeDefined();
35+
});
36+
});
37+
38+
describe('websocketJavaPreset class.self', () => {
39+
const websocketPreset = JavaModelsPresets[1];
40+
41+
it('should add package and basic imports for class without Map properties', () => {
42+
const mockModel = {
43+
properties: {
44+
prop1: { property: { type: 'String' } },
45+
prop2: { property: { type: 'Integer' } }
46+
}
47+
};
48+
const result = websocketPreset.class.self({ content: TEST_CLASS_CONTENT, model: mockModel });
49+
50+
expect(result).toContain(PACKAGE_DECLARATION);
51+
expect(result).toContain(IMPORT_OBJECTS);
52+
expect(result).toContain(TEST_CLASS_CONTENT);
53+
expect(result).not.toContain(IMPORT_MAP);
54+
});
55+
56+
it('should add Map import when property type is Map<String, Object>', () => {
57+
const mockModel = {
58+
properties: {
59+
prop1: { property: { type: MAP_STRING_OBJECT_TYPE } },
60+
prop2: { property: { type: 'String' } }
61+
}
62+
};
63+
const result = websocketPreset.class.self({ content: TEST_CLASS_CONTENT, model: mockModel });
64+
65+
expect(result).toContain(PACKAGE_DECLARATION);
66+
expect(result).toContain(IMPORT_OBJECTS);
67+
expect(result).toContain(IMPORT_MAP);
68+
expect(result).toContain(TEST_CLASS_CONTENT);
69+
});
70+
71+
it('should add Map import only once for multiple Map properties', () => {
72+
const mockModel = {
73+
properties: {
74+
prop1: { property: { type: MAP_STRING_OBJECT_TYPE } },
75+
prop2: { property: { type: MAP_STRING_OBJECT_TYPE } },
76+
prop3: { property: { type: MAP_STRING_OBJECT_TYPE } }
77+
}
78+
};
79+
const result = websocketPreset.class.self({ content: TEST_CLASS_CONTENT, model: mockModel });
80+
81+
const mapImportCount = (result.match(/import java\.util\.Map;/g) || []).length;
82+
expect(mapImportCount).toBe(1);
83+
});
84+
85+
it('should handle model with no properties', () => {
86+
const mockModel = { properties: {} };
87+
const content = 'public class EmptyClass {}';
88+
const result = websocketPreset.class.self({ content, model: mockModel });
89+
90+
expect(result).toContain(PACKAGE_DECLARATION);
91+
expect(result).toContain(IMPORT_OBJECTS);
92+
expect(result).toContain('public class EmptyClass {}');
93+
});
94+
95+
it('should handle model with missing properties key', () => {
96+
const mockModel = {};
97+
const content = 'public class NoPropsClass {}';
98+
const result = websocketPreset.class.self({ content, model: mockModel });
99+
100+
expect(result).toContain(PACKAGE_DECLARATION);
101+
expect(result).toContain(IMPORT_OBJECTS);
102+
expect(result).toContain('public class NoPropsClass {}');
103+
});
104+
});
105+
106+
describe('websocketJavaPreset enum.self', () => {
107+
const websocketPreset = JavaModelsPresets[1];
108+
109+
it('should add package declaration to enum content', () => {
110+
const content = 'public enum Status { ACTIVE, INACTIVE }';
111+
const result = websocketPreset.enum.self({ content });
112+
113+
expect(result).toBe(`${PACKAGE_WITH_NEWLINES}public enum Status { ACTIVE, INACTIVE }`);
114+
});
115+
116+
it('should handle empty enum content', () => {
117+
const content = '';
118+
const result = websocketPreset.enum.self({ content });
119+
120+
expect(result).toBe(PACKAGE_WITH_NEWLINES);
121+
});
122+
});
123+
124+
describe('websocketJavaPreset union.self', () => {
125+
const websocketPreset = JavaModelsPresets[1];
126+
127+
it('should add package declaration to union content', () => {
128+
const content = 'public class UnionType {}';
129+
const result = websocketPreset.union.self({ content });
130+
131+
expect(result).toBe(`${PACKAGE_WITH_NEWLINES}public class UnionType {}`);
132+
});
133+
134+
it('should handle empty union content', () => {
135+
const content = '';
136+
const result = websocketPreset.union.self({ content });
137+
138+
expect(result).toBe(PACKAGE_WITH_NEWLINES);
139+
});
140+
});
141+
});

0 commit comments

Comments
 (0)