Skip to content

Commit 7e1db1e

Browse files
KunalNasaderberg
andauthored
chore: add test for getMessageExamples helper (#1570)
Co-authored-by: KunalNasa <kunalnasa.dev@gmail.com> Co-authored-by: Lukasz Gornicki <lpgornicki@gmail.com>
1 parent 0174c71 commit 7e1db1e

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

packages/helpers/test/operations.test.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
const path = require('path');
22
const { Parser, fromFile } = require('@asyncapi/parser');
3-
const { getOperationMessages } = require('@asyncapi/generator-helpers');
3+
const { getOperationMessages, getMessageExamples } = require('@asyncapi/generator-helpers');
44

55
const parser = new Parser();
66
const asyncapi_v3_path = path.resolve(__dirname, './__fixtures__/asyncapi-websocket-query.yml');
77

8+
async function getOperationsFromParsedDoc() {
9+
const parseResult = await fromFile(parser, asyncapi_v3_path).parse();
10+
const parsedAsyncAPIDocument = parseResult.document;
11+
return parsedAsyncAPIDocument.operations();
12+
}
13+
814
describe('getOperationMessages integration test with AsyncAPI', () => {
9-
let parsedAsyncAPIDocument, operations;
15+
let operations;
1016

1117
beforeAll(async () => {
12-
const parseResult = await fromFile(parser, asyncapi_v3_path).parse();
13-
parsedAsyncAPIDocument = parseResult.document;
14-
operations = parsedAsyncAPIDocument.operations();
18+
operations = await getOperationsFromParsedDoc();
1519
});
1620

1721
it('should return all messages of an operation when operation exists', () => {
@@ -36,3 +40,38 @@ describe('getOperationMessages integration test with AsyncAPI', () => {
3640
});
3741
});
3842

43+
describe('getMessageExamples integration test with AsyncAPI', () => {
44+
let operations;
45+
46+
beforeAll(async () => {
47+
operations = await getOperationsFromParsedDoc();
48+
});
49+
50+
it('Should throw an error when message is null', () => {
51+
expect(() => getMessageExamples(null)).toThrow('Message object must be provided.');
52+
});
53+
54+
it('Should return all messages examples of an operation when operation exists', () => {
55+
const operation = operations.get('multipleExamples');
56+
const messages = operation.messages().all();
57+
for (const message of messages) {
58+
const expectedMessageExamples = message.examples();
59+
const actualMessageExamples = getMessageExamples(message);
60+
expect(actualMessageExamples).toStrictEqual(expectedMessageExamples);
61+
}
62+
}
63+
);
64+
it('Should return null when no examples present for a message', () => {
65+
const operation = operations.get('noMessageExamples');
66+
const messages = operation.messages().all();
67+
for (const message of messages) {
68+
const actualMessageExamples = getMessageExamples(message);
69+
expect(actualMessageExamples).toBeNull();
70+
}
71+
});
72+
73+
it('should throw error when message is undefined', () => {
74+
expect(() => getMessageExamples(undefined)).toThrow('Message object must be provided.');
75+
});
76+
});
77+

0 commit comments

Comments
 (0)