Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an 'AS IS' BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | import { TableFilterPipe } from './tablefilter.pipe'; |
| 17 | import { TableFilter } from './table.base'; |
| 18 | |
| 19 | describe('TableFilterPipe', () => { |
| 20 | |
| 21 | const pipe = new TableFilterPipe(); |
| 22 | const items: any[] = new Array(); |
| 23 | // Array item 0 |
| 24 | items.push({ |
| 25 | id: 'abc', |
| 26 | title: 'def', |
| 27 | origin: 'ghi' |
| 28 | }); |
| 29 | // Array item 1 |
| 30 | items.push({ |
| 31 | id: 'pqr', |
| 32 | title: 'stu', |
| 33 | origin: 'vwx' |
| 34 | }); |
| 35 | // Array item 2 |
| 36 | items.push({ |
| 37 | id: 'dog', |
| 38 | title: 'mouse', |
| 39 | origin: 'cat' |
| 40 | }); |
| 41 | |
| 42 | |
| 43 | it('create an instance', () => { |
| 44 | expect(pipe).toBeTruthy(); |
| 45 | }); |
| 46 | |
| 47 | it('expect it to handle empty search', () => { |
| 48 | const filteredItems: any[] = |
| 49 | pipe.transform(items, <TableFilter>{queryStr: '', queryBy: 'title'}); |
| 50 | expect(filteredItems).toEqual(items); |
| 51 | }); |
| 52 | |
| 53 | it('expect it to handle empty items', () => { |
| 54 | const filteredItems: any[] = |
| 55 | pipe.transform(new Array(), <TableFilter>{queryStr: 'de', queryBy: 'title'}); |
| 56 | expect(filteredItems).toEqual(new Array()); |
| 57 | }); |
| 58 | |
| 59 | |
| 60 | it('expect it to match 0 by title', () => { |
| 61 | const filteredItems: any[] = |
| 62 | pipe.transform(items, <TableFilter>{queryStr: 'de', queryBy: 'title'}); |
| 63 | expect(filteredItems).toEqual(items.slice(0, 1)); |
| 64 | }); |
| 65 | |
| 66 | it('expect it to match 1 by title', () => { |
| 67 | const filteredItems: any[] = |
| 68 | pipe.transform(items, <TableFilter>{queryStr: 'st', queryBy: 'title'}); |
| 69 | expect(filteredItems).toEqual(items.slice(1, 2)); |
| 70 | }); |
| 71 | |
| 72 | it('expect it to match 1 by uppercase title', () => { |
| 73 | const filteredItems: any[] = |
| 74 | pipe.transform(items, <TableFilter>{queryStr: 'sT', queryBy: 'title'}); |
| 75 | expect(filteredItems).toEqual(items.slice(1, 2)); |
| 76 | }); |
| 77 | |
| 78 | it('expect it to not match by title', () => { |
| 79 | const filteredItems: any[] = |
| 80 | pipe.transform(items, <TableFilter>{queryStr: 'pq', queryBy: 'title'}); |
| 81 | expect(filteredItems.length).toEqual(0); |
| 82 | }); |
| 83 | |
| 84 | it('expect it to match 1 by all fields', () => { |
| 85 | const filteredItems: any[] = |
| 86 | pipe.transform(items, <TableFilter>{queryStr: 'pq', queryBy: '$'}); |
| 87 | expect(filteredItems).toEqual(items.slice(1, 2)); |
| 88 | }); |
| 89 | |
| 90 | it('expect it to not match by all fields', () => { |
| 91 | const filteredItems: any[] = |
| 92 | pipe.transform(items, <TableFilter>{queryStr: 'yz', queryBy: '$'}); |
| 93 | expect(filteredItems.length).toEqual(0); |
| 94 | }); |
| 95 | |
| 96 | /** |
| 97 | * Check that items one and two contain a 't' - title=stu and origin=cat |
| 98 | */ |
| 99 | it('expect it to match 1,2 by all fields', () => { |
| 100 | const filteredItems: any[] = |
| 101 | pipe.transform(items, <TableFilter>{queryStr: 't', queryBy: '$'}); |
| 102 | expect(filteredItems).toEqual(items.slice(1)); |
| 103 | }); |
| 104 | }); |