Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Nov 7, 2023
1 parent 606eb12 commit 3d0a0b6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/QueryBuilder/QueryBuilder/helpers/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,14 @@ const getSourceFields = (field) => ({
},
}[field]);

const getFormattedSourceField = async ({ item, intl, booleanOptions, fieldOptions, getParamsSource }) => {
const getFormattedSourceField = async ({ item, intl, booleanOptions, fieldOptions, getParamsSource, boolean }) => {
const [field, query] = Object.entries(item)[0];
const mongoOperator = Object.keys(query)[0];
const mongoValue = query[mongoOperator];

const { operator, value } = getSourceFields(mongoOperator)(mongoValue);

if (operator && value) {
const boolean = BOOLEAN_OPERATORS.AND;
const fieldItem = fieldOptions.find(f => f.value === field);
const { dataType, values, source } = fieldItem;
const hasSourceOrValues = values || source;
Expand Down Expand Up @@ -169,6 +168,8 @@ const getFormattedSourceField = async ({ item, intl, booleanOptions, fieldOption
value: { current: formattedValue || value, source, options: values },
};
}

return null;
};

export const mongoQueryToSource = async ({
Expand All @@ -190,6 +191,7 @@ export const mongoQueryToSource = async ({
for (const item of initialValues[key]) {
const formattedItem = await getFormattedSourceField({
item,
boolean: key,
...sharedArgs,
});

Expand All @@ -201,6 +203,7 @@ export const mongoQueryToSource = async ({

const singleItem = await getFormattedSourceField({
item: initialValues,
boolean: '',
...sharedArgs,
});

Expand Down
26 changes: 26 additions & 0 deletions src/QueryBuilder/QueryBuilder/helpers/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ describe('mongoQueryToSource()', () => {
expect(result).toEqual([]);
});

const singleSource = [{
boolean: { options: [{ label: 'AND', value: '' }], current: '' },
field: { options: fieldOptions, current: 'user_first_name', dataType: 'stringType' },
operator: { options: expect.any(Array), current: OPERATORS.EQUAL, dataType: 'stringType' },
value: { current: 'value', options: undefined, source: undefined },
}];

const source = [
{
boolean: { options: booleanOptions, current: '$and' },
Expand Down Expand Up @@ -132,6 +139,17 @@ describe('mongoQueryToSource()', () => {
})));
});

it('should convert single query without operators to source format', async () => {
const result = await mongoQueryToSource({
initialValues: { user_first_name: { $eq: 'value' } },
booleanOptions: [{ label: 'AND', value: '' }],
fieldOptions,
intl: { formatMessage: jest.fn() },
});

expect(result).toEqual(singleSource);
});

it('should convert from source to simple query format', () => {
const initial = {
$and: [
Expand All @@ -152,4 +170,12 @@ describe('mongoQueryToSource()', () => {

expect(result).toEqual(initial);
});

it('should convert from SINGLE source to simple query format', () => {
const initial = { user_first_name: { $eq: 'value' } };

const result = sourceToMongoQuery(singleSource);

expect(result).toEqual(initial);
});
});

0 comments on commit 3d0a0b6

Please sign in to comment.