Skip to content

Commit 84c621a

Browse files
committed
test: add tests for RegisterReceiveOperation in python websocket components
1 parent 40df7bd commit 84c621a

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import path from 'path';
2+
import { render } from '@asyncapi/generator-react-sdk';
3+
import { Parser, fromFile } from '@asyncapi/parser';
4+
import { RegisterReceiveOperations } from '../../components/RegisterReceiveOperations';
5+
6+
const parser = new Parser();
7+
const asyncapi_websocket_query = path.resolve(__dirname, '../../../test/__fixtures__/asyncapi-websocket-components.yml');
8+
9+
describe('Testing of RegisterReceiveOperations component', () => {
10+
let parsedAsyncAPIDocument;
11+
12+
beforeAll(async () => {
13+
const parseResult = await fromFile(parser, asyncapi_websocket_query).parse();
14+
parsedAsyncAPIDocument = parseResult.document;
15+
});
16+
17+
test('render RegisterReceiveOperations component with receive operations', () => {
18+
const receiveOperations = parsedAsyncAPIDocument.operations().filterByReceive();
19+
const result = render(<RegisterReceiveOperations receiveOperations={receiveOperations} />);
20+
const actual = result.trim();
21+
expect(actual).toMatchSnapshot();
22+
});
23+
24+
test('renders nothing when receive operations is empty', () => {
25+
const result = render(<RegisterReceiveOperations receiveOperations={[]} />);
26+
const actual = result.trim();
27+
expect(actual).toMatchSnapshot();
28+
});
29+
30+
test('renders nothing when receive operations is null', () => {
31+
const result = render(<RegisterReceiveOperations receiveOperations={null} />);
32+
const actual = result.trim();
33+
expect(actual).toMatchSnapshot();
34+
});
35+
36+
test('renders nothing without receive operations', () => {
37+
const result = render(<RegisterReceiveOperations />);
38+
const actual = result.trim();
39+
expect(actual).toMatchSnapshot();
40+
});
41+
42+
test('renders nothing when receiveOperations is undefined', () => {
43+
const result = render(<RegisterReceiveOperations receiveOperations={undefined} />);
44+
const actual = result.trim();
45+
expect(actual).toMatchSnapshot();
46+
});
47+
});
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Testing of RegisterReceiveOperations component render RegisterReceiveOperations component with receive operations 1`] = `
4+
"def register_receive_message_handler(self, handler, discriminator_key=None, discriminator_value=None):
5+
\\"\\"\\"
6+
Register a handler for receiveMessage operation.
7+
8+
Args:
9+
handler (callable): Handler function that receives raw_message as argument
10+
discriminator_key (str): Message field to use for routing (e.g., 'type', 'event_type')
11+
discriminator_value (str): Expected value for routing (e.g., 'hello', 'user_joined')
12+
\\"\\"\\"
13+
if not callable(handler):
14+
print(\\"Handler must be callable\\")
15+
return
16+
17+
# Validate that either both discriminator_key and discriminator_value are provided or neither
18+
if (discriminator_key is not None and discriminator_value is None) or (discriminator_key is None and discriminator_value is not None):
19+
print(\\"Error: Both discriminator_key and discriminator_value must be provided together\\")
20+
return
21+
22+
# Register handler
23+
self.receive_operation_handlers[\\"receiveMessage\\"] = handler
24+
25+
# Add discriminator entry to the list if both key and value are provided
26+
if discriminator_key is not None and discriminator_value is not None:
27+
discriminator_entry = {
28+
\\"key\\": discriminator_key,
29+
\\"value\\": discriminator_value,
30+
\\"operation_id\\": \\"receiveMessage\\"
31+
}
32+
33+
# Check if this discriminator already exists
34+
exists = any(
35+
d.get(\\"key\\") == discriminator_key and
36+
d.get(\\"value\\") == discriminator_value and
37+
d.get(\\"operation_id\\") == \\"receiveMessage\\"
38+
for d in self.receive_operation_discriminators
39+
)
40+
41+
if not exists:
42+
self.receive_operation_discriminators.append(discriminator_entry)
43+
44+
def register_receive_notification_handler(self, handler, discriminator_key=None, discriminator_value=None):
45+
\\"\\"\\"
46+
Register a handler for receiveNotification operation.
47+
48+
Args:
49+
handler (callable): Handler function that receives raw_message as argument
50+
discriminator_key (str): Message field to use for routing (e.g., 'type', 'event_type')
51+
discriminator_value (str): Expected value for routing (e.g., 'hello', 'user_joined')
52+
\\"\\"\\"
53+
if not callable(handler):
54+
print(\\"Handler must be callable\\")
55+
return
56+
57+
# Validate that either both discriminator_key and discriminator_value are provided or neither
58+
if (discriminator_key is not None and discriminator_value is None) or (discriminator_key is None and discriminator_value is not None):
59+
print(\\"Error: Both discriminator_key and discriminator_value must be provided together\\")
60+
return
61+
62+
# Register handler
63+
self.receive_operation_handlers[\\"receiveNotification\\"] = handler
64+
65+
# Add discriminator entry to the list if both key and value are provided
66+
if discriminator_key is not None and discriminator_value is not None:
67+
discriminator_entry = {
68+
\\"key\\": discriminator_key,
69+
\\"value\\": discriminator_value,
70+
\\"operation_id\\": \\"receiveNotification\\"
71+
}
72+
73+
# Check if this discriminator already exists
74+
exists = any(
75+
d.get(\\"key\\") == discriminator_key and
76+
d.get(\\"value\\") == discriminator_value and
77+
d.get(\\"operation_id\\") == \\"receiveNotification\\"
78+
for d in self.receive_operation_discriminators
79+
)
80+
81+
if not exists:
82+
self.receive_operation_discriminators.append(discriminator_entry)"
83+
`;
84+
85+
exports[`Testing of RegisterReceiveOperations component renders nothing when receive operations is empty 1`] = `""`;
86+
87+
exports[`Testing of RegisterReceiveOperations component renders nothing when receive operations is null 1`] = `""`;
88+
89+
exports[`Testing of RegisterReceiveOperations component renders nothing when receiveOperations is undefined 1`] = `""`;
90+
91+
exports[`Testing of RegisterReceiveOperations component renders nothing without receive operations 1`] = `""`;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,21 @@ operations:
4848
channel:
4949
$ref: '#/channels/testChannel'
5050
summary: Send notification
51+
messages:
52+
- $ref: '#/channels/testChannel/messages/testMessage'
53+
54+
receiveMessage:
55+
action: receive
56+
channel:
57+
$ref: '#/channels/testChannel'
58+
summary: Receive a message
59+
messages:
60+
- $ref: '#/channels/testChannel/messages/testMessage'
61+
62+
receiveNotification:
63+
action: receive
64+
channel:
65+
$ref: '#/channels/testChannel'
66+
summary: Receive notification
5167
messages:
5268
- $ref: '#/channels/testChannel/messages/testMessage'

0 commit comments

Comments
 (0)