test(websocket-js): add unit tests for CompileOperationSchemas component#2034
test(websocket-js): add unit tests for CompileOperationSchemas component#2034SHUBHANSHU602 wants to merge 2 commits intoasyncapi:masterfrom
Conversation
|
What reviewer looks at during PR reviewThe following are ideal points maintainers look for during review. Reviewing these points yourself beforehand can help streamline the review process and reduce time to merge.
|
📝 WalkthroughWalkthroughAdds two new Jest test files for the CompileOperationSchemas component: one low-level test asserting null/element returns and rendered content, and one component-level test covering snapshot, empty array, and undefined Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/templates/clients/websocket/javascript/test/CompileOperationSchemas.test.js (1)
15-30: Make JSX-tree assertions less brittle.Current assertions depend on exact Fragment child shape (
result.props.children...). A small JSX restructuring inCompileOperationSchemascould break tests without changing behavior.Suggested refactor
import { CompileOperationSchemas } from '../components/CompileOperationSchemas'; import { Text } from '@asyncapi/generator-react-sdk'; describe('CompileOperationSchemas', () => { + function getTextNode(result) { + const children = Array.isArray(result?.props?.children) + ? result.props.children + : [result?.props?.children]; + return children.find((child) => child?.type === Text); + } + it('should return null when sendOperations is undefined', () => { const result = CompileOperationSchemas({}); expect(result).toBeNull(); }); @@ it('should render Text component when sendOperations exist', () => { const result = CompileOperationSchemas({ sendOperations: ['op1'] }); expect(result).not.toBeNull(); - expect(result.props.children.type).toBe(Text); + const textNode = getTextNode(result); + expect(textNode).toBeDefined(); + expect(textNode.type).toBe(Text); }); it('should contain compileOperationSchemas function in output', () => { const result = CompileOperationSchemas({ sendOperations: ['op1'] }); - - const textContent = result.props.children.props.children; + const textNode = getTextNode(result); + const textContent = textNode.props.children; expect(textContent).toContain('async compileOperationSchemas()'); expect(textContent).toContain('this.schemasCompiled'); expect(textContent).toContain('compileSchemasByOperationId'); }); });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/templates/clients/websocket/javascript/test/CompileOperationSchemas.test.js` around lines 15 - 30, The tests for CompileOperationSchemas rely on a brittle traversal of result.props.children; update them to robustly locate the Text node and its string contents instead: render CompileOperationSchemas into a test renderer result, use renderer utilities or a traversal (e.g., findAll/recursive scan) to locate the child whose type is Text (referencing CompileOperationSchemas and Text), then assert that at least one Text node's rendered string contains 'async compileOperationSchemas()', 'this.schemasCompiled', and 'compileSchemasByOperationId' rather than chaining result.props.children... to reach the strings.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In
`@packages/templates/clients/websocket/javascript/test/CompileOperationSchemas.test.js`:
- Around line 15-30: The tests for CompileOperationSchemas rely on a brittle
traversal of result.props.children; update them to robustly locate the Text node
and its string contents instead: render CompileOperationSchemas into a test
renderer result, use renderer utilities or a traversal (e.g., findAll/recursive
scan) to locate the child whose type is Text (referencing
CompileOperationSchemas and Text), then assert that at least one Text node's
rendered string contains 'async compileOperationSchemas()',
'this.schemasCompiled', and 'compileSchemasByOperationId' rather than chaining
result.props.children... to reach the strings.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 887323da-789c-46cf-9ca0-f841cbcebb09
📒 Files selected for processing (1)
packages/templates/clients/websocket/javascript/test/CompileOperationSchemas.test.js
Adi-204
left a comment
There was a problem hiding this comment.
@SHUBHANSHU602 the tests are incorrect. First, you need to create a components folder inside the test directory to match the expected structure. Also, we use snapshot testing for components—not unit tests. I’d suggest looking at other templates, such as the Java template, which already has proper component testing in place.
https://github.com/asyncapi/generator/tree/master/packages/templates/clients/websocket/java/quarkus/test/components
|
ohk @Adi-204 I was bit confused about the directory , thanks for guidence I will fix it soon. |
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/templates/clients/websocket/javascript/test/components/CompileOperationSchemas.test.js (1)
13-27: Consider parameterizing the two empty-output cases.Both tests assert the same outcome for different inputs;
it.eachcan reduce duplication and keep intent clear.♻️ Optional refactor
- it('renders empty output when no send operations', async () => { - const result = await render( - <CompileOperationSchemas sendOperations={[]} /> - ); - - expect(result).toBe(''); - }); - - it('renders empty output when sendOperations is undefined', async () => { - const result = await render( - <CompileOperationSchemas /> - ); - - expect(result).toBe(''); - }); + it.each([ + { label: 'empty array', sendOperations: [] }, + { label: 'undefined', sendOperations: undefined }, + ])('renders empty output when sendOperations is $label', async ({ sendOperations }) => { + const result = await render( + <CompileOperationSchemas sendOperations={sendOperations} /> + ); + + expect(result).toBe(''); + });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/templates/clients/websocket/javascript/test/components/CompileOperationSchemas.test.js` around lines 13 - 27, Replace the two duplicate tests that call render(<CompileOperationSchemas sendOperations={[]}/> ) and render(<CompileOperationSchemas />) with a single parameterized test using it.each, passing the two inputs (an empty array and undefined) and asserting the same result; locate the tests that reference CompileOperationSchemas and render in CompileOperationSchemas.test.js and refactor them to use it.each([...]) with a descriptive test name while keeping the expect(result).toBe('') assertion.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In
`@packages/templates/clients/websocket/javascript/test/components/CompileOperationSchemas.test.js`:
- Around line 13-27: Replace the two duplicate tests that call
render(<CompileOperationSchemas sendOperations={[]}/> ) and
render(<CompileOperationSchemas />) with a single parameterized test using
it.each, passing the two inputs (an empty array and undefined) and asserting the
same result; locate the tests that reference CompileOperationSchemas and render
in CompileOperationSchemas.test.js and refactor them to use it.each([...]) with
a descriptive test name while keeping the expect(result).toBe('') assertion.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ddfc8a87-30e3-4e23-8234-a40c06ce1319
⛔ Files ignored due to path filters (1)
packages/templates/clients/websocket/javascript/test/components/__snapshots__/CompileOperationSchemas.test.js.snapis excluded by!**/*.snap
📒 Files selected for processing (1)
packages/templates/clients/websocket/javascript/test/components/CompileOperationSchemas.test.js
|
@Adi-204 let me know if it is ohk now |
Adi-204
left a comment
There was a problem hiding this comment.
@SHUBHANSHU602 left 2 comments rest looks good!
| expect(textContent).toContain('this.schemasCompiled'); | ||
| expect(textContent).toContain('compileSchemasByOperationId'); | ||
| }); | ||
| }); No newline at end of file |
There was a problem hiding this comment.
please delete this test file as we don't need it anymore.
|
|
||
| expect(result).toBe(''); | ||
| }); | ||
| }); No newline at end of file |
There was a problem hiding this comment.
just add one more testcase with sendOperations=null



🧪 Add unit tests for
CompileOperationSchemascomponentSummary
This PR introduces unit tests for the
CompileOperationSchemascomponent in the WebSocket JavaScript client template.The component is responsible for generating a method (
compileOperationSchemas) as a string using theTextcomponent from@asyncapi/generator-react-sdk.What is tested
Returns
nullwhen:sendOperationsisundefinedsendOperationsis an empty arrayRenders a
Textcomponent whensendOperationsare providedVerifies that the generated output string contains key parts:
async compileOperationSchemas()this.schemasCompiledcompileSchemasByOperationIdResolves #1935
Summary by CodeRabbit