@@ -16,7 +16,8 @@ import {
16
16
parseAsString ,
17
17
parseAsStringEnum ,
18
18
parseAsStringLiteral ,
19
- parseAsTimestamp
19
+ parseAsTimestamp ,
20
+ parseAsTuple
20
21
} from './parsers'
21
22
import {
22
23
isParserBijective ,
@@ -299,6 +300,25 @@ describe('parsers', () => {
299
300
isParserBijective ( parser , 'not-an-array' , [ 'a' , 'b' ] )
300
301
) . toThrow ( )
301
302
} )
303
+ it . only ( 'parseAsTuple' , ( ) => {
304
+ const parser = parseAsTuple ( [ parseAsInteger , parseAsString , parseAsBoolean ] )
305
+ expect ( parser . parse ( '1,a,false,will-ignore' ) ) . toStrictEqual ( [ 1 , 'a' , false ] )
306
+ expect ( parser . parse ( 'not-a-number,a,true' ) ) . toBeNull ( )
307
+ expect ( parser . parse ( '1,a' ) ) . toBeNull ( )
308
+ // @ts -expect-error - Tuple length is less than 2
309
+ expect ( ( ) => parseAsTuple ( [ parseAsInteger ] ) ) . toThrow ( )
310
+ expect ( parser . serialize ( [ 1 , 'a' , true ] ) ) . toBe ( '1,a,true' )
311
+ // @ts -expect-error - Tuple length mismatch
312
+ expect ( ( ) => parser . serialize ( [ 1 , 'a' ] ) ) . toThrow ( )
313
+ expect ( testParseThenSerialize ( parser , '1,a,true' ) ) . toBe ( true )
314
+ expect ( testSerializeThenParse ( parser , [ 1 , 'a' , true ] as const ) ) . toBe ( true )
315
+ expect ( isParserBijective ( parser , '1,a,true' , [ 1 , 'a' , true ] as const ) ) . toBe (
316
+ true
317
+ )
318
+ expect ( ( ) =>
319
+ isParserBijective ( parser , 'not-a-tuple' , [ 1 , 'a' , true ] as const )
320
+ ) . toThrow ( )
321
+ } )
302
322
303
323
it ( 'parseServerSide with default (#384)' , ( ) => {
304
324
const p = parseAsString . withDefault ( 'default' )
@@ -351,4 +371,14 @@ describe('parsers/equality', () => {
351
371
expect ( eq ( [ ] , [ 'foo' ] ) ) . toBe ( false )
352
372
expect ( eq ( [ 'foo' ] , [ 'bar' ] ) ) . toBe ( false )
353
373
} )
374
+ it . only ( 'parseAsTuple' , ( ) => {
375
+ const eq = parseAsTuple ( [ parseAsInteger , parseAsBoolean ] ) . eq !
376
+ expect ( eq ( [ 1 , true ] , [ 1 , true ] ) ) . toBe ( true )
377
+ expect ( eq ( [ 1 , true ] , [ 1 , false ] ) ) . toBe ( false )
378
+ expect ( eq ( [ 1 , true ] , [ 2 , true ] ) ) . toBe ( false )
379
+ // @ts -expect-error - Tuple length mismatch
380
+ expect ( eq ( [ 1 , true ] , [ 1 ] ) ) . toBe ( false )
381
+ // @ts -expect-error - Tuple length mismatch
382
+ expect ( eq ( [ 1 ] , [ 1 ] ) ) . toBe ( false )
383
+ } )
354
384
} )
0 commit comments