|
| 1 | +const { |
| 2 | + createKeyRegex, |
| 3 | + createPathFilter, |
| 4 | + filterPaths, |
| 5 | + createViewFilter, |
| 6 | + filterViews, |
| 7 | +} = require('../src/adapter-utils'); |
| 8 | + |
| 9 | +test('createKeyRegex', () => { |
| 10 | + expect(createKeyRegex('/path').test('/pathname')).toBe(true); |
| 11 | + expect(createKeyRegex('/p*th').test('/pathname')).toBe(true); |
| 12 | + expect(createKeyRegex('/path').test('/athname')).toBe(false); |
| 13 | +}); |
| 14 | + |
| 15 | +test('createPathFilter with ignoreWildcard', () => { |
| 16 | + const keys = ['/path', '/other-path', '/pathname', '/p-ath']; |
| 17 | + const filterFunc = createPathFilter({ |
| 18 | + ignoreWildcard: true, |
| 19 | + pathname: '/path', |
| 20 | + }); |
| 21 | + |
| 22 | + expect(keys.filter(filterFunc)).toEqual(['/path', '/pathname']); |
| 23 | +}); |
| 24 | + |
| 25 | +test('createPathFilter without ignoreWildcard', () => { |
| 26 | + const keys = ['/path', '/other-path', '/pathname', '/p-ath']; |
| 27 | + const filterFunc = createPathFilter({ |
| 28 | + ignoreWildcard: false, |
| 29 | + pathname: '/p*ath', |
| 30 | + }); |
| 31 | + |
| 32 | + expect(keys.filter(filterFunc)).toEqual(['/path', '/pathname', '/p-ath']); |
| 33 | +}); |
| 34 | + |
| 35 | +test('filterPaths should return array when paths is undefined', () => { |
| 36 | + expect(filterPaths(undefined)).toEqual([]); |
| 37 | +}); |
| 38 | + |
| 39 | +test('filterPaths with ignoreWildcard', () => { |
| 40 | + const keys = ['/path', '/other-path', '/pathname', '/p-ath']; |
| 41 | + const filtered = filterPaths(keys, { |
| 42 | + ignoreWildcard: true, |
| 43 | + pathname: '/path', |
| 44 | + }); |
| 45 | + |
| 46 | + expect(filtered).toEqual(['/path', '/pathname']); |
| 47 | +}); |
| 48 | + |
| 49 | +test('filterPaths without ignoreWildcard', () => { |
| 50 | + const keys = ['/path', '/other-path', '/pathname', '/p-ath']; |
| 51 | + const filtered = filterPaths(keys, { |
| 52 | + ignoreWildcard: false, |
| 53 | + pathname: '/p*ath', |
| 54 | + }); |
| 55 | + |
| 56 | + expect(filtered).toEqual(['/path', '/pathname', '/p-ath']); |
| 57 | +}); |
| 58 | + |
| 59 | +const one = { time: new Date(2017, 4, 4, 1, 0).getTime() }; |
| 60 | +const two = { time: new Date(2017, 4, 4, 2, 0).getTime() }; |
| 61 | +const three = { time: new Date(2017, 4, 4, 3, 0).getTime() }; |
| 62 | +const four = { time: new Date(2017, 4, 4, 4, 0).getTime() }; |
| 63 | +const views = [one, two, three, four]; |
| 64 | + |
| 65 | +const after = new Date(2017, 4, 4, 2, 30).getTime(); |
| 66 | +const before = new Date(2017, 4, 4, 3, 30).getTime(); |
| 67 | + |
| 68 | +test('createViewFilter should with before filter', () => { |
| 69 | + expect(views.filter(createViewFilter({ before }))).toEqual([one, two, three]); |
| 70 | +}); |
| 71 | + |
| 72 | +test('createViewFilter should with after filter', () => { |
| 73 | + expect(views.filter(createViewFilter({ after }))).toEqual([three, four]); |
| 74 | +}); |
| 75 | + |
| 76 | +test('createViewFilter should with after and before filter', () => { |
| 77 | + expect(views.filter(createViewFilter({ after, before }))).toEqual([three]); |
| 78 | +}); |
| 79 | + |
| 80 | +test('filterViews should return array when views is undefined', () => { |
| 81 | + expect(filterViews(undefined)).toEqual([]); |
| 82 | +}); |
| 83 | + |
| 84 | +test('filterViews should not filter without options', () => { |
| 85 | + expect(filterViews(views)).toEqual([one, two, three, four]); |
| 86 | +}); |
| 87 | + |
| 88 | +test('filterViews should with before filter', () => { |
| 89 | + expect(filterViews(views, { before })).toEqual([one, two, three]); |
| 90 | +}); |
| 91 | + |
| 92 | +test('filterViews should with after filter', () => { |
| 93 | + expect(filterViews(views, { after })).toEqual([three, four]); |
| 94 | +}); |
| 95 | + |
| 96 | +test('filterViews should with after and before filter', () => { |
| 97 | + expect(filterViews(views, { after, before })).toEqual([three]); |
| 98 | +}); |
0 commit comments