-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathcombineFilters.spec.js
60 lines (50 loc) · 1.93 KB
/
combineFilters.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { expect, describe, it } from 'vitest'
import { combineFilters } from '../src/index'
describe('Combine Filters', () => {
const sample = {
action: { type: 'TEST' },
currentState: 1,
previousHistory: [0, -1]
}
function checkArguments (action, currentState, previousHistory) {
return (
action === sample.action &&
currentState === sample.currentState &&
previousHistory === sample.previousHistory
)
}
function checkArgumentsNot (action, currentState, previousHistory) {
return (
action !== sample.action ||
currentState !== sample.currentState ||
previousHistory !== sample.previousHistory
)
}
function checkStateNot1 (action, state) { return state !== 1 }
function checkStateNot2 (action, state) { return state !== 2 }
function checkIfCalled (action) {
action.hasBeenCalled = true
return true
}
it('should pass its arguments while calling a filter', () => {
expect(combineFilters(checkArguments, checkArguments)(sample.action, sample.currentState, sample.previousHistory))
.to.equal(true)
expect(combineFilters(checkArgumentsNot, checkArguments)(sample.action, sample.currentState, sample.previousHistory))
.to.equal(false)
})
it('should return false if any filter does', () => {
expect(combineFilters(checkStateNot1, checkStateNot2)(null, 1)).to.equal(false)
expect(combineFilters(checkStateNot1, checkStateNot2)(null, 2)).to.equal(false)
})
it('should return true if every filter does', () => {
expect(combineFilters(checkStateNot1, checkStateNot2)(null, 3)).to.equal(true)
})
it('should not call remaining filters if one already returned false', () => {
const act = { hasBeenCalled: false }
const combined = combineFilters(checkStateNot1, checkStateNot2, checkIfCalled)
combined(act, 2)
expect(act.hasBeenCalled).to.equal(false)
combined(act, 3)
expect(act.hasBeenCalled).to.equal(true)
})
})