11const path = require ( 'path' ) ;
22const { Parser, fromFile } = require ( '@asyncapi/parser' ) ;
3- const { getOperationMessages } = require ( '@asyncapi/generator-helpers' ) ;
3+ const { getOperationMessages, getMessageExamples } = require ( '@asyncapi/generator-helpers' ) ;
44
55const parser = new Parser ( ) ;
66const 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+
814describe ( '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