diff --git a/packages/templates/clients/websocket/python/test/components/RegisterReceiveOperations.test.js b/packages/templates/clients/websocket/python/test/components/RegisterReceiveOperations.test.js new file mode 100644 index 0000000000..b493738cfb --- /dev/null +++ b/packages/templates/clients/websocket/python/test/components/RegisterReceiveOperations.test.js @@ -0,0 +1,41 @@ +import path from 'path'; +import { render } from '@asyncapi/generator-react-sdk'; +import { Parser, fromFile } from '@asyncapi/parser'; +import { RegisterReceiveOperations } from '../../components/RegisterReceiveOperations'; + +const parser = new Parser(); +const asyncapiWebsocketQuery = path.resolve(__dirname, '../../../test/__fixtures__/asyncapi-websocket-components.yml'); + +describe('Testing of RegisterReceiveOperations component', () => { + let parsedAsyncAPIDocument; + + beforeAll(async () => { + const parseResult = await fromFile(parser, asyncapiWebsocketQuery).parse(); + parsedAsyncAPIDocument = parseResult.document; + }); + + test('render RegisterReceiveOperations component with receive operations', () => { + const receiveOperations = parsedAsyncAPIDocument.operations().filterByReceive(); + const result = render(); + const actual = result.trim(); + expect(actual).toMatchSnapshot(); + }); + + test('renders nothing when receive operations is empty', () => { + const result = render(); + const actual = result.trim(); + expect(actual).toBe(''); + }); + + test('renders nothing when receive operations is null', () => { + const result = render(); + const actual = result.trim(); + expect(actual).toBe(''); + }); + + test('renders nothing without receive operations', () => { + const result = render(); + const actual = result.trim(); + expect(actual).toBe(''); + }); +}); diff --git a/packages/templates/clients/websocket/python/test/components/__snapshots__/RegisterReceiveOperations.test.js.snap b/packages/templates/clients/websocket/python/test/components/__snapshots__/RegisterReceiveOperations.test.js.snap new file mode 100644 index 0000000000..9f0b8760b8 --- /dev/null +++ b/packages/templates/clients/websocket/python/test/components/__snapshots__/RegisterReceiveOperations.test.js.snap @@ -0,0 +1,83 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Testing of RegisterReceiveOperations component render RegisterReceiveOperations component with receive operations 1`] = ` +"def register_receive_message_handler(self, handler, discriminator_key=None, discriminator_value=None): + \\"\\"\\" + Register a handler for receiveMessage operation. + + Args: + handler (callable): Handler function that receives raw_message as argument + discriminator_key (str): Message field to use for routing (e.g., 'type', 'event_type') + discriminator_value (str): Expected value for routing (e.g., 'hello', 'user_joined') + \\"\\"\\" + if not callable(handler): + print(\\"Handler must be callable\\") + return + + # Validate that either both discriminator_key and discriminator_value are provided or neither + if (discriminator_key is not None and discriminator_value is None) or (discriminator_key is None and discriminator_value is not None): + print(\\"Error: Both discriminator_key and discriminator_value must be provided together\\") + return + + # Register handler + self.receive_operation_handlers[\\"receiveMessage\\"] = handler + + # Add discriminator entry to the list if both key and value are provided + if discriminator_key is not None and discriminator_value is not None: + discriminator_entry = { + \\"key\\": discriminator_key, + \\"value\\": discriminator_value, + \\"operation_id\\": \\"receiveMessage\\" + } + + # Check if this discriminator already exists + exists = any( + d.get(\\"key\\") == discriminator_key and + d.get(\\"value\\") == discriminator_value and + d.get(\\"operation_id\\") == \\"receiveMessage\\" + for d in self.receive_operation_discriminators + ) + + if not exists: + self.receive_operation_discriminators.append(discriminator_entry) + + def register_receive_notification_handler(self, handler, discriminator_key=None, discriminator_value=None): + \\"\\"\\" + Register a handler for receiveNotification operation. + + Args: + handler (callable): Handler function that receives raw_message as argument + discriminator_key (str): Message field to use for routing (e.g., 'type', 'event_type') + discriminator_value (str): Expected value for routing (e.g., 'hello', 'user_joined') + \\"\\"\\" + if not callable(handler): + print(\\"Handler must be callable\\") + return + + # Validate that either both discriminator_key and discriminator_value are provided or neither + if (discriminator_key is not None and discriminator_value is None) or (discriminator_key is None and discriminator_value is not None): + print(\\"Error: Both discriminator_key and discriminator_value must be provided together\\") + return + + # Register handler + self.receive_operation_handlers[\\"receiveNotification\\"] = handler + + # Add discriminator entry to the list if both key and value are provided + if discriminator_key is not None and discriminator_value is not None: + discriminator_entry = { + \\"key\\": discriminator_key, + \\"value\\": discriminator_value, + \\"operation_id\\": \\"receiveNotification\\" + } + + # Check if this discriminator already exists + exists = any( + d.get(\\"key\\") == discriminator_key and + d.get(\\"value\\") == discriminator_value and + d.get(\\"operation_id\\") == \\"receiveNotification\\" + for d in self.receive_operation_discriminators + ) + + if not exists: + self.receive_operation_discriminators.append(discriminator_entry)" +`; diff --git a/packages/templates/clients/websocket/test/__fixtures__/asyncapi-websocket-components.yml b/packages/templates/clients/websocket/test/__fixtures__/asyncapi-websocket-components.yml index 36f3d796de..acb65a9119 100644 --- a/packages/templates/clients/websocket/test/__fixtures__/asyncapi-websocket-components.yml +++ b/packages/templates/clients/websocket/test/__fixtures__/asyncapi-websocket-components.yml @@ -48,5 +48,21 @@ operations: channel: $ref: '#/channels/testChannel' summary: Send notification + messages: + - $ref: '#/channels/testChannel/messages/testMessage' + + receiveMessage: + action: receive + channel: + $ref: '#/channels/testChannel' + summary: Receive a message + messages: + - $ref: '#/channels/testChannel/messages/testMessage' + + receiveNotification: + action: receive + channel: + $ref: '#/channels/testChannel' + summary: Receive notification messages: - $ref: '#/channels/testChannel/messages/testMessage' \ No newline at end of file