1
1
import { isNumber , isString } from "lodash" ;
2
2
3
- import { be , beTrue , decode } from "./index" ;
3
+ import { be , beArray , beTrue , decode } from "./index" ;
4
4
5
5
test ( "be" , ( ) => {
6
6
const numError = "Not a number!" ;
@@ -17,7 +17,7 @@ test("beTrue", () => {
17
17
expect ( ( ) => beTrue ( 5.7 , eq ( 5.5 ) , fiverError ) ) . toThrow ( fiverError ) ;
18
18
} ) ;
19
19
20
- test ( "orBe : validation ok" , ( ) => {
20
+ test ( "decode : validation ok" , ( ) => {
21
21
expect (
22
22
decode (
23
23
[ "foo" , "bar" ] ,
@@ -33,7 +33,7 @@ test("orBe: validation ok", () => {
33
33
} ) ;
34
34
} ) ;
35
35
36
- test ( "orBe : falling back" , ( ) => {
36
+ test ( "decode : falling back" , ( ) => {
37
37
const logger = jest . fn ( ) ;
38
38
39
39
expect (
@@ -50,3 +50,69 @@ test("orBe: falling back", () => {
50
50
51
51
expect ( logger ) . toBeCalledWith ( expect . objectContaining ( { message : "Failed" } ) ) ;
52
52
} ) ;
53
+
54
+ test ( "beArray: happy path" , ( ) => {
55
+ expect ( beArray ( [ 1 , false , "Bob" ] , el => el ) ) . toStrictEqual ( [ 1 , false , "Bob" ] ) ;
56
+ } ) ;
57
+
58
+ test ( "beArray: filtering" , ( ) => {
59
+ expect (
60
+ beArray ( [ "a" , 0 , "b" , 1 , "c" ] , s => {
61
+ if ( typeof s !== "string" ) throw Error ( "!" ) ;
62
+ return { s } ;
63
+ } )
64
+ ) . toStrictEqual ( [ { s : "a" } , { s : "b" } , { s : "c" } ] ) ;
65
+ } ) ;
66
+
67
+ test ( "beArray: invalidate all" , ( ) => {
68
+ expect ( ( ) =>
69
+ beArray (
70
+ [ "a" , 0 , "b" , 1 , "c" ] ,
71
+ x => {
72
+ if ( typeof x !== "string" ) throw Error ( "!!!" ) ;
73
+ return x ;
74
+ } ,
75
+ { invalidateAll : true }
76
+ )
77
+ ) . toThrow ( "!!!" ) ;
78
+ } ) ;
79
+
80
+ test ( "beArray: not an array" , ( ) => {
81
+ expect ( ( ) => {
82
+ beArray ( "[]" , x => x ) ;
83
+ } ) . toThrow ( "“[]” (string) is not an array" ) ;
84
+
85
+ expect ( ( ) => {
86
+ beArray ( 3.14 , x => x ) ;
87
+ } ) . toThrow ( "3.14 (number) is not an array" ) ;
88
+
89
+ expect ( ( ) => {
90
+ beArray ( "[]" , x => x , {
91
+ notAnArrayError : "Not quite an array one would expect"
92
+ } ) ;
93
+ } ) . toThrow ( "Not quite an array one would expect" ) ;
94
+ } ) ;
95
+
96
+ test ( "beArray: checkLength" , ( ) => {
97
+ expect ( ( ) =>
98
+ beArray (
99
+ [ false , false , false , false , true ] ,
100
+ x => {
101
+ if ( ! x ) throw Error ( "!!!" ) ;
102
+ return x ;
103
+ } ,
104
+ { minLength : 2 , minLengthError : "Way too short!" }
105
+ )
106
+ ) . toThrow ( "Way too short!" ) ;
107
+
108
+ expect ( ( ) =>
109
+ beArray (
110
+ [ false , false , false , false , true ] ,
111
+ x => {
112
+ if ( ! x ) throw Error ( "!!!" ) ;
113
+ return x ;
114
+ } ,
115
+ { minLength : 2 }
116
+ )
117
+ ) . toThrow ( "Array length (1) less than specified (2)" ) ;
118
+ } ) ;
0 commit comments