Skip to content

test: add tests for bindings.js#getQueryParams #1576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e2b4f5d
test: bindings.js#getQueryParams
achaljhawar May 24, 2025
e36c05a
fix: undefined channel.bindings() bug
achaljhawar May 24, 2025
be65fb2
fix: resolve ESLint violations in bindings test
achaljhawar May 24, 2025
333433d
refactor: created a TEST_CHANNEL_NAME variable
achaljhawar May 26, 2025
546a041
Merge branch 'master' into master
achaljhawar May 26, 2025
3f31e79
test: use test fixtures in bindings.test.js
achaljhawar May 26, 2025
f0fbbcc
Merge branch 'master' into master
achaljhawar May 29, 2025
196311d
Merge branch 'master' into master
achaljhawar May 30, 2025
024e954
test: fixed getQueryParams tests by removing mocks
achaljhawar May 30, 2025
b61121a
Merge branch 'master' into master
achaljhawar Jun 2, 2025
ee08acc
Merge branch 'master' into master
achaljhawar Jun 2, 2025
e69f4b1
refactor: use Map.delete() instead of filtering for test channels
achaljhawar Jun 2, 2025
16c0f42
Merge branch 'master' into master
achaljhawar Jun 4, 2025
2011e46
Merge branch 'master' into master
achaljhawar Jun 5, 2025
6bdaf8f
refactor: Simplify createChannelsWithOnly in bindings.test.js
achaljhawar Jun 5, 2025
bda84bc
Merge branch 'master' of https://github.com/achaljhawar/generator
achaljhawar Jun 5, 2025
b89c84d
Merge branch 'master' into master
achaljhawar Jun 10, 2025
d1b4adc
refactor: changed import for getQueryParams
achaljhawar Jun 12, 2025
5a09394
Merge branch 'master' of https://github.com/achaljhawar/generator
achaljhawar Jun 12, 2025
bb9a194
Merge branch 'master' into master
achaljhawar Jun 12, 2025
7d9eb64
refactor: eliminated code duplication in bindings.test.js
achaljhawar Jun 12, 2025
73aa16a
docs: add comments explaining mocking and Map copying in bindings tests
achaljhawar Jun 12, 2025
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
6 changes: 3 additions & 3 deletions packages/helpers/src/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ function getQueryParams(channels) {

const queryMap = new Map();

const hasWsBinding = channel?.bindings?.().has('ws');
const bindings = channel?.bindings?.();
const hasWsBinding = bindings?.has('ws');

if (!hasWsBinding) {
return null;
}

const wsBinding = channel.bindings().get('ws');

const wsBinding = bindings.get('ws');
const query = wsBinding.value()?.query;
//we do not throw error, as user do not have to use query params, we just exit with null as it doesn't make sense to continue with query building
if (!query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ channels:
payload: false
- name: number
payload: 123
wsBindingNoQuery:
address: '/no-query'
bindings:
ws:
bindingVersion: 0.1.0
wsBindingEmptyQuery:
address: '/empty-query'
bindings:
ws:
bindingVersion: 0.1.0
query:
type: object

emptyChannel: {}

marketDataV1NoBinding:
Expand Down Expand Up @@ -146,4 +159,4 @@ components:
enum:
- btcusd
- ethbtc
- ethusd
- ethusd
107 changes: 107 additions & 0 deletions packages/helpers/test/bindings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const path = require('path');
const { Parser, fromFile } = require('@asyncapi/parser');
const { getQueryParams } = require('../src/bindings');

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

describe('getQueryParams integration test with AsyncAPI', () => {
let parsedAsyncAPIDocument;

beforeAll(async () => {
const parseResult = await fromFile(parser, asyncapi_v3_path).parse();
parsedAsyncAPIDocument = parseResult.document;
});

it('should extract query parameters from WebSocket binding with properties', () => {
const channels = parsedAsyncAPIDocument.channels();
const params = getQueryParams(channels);

expect(params).not.toBeNull();
expect(params.get('heartbeat')).toBe('false');
expect(params.get('top_of_book')).toBe('false');
expect(params.get('bids')).toBe('true');
expect(params.get('offers')).toBe('');
});

it('should return null for channel without WebSocket binding', () => {
const channels = parsedAsyncAPIDocument.channels();

const filteredChannels = new Map();
const channelWithoutBinding = channels.get('marketDataV1NoBinding');
if (channelWithoutBinding) {
filteredChannels.set('marketDataV1NoBinding', channelWithoutBinding);
}

filteredChannels.isEmpty = function() { return this.size === 0; };
filteredChannels.all = function() { return this; };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of doing this, and need for mocking

how about just using delete on the map? deleting channels that are not needed for given test case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey, I see you did some updates but you did not answer the question

Copy link
Contributor Author

@achaljhawar achaljhawar Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of doing this, and need for mocking

how about just using delete on the map? deleting channels that are not needed for given test case?

Yeah, this can be done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome, than please proceed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you check my latest commit I used Map.delete() to implement the fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed the variable names accordingly


const params = getQueryParams(filteredChannels);
expect(params).toBeNull();
});

it('should return null for empty channels map', () => {
const emptyChannels = new Map();
emptyChannels.isEmpty = function() { return this.size === 0; };
emptyChannels.all = function() { return this; };

const params = getQueryParams(emptyChannels);
expect(params).toBeNull();
});

it('should return null for channel with empty binding', () => {
const channels = parsedAsyncAPIDocument.channels();

const filteredChannels = new Map();
const emptyChannel = channels.get('emptyChannel');
if (emptyChannel) {
filteredChannels.set('emptyChannel', emptyChannel);
}

filteredChannels.isEmpty = function() { return this.size === 0; };
filteredChannels.all = function() { return this; };

const params = getQueryParams(filteredChannels);
expect(params).toBeNull();
});

it('should return null if WebSocket binding exists but has no query parameters', () => {
const channels = parsedAsyncAPIDocument.channels();
const filteredChannels = new Map();
const channelWithNoQuery = channels.get('wsBindingNoQuery');
if (channelWithNoQuery) {
filteredChannels.set('wsBindingNoQuery', channelWithNoQuery);
}

filteredChannels.isEmpty = function() { return this.size === 0; };
filteredChannels.all = function() { return this; };

const params = getQueryParams(filteredChannels);
expect(params).toBeNull();
});

it('should return null if WebSocket binding query exists but has no properties', () => {
const channels = parsedAsyncAPIDocument.channels();
const filteredChannels = new Map();
const channelWithEmptyQuery = channels.get('wsBindingEmptyQuery');
if (channelWithEmptyQuery) {
filteredChannels.set('wsBindingEmptyQuery', channelWithEmptyQuery);
}

filteredChannels.isEmpty = function() { return this.size === 0; };
filteredChannels.all = function() { return this; };

const params = getQueryParams(filteredChannels);
expect(params).toBeNull();
});

it('should return null if channel bindings is undefined', () => {
const filteredChannels = new Map();
filteredChannels.set('test', { bindings: () => undefined });
filteredChannels.isEmpty = function() { return this.size === 0; };
filteredChannels.all = function() { return this; };

const params = getQueryParams(filteredChannels);
expect(params).toBeNull();
});
});