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 all 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
13 changes: 13 additions & 0 deletions packages/helpers/test/__fixtures__/asyncapi-websocket-query.yml
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
80 changes: 80 additions & 0 deletions packages/helpers/test/bindings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const path = require('path');
const { Parser, fromFile } = require('@asyncapi/parser');
const { getQueryParams } = require('@asyncapi/generator-helpers');
const parser = new Parser();
const asyncapi_v3_path = path.resolve(__dirname, './__fixtures__/asyncapi-websocket-query.yml');

function createChannelsWithOnly(keysToKeep, originalChannels) {
Copy link
Member

Choose a reason for hiding this comment

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

why we need to create a new map here, and still do some mocking basically of all and isEmpty

Copy link
Member

Choose a reason for hiding this comment

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

hey are you still working on something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No

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 am done ig

Copy link
Member

Choose a reason for hiding this comment

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

why we need to create a new map here, and still do some mocking basically of all and isEmpty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ohk I'll fix this

Copy link
Contributor Author

@achaljhawar achaljhawar Jun 9, 2025

Choose a reason for hiding this comment

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

should I create a second fixture file to fix this problem?
or should I test with the full channel object and verify its behaviour?

Copy link
Member

Choose a reason for hiding this comment

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

let's not decide yet, first answer my question 😄 why new map is needed, why you cannot operate in the helper on the map you get as input

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new Map is needed to create a copy so the original channels map isn't mutated, which would affect other tests that need the data.

Copy link
Member

Choose a reason for hiding this comment

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

oh, now much better. Remember reviewer did not code and do not see what issues you had during development. Sometimes we just ask question not to play smart, but to really get an answer 😄 thanks for the context - now it makes sense why you've done so, and why later you still do mocking of all and isEmpty - please add comment to tested code to explain why this mocking is needed

// Create a copy to avoid mutating the original channels map -
// other tests need the original data intact
const channelsMap = new Map(originalChannels.all());
for (const key of channelsMap.keys()) {
if (!keysToKeep.includes(key)) {
channelsMap.delete(key);
}
}
return channelsMap;
}

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

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

// Helper function to create filtered channels and get query params
const getQueryParamsForChannels = (channelNames) => {
const channels = parsedAsyncAPIDocument.channels();
const filteredChannelsMap = createChannelsWithOnly(channelNames, channels);
// Mock the channels interface that getQueryParams() expects - it needs
// isEmpty() and all() methods, not direct Map access
return getQueryParams({
isEmpty: () => filteredChannelsMap.size === 0,
all: () => filteredChannelsMap
});
};

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 params = getQueryParamsForChannels(['marketDataV1NoBinding']);
expect(params).toBeNull();
});

it('should return null for empty channels map', () => {
// Mock channels object to simulate empty state - getQueryParams() expects
// channels with isEmpty() and all() methods, not a plain Map
const emptyChannels = {
isEmpty: () => true,
all: () => new Map()
};
const params = getQueryParams(emptyChannels);
expect(params).toBeNull();
});

it('should return null for channel with empty binding', () => {
const params = getQueryParamsForChannels(['emptyChannel']);
expect(params).toBeNull();
});

it('should return null if WebSocket binding exists but has no query parameters', () => {
const params = getQueryParamsForChannels(['wsBindingNoQuery']);
expect(params).toBeNull();
});

it('should return null if WebSocket binding query exists but has no properties', () => {
const params = getQueryParamsForChannels(['wsBindingEmptyQuery']);
expect(params).toBeNull();
});
});