Skip to content

Commit 33b1719

Browse files
authored
test: add test coverage for URIParams (#1898)
Co-authored-by: Harsh Gupta <harsh16official@gmail.com>
1 parent 5f1e08e commit 33b1719

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import path from 'path';
2+
import { render } from '@asyncapi/generator-react-sdk';
3+
import { Parser, fromFile } from '@asyncapi/parser';
4+
import { getQueryParams } from '@asyncapi/generator-helpers';
5+
import URIParams from '../../components/URIParams.js';
6+
7+
const parser = new Parser();
8+
const asyncapiFilePath = path.resolve(__dirname, '../../../../test/__fixtures__/asyncapi-websocket-components.yml');
9+
10+
describe('URIParams component (integration with AsyncAPI document)', () => {
11+
let parsedAsyncAPIDocument;
12+
let channels;
13+
let queryParamsArray;
14+
15+
beforeAll(async () => {
16+
const parseResult = await fromFile(parser, asyncapiFilePath).parse();
17+
parsedAsyncAPIDocument = parseResult.document;
18+
channels = parsedAsyncAPIDocument.channels();
19+
const queryParams = getQueryParams(channels);
20+
queryParamsArray = queryParams ? Array.from(queryParams.entries()) : null;
21+
});
22+
23+
test('renders nothing when queryParamsArray is null', () => {
24+
const result = render(<URIParams queryParamsArray={null} pathName="/link" />);
25+
expect(result.trim()).toMatchSnapshot();
26+
});
27+
28+
test('renders nothing when queryParamsArray is empty array', () => {
29+
const result = render(<URIParams queryParamsArray={[]} pathName="/link" />);
30+
expect(result.trim()).toMatchSnapshot();
31+
});
32+
33+
test('renders nothing when queryParamsArray is undefined', () => {
34+
const result = render(<URIParams pathName="/link" />);
35+
expect(result.trim()).toMatchSnapshot();
36+
});
37+
38+
test('renders URI params with multiple query parameters from fixture', () => {
39+
const result = render(<URIParams queryParamsArray={queryParamsArray} pathName="/link" />);
40+
expect(result.trim()).toMatchSnapshot();
41+
});
42+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`URIParams component (integration with AsyncAPI document) renders URI params with multiple query parameters from fixture 1`] = `
4+
"// URI parameters
5+
String query = \\"\\";
6+
7+
String heartbeat = System.getenv(\\"HEARTBEAT\\");
8+
if(heartbeat == null || heartbeat.isEmpty()){
9+
throw new IllegalArgumentException(\\"Required environment variable HEARTBEAT is missing or empty\\");
10+
}
11+
query += \\"heartbeat=\\" + URLEncoder.encode(heartbeat, StandardCharsets.UTF_8);
12+
13+
String bids = System.getenv(\\"BIDS\\");
14+
if(bids == null || bids.isEmpty()){
15+
throw new IllegalArgumentException(\\"Required environment variable BIDS is missing or empty\\");
16+
}
17+
query += \\"&bids=\\" + URLEncoder.encode(bids, StandardCharsets.UTF_8);
18+
19+
20+
String queryUri = baseURI + \\"/link\\" + \\"?\\" + query;
21+
WebSocketClientConnection connection = connector.baseUri(queryUri).connectAndAwait();
22+
Thread.sleep(120000); // Keep the connection open for 2 minutes"
23+
`;
24+
25+
exports[`URIParams component (integration with AsyncAPI document) renders nothing when queryParamsArray is empty array 1`] = `""`;
26+
27+
exports[`URIParams component (integration with AsyncAPI document) renders nothing when queryParamsArray is null 1`] = `""`;
28+
29+
exports[`URIParams component (integration with AsyncAPI document) renders nothing when queryParamsArray is undefined 1`] = `""`;

packages/templates/clients/websocket/test/__fixtures__/asyncapi-websocket-components.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ channels:
1414
testChannel:
1515
address: /test
1616
description: Test channel for operation routing
17+
bindings:
18+
ws:
19+
bindingVersion: 0.1.0
20+
query:
21+
type: object
22+
properties:
23+
heartbeat:
24+
type: boolean
25+
default: false
26+
bids:
27+
type: boolean
28+
default: true
1729
messages:
1830
testMessage:
1931
summary: Test message

0 commit comments

Comments
 (0)