@@ -16,7 +16,8 @@ import {
1616 parseAsString ,
1717 parseAsStringEnum ,
1818 parseAsStringLiteral ,
19- parseAsTimestamp
19+ parseAsTimestamp ,
20+ parseAsTuple
2021} from './parsers'
2122import {
2223 isParserBijective ,
@@ -299,6 +300,25 @@ describe('parsers', () => {
299300 isParserBijective ( parser , 'not-an-array' , [ 'a' , 'b' ] )
300301 ) . toThrow ( )
301302 } )
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+ } )
302322
303323 it ( 'parseServerSide with default (#384)' , ( ) => {
304324 const p = parseAsString . withDefault ( 'default' )
@@ -351,4 +371,14 @@ describe('parsers/equality', () => {
351371 expect ( eq ( [ ] , [ 'foo' ] ) ) . toBe ( false )
352372 expect ( eq ( [ 'foo' ] , [ 'bar' ] ) ) . toBe ( false )
353373 } )
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+ } )
354384} )
0 commit comments