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
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { BSON } from 'bson';
import reducer, {
initialState,
documentsReceived,
Expand All @@ -8,6 +9,7 @@ import reducer, {
themeColorsReceived,
selectDocumentQuery,
getInitialSort,
getInitialQuery,
isBasicQuery,
nextPageRequested,
previousPageRequested,
Expand Down Expand Up @@ -195,6 +197,25 @@ describe('documentQuerySlice', function () {
});
});

describe('getInitialQuery', function () {
afterEach(function () {
delete window.MDB_DATA_BROWSING_OPTIONS;
});

it('should return null when window.MDB_DATA_BROWSING_OPTIONS is undefined', function () {
delete window.MDB_DATA_BROWSING_OPTIONS;
expect(getInitialQuery()).to.be.null;
});

it('should return null when deserializeBSON returns undefined', function () {
// Create a BSON payload without a `value` field so deserializeBSON(data).value is undefined.
const queryString = Buffer.from(BSON.serialize({})).toString('base64');
window.MDB_DATA_BROWSING_OPTIONS = { query: queryString };

expect(getInitialQuery()).to.be.null;
});
});

describe('SORT_VALUE_MAP', function () {
it('should have exactly three keys', function () {
expect(Object.keys(SORT_VALUE_MAP)).to.have.lengthOf(3);
Expand Down Expand Up @@ -347,6 +368,11 @@ describe('documentQuerySlice', function () {
});

describe('isBasicQuery', function () {
it('returns true when query is undefined', function () {
const state = { ...initialState, query: undefined };
expect(isBasicQuery(state)).to.be.true;
});

it('returns true when query is null', function () {
const state = { ...initialState, query: null };
expect(isBasicQuery(state)).to.be.true;
Expand Down
4 changes: 2 additions & 2 deletions src/views/data-browsing-app/store/documentQuerySlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const DEFAULT_ITEMS_PER_PAGE = 10;
export const isBasicQuery = (
state: any,
): state is DocumentQueryState & { query: null } => {
Comment thread
lerouxb marked this conversation as resolved.
return state.query === null;
return !state.query;
};

const recalculatePaginationValues = (
Expand Down Expand Up @@ -168,7 +168,7 @@ export const getInitialQuery = (): ServiceProviderQuery | null => {
window.MDB_DATA_BROWSING_OPTIONS?.query
) {
const queryString = window.MDB_DATA_BROWSING_OPTIONS.query;
return deserializeBSON(queryString) as ServiceProviderQuery;
return (deserializeBSON(queryString) as ServiceProviderQuery) || null;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The real bug is that some things deserialize to undefined and it should be either a ServiceProviderQuery or null

}
return null;
};
Expand Down
Loading