1
+ import { reduceDetectors } from "../src" ;
1
2
2
- import { reduceDetectors } from '../src' ;
3
-
4
- describe ( 'reduceDetectors' , ( ) => {
5
- it ( 'should export reduceDetectors function' , ( ) => {
3
+ describe ( "reduceDetectors" , ( ) => {
4
+ it ( "should export composeDetectors function" , ( ) => {
6
5
expect ( reduceDetectors ) . toBeInstanceOf ( Function ) ;
7
6
} ) ;
8
7
9
- it ( ' should return valid detector for reduction of two detectors' , ( ) => {
8
+ it ( " should return valid detector for composition of two detectors" , ( ) => {
10
9
function detectorA ( ) {
11
- return [ { type : ' ACTION_A' } , { type : ' ACTION_B' } ] ;
10
+ return [ { type : " ACTION_A" } , { type : " ACTION_B" } ] ;
12
11
}
13
12
14
13
function detectorB ( ) {
15
- return [ { type : ' ACTION_C' } ] ;
14
+ return [ { type : " ACTION_C" } ] ;
16
15
}
17
16
18
17
const detectorAB = reduceDetectors ( detectorA , detectorB ) ;
@@ -21,75 +20,90 @@ describe('reduceDetectors', () => {
21
20
expect ( detectorAB ( { } , { } ) ) . toBeInstanceOf ( Array ) ;
22
21
23
22
// we check it twice to be sure that detectorAB doesn't has any internal state.
24
- expect ( detectorAB ( { } , { } ) ) . toEqual ( [ { type : 'ACTION_A' } , { type : 'ACTION_B' } , { type : 'ACTION_C' } ] ) ;
25
- expect ( detectorAB ( { } , { } ) ) . toEqual ( [ { type : 'ACTION_A' } , { type : 'ACTION_B' } , { type : 'ACTION_C' } ] ) ;
23
+ expect ( detectorAB ( { } , { } ) ) . toEqual ( [
24
+ { type : "ACTION_A" } ,
25
+ { type : "ACTION_B" } ,
26
+ { type : "ACTION_C" }
27
+ ] ) ;
28
+ expect ( detectorAB ( { } , { } ) ) . toEqual ( [
29
+ { type : "ACTION_A" } ,
30
+ { type : "ACTION_B" } ,
31
+ { type : "ACTION_C" }
32
+ ] ) ;
26
33
} ) ;
27
34
28
- it ( ' should pass states in valid order for reduction of two detectors' , ( ) => {
35
+ it ( " should pass states in valid order for composition of two detectors" , ( ) => {
29
36
function detectorA ( prevState ?: number , nextState ?: number ) {
30
37
if ( prevState && nextState && prevState > nextState ) {
31
- return [ { type : ' PREV_STATE_GREATER' } ] ;
38
+ return [ { type : " PREV_STATE_GREATER" } ] ;
32
39
}
33
40
34
41
return [ ] ;
35
42
}
36
43
37
44
function detectorB ( prevState ?: number , nextState ?: number ) {
38
45
if ( prevState && nextState && nextState > prevState ) {
39
- return [ { type : ' NEXT_STATE_GREATER' } ] ;
46
+ return [ { type : " NEXT_STATE_GREATER" } ] ;
40
47
}
41
48
42
49
return [ ] ;
43
50
}
44
51
45
52
const detectorAB = reduceDetectors ( detectorA , detectorB ) ;
46
53
47
- expect ( detectorAB ( - 10 , 50 ) ) . toEqual ( [ { type : ' NEXT_STATE_GREATER' } ] ) ;
48
- expect ( detectorAB ( 30 , 20 ) ) . toEqual ( [ { type : ' PREV_STATE_GREATER' } ] ) ;
54
+ expect ( detectorAB ( - 10 , 50 ) ) . toEqual ( [ { type : " NEXT_STATE_GREATER" } ] ) ;
55
+ expect ( detectorAB ( 30 , 20 ) ) . toEqual ( [ { type : " PREV_STATE_GREATER" } ] ) ;
49
56
} ) ;
50
57
51
- it ( ' should allow to reduce detectors with undefined result on no-action detect' , ( ) => {
58
+ it ( " should allow to compose detectors with undefined result on no-action detect" , ( ) => {
52
59
function detectorA ( prevState ?: number , nextState ?: number ) {
53
60
if ( prevState && nextState && prevState > nextState ) {
54
- return [ { type : ' PREV_STATE_GREATER' } ] ;
61
+ return [ { type : " PREV_STATE_GREATER" } ] ;
55
62
}
56
63
}
57
64
58
65
function detectorB ( prevState ?: number , nextState ?: number ) {
59
66
if ( prevState && nextState && nextState > prevState ) {
60
- return [ { type : ' NEXT_STATE_GREATER' } ] ;
67
+ return [ { type : " NEXT_STATE_GREATER" } ] ;
61
68
}
62
69
}
63
70
64
71
const detectorAB = reduceDetectors ( detectorA , detectorB ) ;
65
72
66
- expect ( detectorAB ( - 10 , 50 ) ) . toEqual ( [ { type : ' NEXT_STATE_GREATER' } ] ) ;
67
- expect ( detectorAB ( 30 , 20 ) ) . toEqual ( [ { type : ' PREV_STATE_GREATER' } ] ) ;
73
+ expect ( detectorAB ( - 10 , 50 ) ) . toEqual ( [ { type : " NEXT_STATE_GREATER" } ] ) ;
74
+ expect ( detectorAB ( 30 , 20 ) ) . toEqual ( [ { type : " PREV_STATE_GREATER" } ] ) ;
68
75
} ) ;
69
76
70
- it ( ' should allow to reduce detectors with array and single result' , ( ) => {
77
+ it ( " should allow to compose detectors with array and single result" , ( ) => {
71
78
function detectorA ( ) {
72
- return [ { type : ' ARRAY_DETECTOR' } ] ;
79
+ return [ { type : " ARRAY_DETECTOR" } ] ;
73
80
}
74
81
75
82
function detectorB ( ) {
76
- return { type : ' SINGLE_DETECTOR' } ;
83
+ return { type : " SINGLE_DETECTOR" } ;
77
84
}
78
85
79
86
const detectorAB = reduceDetectors ( detectorA , detectorB ) ;
80
87
81
- expect ( detectorAB ( undefined , undefined ) ) . toEqual ( [ { type : 'ARRAY_DETECTOR' } , { type : 'SINGLE_DETECTOR' } ] ) ;
88
+ expect ( detectorAB ( undefined , undefined ) ) . toEqual ( [
89
+ { type : "ARRAY_DETECTOR" } ,
90
+ { type : "SINGLE_DETECTOR" }
91
+ ] ) ;
82
92
} ) ;
83
93
84
- it ( ' should return valid reduced detector for empty arguments' , ( ) => {
94
+ it ( " should return valid composed detector for empty arguments" , ( ) => {
85
95
const emptyDetector = reduceDetectors ( ) ;
86
96
87
97
expect ( emptyDetector ) . toBeInstanceOf ( Function ) ;
88
98
expect ( emptyDetector ( 10 , 20 ) ) . toEqual ( [ ] ) ;
89
99
} ) ;
90
100
91
- it ( 'should throw an exception for invalid arguments' , ( ) => {
92
- expect ( ( ) => { ( reduceDetectors as any ) ( { 'foo' : 'bar' } ) ; } ) . toThrow ( ) ;
93
- expect ( ( ) => { ( reduceDetectors as any ) ( ( ) => undefined , undefined ) ; } ) . toThrow ( ) ;
101
+ it ( "should throw an exception for invalid arguments" , ( ) => {
102
+ expect ( ( ) => {
103
+ ( reduceDetectors as any ) ( { foo : "bar" } ) ;
104
+ } ) . toThrow ( ) ;
105
+ expect ( ( ) => {
106
+ ( reduceDetectors as any ) ( ( ) => undefined , undefined ) ;
107
+ } ) . toThrow ( ) ;
94
108
} ) ;
95
109
} ) ;
0 commit comments