|
| 1 | +import { CaseType } from '../../models/caseType'; |
| 2 | +import { OdataQueryBuilderService } from '../odataQueryBuilder.service'; |
| 3 | +import { |
| 4 | + Column, |
| 5 | + ColumnFilter, |
| 6 | + ColumnSort, |
| 7 | + CurrentFilter, |
| 8 | + FilterChangedArgs, |
| 9 | + GridOption, |
| 10 | + MultiColumnSort, |
| 11 | + Pagination, |
| 12 | + ColumnFilters, |
| 13 | + OperatorType, |
| 14 | + FieldType, |
| 15 | + CurrentSorter, |
| 16 | + OdataOption, |
| 17 | +} from '../../models'; |
| 18 | + |
| 19 | +const DEFAULT_ITEMS_PER_PAGE = 25; |
| 20 | +const DEFAULT_PAGE_SIZE = 20; |
| 21 | + |
| 22 | +const gridOptionMock = { |
| 23 | + enablePagination: true, |
| 24 | + backendServiceApi: { |
| 25 | + service: undefined, |
| 26 | + preProcess: jest.fn(), |
| 27 | + process: jest.fn(), |
| 28 | + postProcess: jest.fn(), |
| 29 | + } |
| 30 | +} as GridOption; |
| 31 | + |
| 32 | +const gridStub = { |
| 33 | + autosizeColumns: jest.fn(), |
| 34 | + getColumnIndex: jest.fn(), |
| 35 | + getScrollbarDimensions: jest.fn(), |
| 36 | + getOptions: () => gridOptionMock, |
| 37 | + getColumns: jest.fn(), |
| 38 | + setColumns: jest.fn(), |
| 39 | + registerPlugin: jest.fn(), |
| 40 | + setSelectedRows: jest.fn(), |
| 41 | + setSortColumns: jest.fn(), |
| 42 | +}; |
| 43 | + |
| 44 | +describe('OdataService', () => { |
| 45 | + let mockColumns: Column[]; |
| 46 | + let service: OdataQueryBuilderService; |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + mockColumns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100 }]; |
| 50 | + service = new OdataQueryBuilderService(); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + jest.clearAllMocks(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should create the service', () => { |
| 58 | + expect(service).toBeTruthy(); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('buildQuery method', () => { |
| 62 | + beforeEach(() => { |
| 63 | + service.options = { filterQueue: [], orderBy: '' }; |
| 64 | + }); |
| 65 | + |
| 66 | + it('should throw an error when odata options are null', () => { |
| 67 | + service.options = undefined; |
| 68 | + expect(() => service.buildQuery()).toThrow(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should return a query with $top pagination', () => { |
| 72 | + const expectation = '$top=25'; |
| 73 | + |
| 74 | + service.options = { top: 25 }; |
| 75 | + const query = service.buildQuery(); |
| 76 | + |
| 77 | + expect(query).toBe(expectation); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return a query with $top and $skip pagination', () => { |
| 81 | + const expectation = '$top=25&$skip=10'; |
| 82 | + |
| 83 | + service.options = { top: 25, skip: 10 }; |
| 84 | + const query = service.buildQuery(); |
| 85 | + |
| 86 | + expect(query).toBe(expectation); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return a query with sorting when "orderBy" is provided as a string', () => { |
| 90 | + const expectation = '$top=10&$orderby=Gender asc'; |
| 91 | + |
| 92 | + service.options = { top: 10, orderBy: 'Gender asc' }; |
| 93 | + const query = service.buildQuery(); |
| 94 | + |
| 95 | + expect(query).toBe(expectation); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should return a query with sorting when "orderBy" is provided as an array', () => { |
| 99 | + const expectation = '$top=10&$orderby=Gender asc,Company desc'; |
| 100 | + |
| 101 | + service.options = { top: 10, orderBy: ['Gender asc', 'Company desc'] }; |
| 102 | + const query = service.buildQuery(); |
| 103 | + |
| 104 | + expect(query).toBe(expectation); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return a query with sorting when "filter" is provided as a string', () => { |
| 108 | + const expectation = `$top=10&$filter=(FirstName eq 'John')`; |
| 109 | + |
| 110 | + service.options = { top: 10, filter: `FirstName eq 'John'` }; |
| 111 | + const query = service.buildQuery(); |
| 112 | + |
| 113 | + expect(query).toBe(expectation); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should return a query with sorting when "filterBy" is provided as a string', () => { |
| 117 | + const expectation = `$top=10&$filter=(FirstName eq 'John')`; |
| 118 | + |
| 119 | + service.options = { top: 10, filterBy: `FirstName eq 'John'` }; |
| 120 | + const query = service.buildQuery(); |
| 121 | + |
| 122 | + expect(query).toBe(expectation); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should return a query with sorting when "filterBy" is provided as an array', () => { |
| 126 | + const expectation = `$top=10&$filter=(FirstName eq 'John' and FirstName eq 'Jane')`; |
| 127 | + |
| 128 | + service.options = { top: 10, filter: [`FirstName eq 'John'`, `FirstName eq 'Jane'`] }; |
| 129 | + const query = service.buildQuery(); |
| 130 | + |
| 131 | + expect(query).toBe(expectation); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should return a query with sorting when "filterBy" is provided as an array', () => { |
| 135 | + const expectation = `$top=10&$filter=(FirstName eq 'John' or FirstName eq 'Jane')`; |
| 136 | + |
| 137 | + service.options = { top: 10, filterBy: [`FirstName eq 'John'`, `FirstName eq 'Jane'`], filterBySeparator: 'or' }; |
| 138 | + const query = service.buildQuery(); |
| 139 | + const filterCount = service.getFilterCount(); |
| 140 | + |
| 141 | + expect(query).toBe(expectation); |
| 142 | + expect(filterCount).toBe(2); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('saveColumnFilter method', () => { |
| 147 | + it('should return "columnFilters" object with multiple properties as the filter names', () => { |
| 148 | + service.saveColumnFilter('FirstName', 'John', ['John']); |
| 149 | + |
| 150 | + const columnFilters = service.columnFilters; |
| 151 | + |
| 152 | + expect(columnFilters).toEqual({ FirstName: { search: ['John'], value: 'John' } }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should return "columnFilters" object with multiple properties as the filter names', () => { |
| 156 | + service.saveColumnFilter('FirstName', 'John', ['John']); |
| 157 | + service.saveColumnFilter('LastName', 'Doe', ['Doe']); |
| 158 | + |
| 159 | + const columnFilters = service.columnFilters; |
| 160 | + |
| 161 | + expect(columnFilters).toEqual({ |
| 162 | + FirstName: { search: ['John'], value: 'John' }, |
| 163 | + LastName: { search: ['Doe'], value: 'Doe' } |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + describe('removeColumnFilter method', () => { |
| 169 | + it('should return "columnFilters" object without the one deleted', () => { |
| 170 | + service.saveColumnFilter('FirstName', 'John', ['John']); |
| 171 | + service.saveColumnFilter('LastName', 'Doe', ['Doe']); |
| 172 | + |
| 173 | + service.removeColumnFilter('LastName'); |
| 174 | + const columnFilters = service.columnFilters; |
| 175 | + |
| 176 | + expect(columnFilters).toEqual({ |
| 177 | + FirstName: { search: ['John'], value: 'John' } |
| 178 | + }); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + describe('updateOptions method', () => { |
| 183 | + beforeEach(() => { |
| 184 | + service.updateOptions({ caseType: CaseType.pascalCase }); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should be able to provide "filterBy" array and expect see the query string include the filter', () => { |
| 188 | + const expectation = `$filter=(FirstName eq 'John')`; |
| 189 | + |
| 190 | + service.updateOptions({ filterBy: `FirstName eq 'John'` }); |
| 191 | + const query = service.buildQuery(); |
| 192 | + |
| 193 | + expect(query).toBe(expectation); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should be able to provide "filterBy" array and expect see the query string include the filter', () => { |
| 197 | + const expectation = `$filter=(FirstName eq 'John' and substring(Gender 'male')`; |
| 198 | + |
| 199 | + service.updateOptions({ filterBy: [`FirstName eq 'John'`, `substring(Gender 'male'`] }); |
| 200 | + const query = service.buildQuery(); |
| 201 | + |
| 202 | + expect(query).toBe(expectation); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should be able to provide "orderBy" array and expect see the query string include the filter', () => { |
| 206 | + const expectation = `$orderby=FirstName desc`; |
| 207 | + |
| 208 | + service.updateOptions({ orderBy: 'FirstName desc' }); |
| 209 | + const query = service.buildQuery(); |
| 210 | + |
| 211 | + expect(query).toBe(expectation); |
| 212 | + }); |
| 213 | + |
| 214 | + it('should be able to provide "orderBy" array and expect see the query string include the filter', () => { |
| 215 | + const expectation = `$orderby=FirstName desc,LastName asc`; |
| 216 | + |
| 217 | + service.updateOptions({ orderBy: ['FirstName desc', 'LastName asc'] }); |
| 218 | + const query = service.buildQuery(); |
| 219 | + |
| 220 | + expect(query).toBe(expectation); |
| 221 | + }); |
| 222 | + }); |
| 223 | +}); |
0 commit comments