@@ -27,6 +27,7 @@ describe('isMatching', () => {
27
27
) ;
28
28
}
29
29
} ) ;
30
+
30
31
it ( 'should act as a type guard function if given a two arguments' , ( ) => {
31
32
const something : unknown = {
32
33
title : 'Hello' ,
@@ -56,6 +57,47 @@ describe('isMatching', () => {
56
57
}
57
58
} ) ;
58
59
60
+ it ( 'should work with object patterns' , ( ) => {
61
+ const value : unknown = { foo : true } ;
62
+ expect ( isMatching ( { foo : true } , value ) ) . toEqual ( true ) ;
63
+ expect ( isMatching ( { foo : 'true' } , value ) ) . toEqual ( false ) ;
64
+ } ) ;
65
+
66
+ it ( 'should work with array patterns' , ( ) => {
67
+ const value : unknown = [ 1 , 2 , 3 ] ;
68
+ expect ( isMatching ( P . array ( P . number ) , value ) ) . toEqual ( true ) ;
69
+ expect ( isMatching ( P . array ( P . string ) , value ) ) . toEqual ( false ) ;
70
+ } ) ;
71
+
72
+ it ( 'should work with variadic patterns' , ( ) => {
73
+ const value : unknown = [ 1 , 2 , 3 ] ;
74
+ expect ( isMatching ( [ 1 , ...P . array ( P . number ) ] , value ) ) . toEqual ( true ) ;
75
+ expect ( isMatching ( [ 2 , ...P . array ( P . number ) ] , value ) ) . toEqual ( false ) ;
76
+ } ) ;
77
+
78
+ it ( 'should work with primitive patterns' , ( ) => {
79
+ const value : unknown = 1 ;
80
+ expect ( isMatching ( P . number , value ) ) . toEqual ( true ) ;
81
+ expect ( isMatching ( P . boolean , value ) ) . toEqual ( false ) ;
82
+ } ) ;
83
+
84
+ it ( 'should work with literal patterns' , ( ) => {
85
+ const value : unknown = 1 ;
86
+ expect ( isMatching ( 1 , value ) ) . toEqual ( true ) ;
87
+ expect ( isMatching ( 'oops' , value ) ) . toEqual ( false ) ;
88
+ } ) ;
89
+
90
+ it ( 'should work with union and intersection patterns' , ( ) => {
91
+ const value : unknown = { foo : true } ;
92
+ expect ( isMatching ( P . union ( { foo : true } , { bar : false } ) , value ) ) . toEqual (
93
+ true
94
+ ) ;
95
+
96
+ expect ( isMatching ( P . union ( { foo : false } , { bar : false } ) , value ) ) . toEqual (
97
+ false
98
+ ) ;
99
+ } ) ;
100
+
59
101
type Pizza = { type : 'pizza' ; topping : string } ;
60
102
type Sandwich = { type : 'sandwich' ; condiments : string [ ] } ;
61
103
type Food = Pizza | Sandwich ;
@@ -83,7 +125,9 @@ describe('isMatching', () => {
83
125
84
126
isMatching (
85
127
// @ts -expect-error
86
- { type : 'oops' } ,
128
+ {
129
+ type : 'oops' ,
130
+ } ,
87
131
food
88
132
) ;
89
133
} ) ;
0 commit comments