Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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(<RegisterReceiveOperations receiveOperations={receiveOperations} />);
const actual = result.trim();
expect(actual).toMatchSnapshot();
});

test('renders nothing when receive operations is empty', () => {
const result = render(<RegisterReceiveOperations receiveOperations={[]} />);
const actual = result.trim();
expect(actual).toBe('');
});

test('renders nothing when receive operations is null', () => {
const result = render(<RegisterReceiveOperations receiveOperations={null} />);
const actual = result.trim();
expect(actual).toBe('');
});

test('renders nothing without receive operations', () => {
const result = render(<RegisterReceiveOperations />);
const actual = result.trim();
expect(actual).toBe('');
});
});
Original file line number Diff line number Diff line change
@@ -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)"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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'