|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2022 VMware, Inc. All Rights Reserved. |
| 3 | + * This software is released under MIT license. |
| 4 | + * The full license information can be found in LICENSE in the root directory of this project. |
| 5 | + */ |
| 6 | + |
| 7 | +import { Observable } from 'rxjs'; |
| 8 | + |
| 9 | +import { ClrDatagridDateFilterInterface } from '../../interfaces/date-filter.interface'; |
| 10 | +import { ClrDatagridFilterInterface } from '../../interfaces/filter.interface'; |
| 11 | +import { DatagridDateFilterImpl } from './datagrid-date-filter-impl'; |
| 12 | +import { DatagridPropertyDateFilter } from './datagrid-property-date-filter'; |
| 13 | + |
| 14 | +const yesterday = new Date(Date.now() - 24 * 1000 * 60 * 60); |
| 15 | +const today = new Date(); |
| 16 | +const tomorrow = new Date(Date.now() + 24 * 1000 * 60 * 60); |
| 17 | +const dayAfterTomorrow = new Date(Date.now() + 2 * 24 * 1000 * 60 * 60); |
| 18 | +const threeDayFromNow = new Date(Date.now() + 3 * 24 * 1000 * 60 * 60); |
| 19 | + |
| 20 | +export default function (): void { |
| 21 | + describe('DatagridDateFilterImpl', function () { |
| 22 | + let fullFilter: DatagridDateFilterImpl<Date>; |
| 23 | + |
| 24 | + beforeEach(function () { |
| 25 | + const dateFilter = new TestFilter(); |
| 26 | + fullFilter = new DatagridDateFilterImpl(dateFilter); |
| 27 | + }); |
| 28 | + |
| 29 | + it('becomes active when either from or to are set', function () { |
| 30 | + expect(fullFilter.isActive()).toBe(false); |
| 31 | + fullFilter.to = tomorrow; |
| 32 | + expect(fullFilter.isActive()).toBe(true); |
| 33 | + fullFilter.to = null; |
| 34 | + expect(fullFilter.isActive()).toBe(false); |
| 35 | + fullFilter.from = today; |
| 36 | + expect(fullFilter.isActive()).toBe(true); |
| 37 | + fullFilter.from = null; |
| 38 | + expect(fullFilter.isActive()).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it('filters dates that are after the to date', function () { |
| 42 | + expect(fullFilter.accepts(new Date(Date.now() + 11_000))).toBe(true); |
| 43 | + fullFilter.to = tomorrow; |
| 44 | + expect(fullFilter.accepts(dayAfterTomorrow)).toBe(false); |
| 45 | + expect(fullFilter.accepts(today)).toBe(true); |
| 46 | + fullFilter.to = null; |
| 47 | + expect(fullFilter.accepts(dayAfterTomorrow)).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + it('filters dates that are before the from date', function () { |
| 51 | + expect(fullFilter.accepts(yesterday)).toBe(true); |
| 52 | + fullFilter.from = today; |
| 53 | + expect(fullFilter.accepts(yesterday)).toBe(false); |
| 54 | + expect(fullFilter.accepts(tomorrow)).toBe(true); |
| 55 | + fullFilter.from = null; |
| 56 | + expect(fullFilter.accepts(yesterday)).toBe(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it('only allows values within the range when both filters are set', function () { |
| 60 | + fullFilter.from = today; |
| 61 | + fullFilter.to = dayAfterTomorrow; |
| 62 | + expect(fullFilter.accepts(yesterday)).toBe(false); |
| 63 | + expect(fullFilter.accepts(tomorrow)).toBe(true); |
| 64 | + expect(fullFilter.accepts(threeDayFromNow)).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it('exposes state', function () { |
| 68 | + expect(fullFilter.state).toBe(fullFilter); |
| 69 | + }); |
| 70 | + |
| 71 | + it('compares filters', function () { |
| 72 | + let otherFilter: ClrDatagridFilterInterface<any> = fullFilter; |
| 73 | + expect(fullFilter.equals(otherFilter)).toBe(true); |
| 74 | + // Reference only comparison should be enough for the common case |
| 75 | + otherFilter = new DatagridDateFilterImpl(new TestFilter()); |
| 76 | + expect(fullFilter.equals(otherFilter)).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('with DatagridPropertyDateFilter', function () { |
| 80 | + beforeEach(function () { |
| 81 | + const propFilter = new DatagridPropertyDateFilter('a.b.c'); |
| 82 | + fullFilter = new DatagridDateFilterImpl(propFilter); |
| 83 | + }); |
| 84 | + |
| 85 | + it('exposes state', function () { |
| 86 | + fullFilter.to = today; |
| 87 | + fullFilter.from = null; |
| 88 | + expect(fullFilter.state).toEqual({ property: 'a.b.c', to: today, from: null }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('compares filters', function () { |
| 92 | + let otherFilter: ClrDatagridFilterInterface<any> = fullFilter; |
| 93 | + expect(fullFilter.equals(otherFilter)).toBe(true); |
| 94 | + // In the specific case we can compare different filter instances |
| 95 | + otherFilter = new DatagridDateFilterImpl(new DatagridPropertyDateFilter('a.b.c')); |
| 96 | + expect(fullFilter.equals(otherFilter)).toBe(true); |
| 97 | + // Incompatible inner function type |
| 98 | + otherFilter = new DatagridDateFilterImpl(new TestFilter()); |
| 99 | + expect(fullFilter.equals(otherFilter)).toBe(false); |
| 100 | + // Incompatible filter object |
| 101 | + otherFilter = new IncompatibleFilter(); |
| 102 | + expect(fullFilter.equals(otherFilter)).toBe(false); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +class TestFilter implements ClrDatagridDateFilterInterface<Date> { |
| 109 | + accepts(item: Date, from: Date, to: Date) { |
| 110 | + if (from !== null && item < from) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + if (to !== null && item > to) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + return true; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +class IncompatibleFilter implements ClrDatagridFilterInterface<Date> { |
| 122 | + // eslint-disable-next-line |
| 123 | + accepts(_item: Date): boolean { |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + changes: Observable<any>; |
| 128 | + |
| 129 | + isActive(): boolean { |
| 130 | + return true; |
| 131 | + } |
| 132 | +} |
0 commit comments