Skip to content

Commit

Permalink
fix(odata): fix range filtering with ".."
Browse files Browse the repository at this point in the history
- range filter was no longer working for GraphQL backend Service
  • Loading branch information
ghiscoding committed Jan 18, 2022
1 parent 07cf12a commit b07af88
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
54 changes: 42 additions & 12 deletions packages/graphql/src/services/__tests__/graphql.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@ function removeSpaces(text: string) {
return `${text}`.replace(/\s+/g, '');
}

const gridOptionMock = {
enablePagination: true,
defaultFilterRangeOperator: OperatorType.rangeInclusive,
backendServiceApi: {
service: undefined,
options: { datasetName: '' },
preProcess: jest.fn(),
process: jest.fn(),
postProcess: jest.fn(),
} as unknown as GraphqlServiceApi
} as unknown as GridOption;
let gridOptionMock: GridOption;

const gridStub = {
autosizeColumns: jest.fn(),
Expand Down Expand Up @@ -71,7 +61,17 @@ describe('GraphqlService', () => {
pageSize: 10,
totalItems: 100
};
gridOptionMock.backendServiceApi!.service = service as unknown as BackendService;
gridOptionMock = {
enablePagination: true,
defaultFilterRangeOperator: OperatorType.rangeInclusive,
backendServiceApi: {
service: service as unknown as BackendService,
options: { datasetName: '' },
preProcess: jest.fn(),
process: jest.fn(),
postProcess: jest.fn(),
} as unknown as GraphqlServiceApi
} as unknown as GridOption;
jest.spyOn(gridStub, 'getColumns').mockReturnValue(mockColumns);
});

Expand Down Expand Up @@ -939,6 +939,36 @@ describe('GraphqlService', () => {
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
});

it('should return a query to filter a search value between an inclusive range of numbers using the 2 dots (..) separator, "defaultFilterRangeOperator" is not set and operator is not set to "RangeInclusive" or "RangeExclusive"', () => {
const expectation = `query{users(first:10, offset:0, filterBy:[{field:duration, operator:LE, value:"5"}]) { totalCount,nodes{ id,company,gender,name } }}`;
const mockColumnDuration = { id: 'duration', field: 'duration', type: FieldType.number } as Column;
const mockColumnFilters = {
duration: { columnId: 'duration', columnDef: mockColumnDuration, searchTerms: ['..5'], operator: OperatorType.contains, type: FieldType.number },
} as ColumnFilters;
gridOptionMock.defaultFilterRangeOperator = undefined;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(removeSpaces(query)).toBe(removeSpaces(expectation));
});

it('should return a query to filter a search value between an exclusive range of numbers using the 2 dots (..) separator, "defaultFilterRangeOperator" is set to "rangeExclusive" and operator is not set to "RangeInclusive" or "RangeExclusive"', () => {
const expectation = `query{users(first:10, offset:0, filterBy:[{field:duration, operator:LT, value:"5"}]) { totalCount,nodes{ id,company,gender,name } }}`;
const mockColumnDuration = { id: 'duration', field: 'duration', type: FieldType.number } as Column;
const mockColumnFilters = {
duration: { columnId: 'duration', columnDef: mockColumnDuration, searchTerms: ['..5'], operator: OperatorType.contains, type: FieldType.number },
} as ColumnFilters;
gridOptionMock.defaultFilterRangeOperator = OperatorType.rangeExclusive;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(removeSpaces(query)).toBe(removeSpaces(expectation));
});

it('should return a query with search having a range of exclusive dates when the search value contains 2 dots (..) to represent a range of dates', () => {
const expectation = `query{users(first:10, offset:0, filterBy:[{field:startDate, operator:GE, value:"2001-01-01"}, {field:startDate, operator:LE, value:"2001-01-31"}]) { totalCount,nodes{ id,company,gender,name } }}`;
const mockColumn = { id: 'startDate', field: 'startDate' } as Column;
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/services/graphql.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ export class GraphqlService implements BackendService {
}

if (Array.isArray(searchTerms) && searchTerms.length === 1 && typeof searchTerms[0] === 'string' && searchTerms[0].indexOf('..') >= 0) {
if (!operator) {
operator = this._gridOptions.defaultFilterRangeOperator as OperatorString;
if (operator !== OperatorType.rangeInclusive && operator !== OperatorType.rangeExclusive) {
operator = this._gridOptions.defaultFilterRangeOperator ?? OperatorType.rangeInclusive;
}
searchTerms = searchTerms[0].split('..', 2);
if (searchTerms[0] === '') {
Expand Down
26 changes: 26 additions & 0 deletions test/cypress/integration/example10.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,32 @@ describe('Example 10 - GraphQL Grid', { retries: 1 }, () => {
});
});

it('should use a range filter when searching with ".."', () => {
cy.get('.slick-header-columns')
.children('.slick-header-left .slick-header-column:nth(0)')
.contains('Name')
.click();

cy.get('.search-filter.filter-name')
.find('input')
.clear()
.type('Anthony Joyner..Ayers Hood');

// wait for the query to finish
cy.get('[data-test=status]').should('contain', 'finished');

cy.get('[data-test=graphql-query-result]')
.should(($span) => {
const text = removeSpaces($span.text()); // remove all white spaces
expect(text).to.eq(removeSpaces(`query { users (first:30,offset:0,
orderBy:[{field:"name",direction:DESC}],
filterBy:[{field:"gender",operator:EQ,value:"female"},{field:"name",operator:GE,value:"Anthony Joyner"},{field:"name",operator:LE,value:"Ayers Hood"},
{field:"company",operator:IN,value:"acme"},{field:"billing.address.zip",operator:GE,value:"11"},
{field:"finish",operator:GE,value:"${presetLowestDay}"},{field:"finish",operator:LE,value:"${presetHighestDay}"}],locale:"en",userId:123)
{totalCount,nodes{id,name,gender,company,billing{address{street,zip}},finish}}}`));
});
});

describe('Set Dynamic Sorting', () => {
it('should click on "Clear all Filters & Sorting" then "Set Dynamic Sorting" buttons', () => {
cy.get('[data-test=clear-filters-sorting]')
Expand Down

0 comments on commit b07af88

Please sign in to comment.