Skip to content

Commit 202898b

Browse files
tests: add parameterized unit tests
1 parent 4c75e83 commit 202898b

File tree

1 file changed

+151
-1
lines changed

1 file changed

+151
-1
lines changed

__tests__/strings.test.ts

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { lcFirst, parseValue, stringifyValue, toCamelCase, toKebabCase, ucFirst } from '@/strings'
1+
import { lcFirst, parameterized, parseValue, stringifyValue, toCamelCase, toKebabCase, ucFirst } from '@/strings'
22
import { addLeadingCharacter, addTrailingCharacter, removeLeadingCharacter, removeTrailingCharacter } from '@/strings'
33
import { recipientsToString, emailDataToString } from '@/strings'
44

@@ -607,4 +607,154 @@ describe( 'emailDataToString', () => {
607607
)
608608
} )
609609

610+
} )
611+
612+
613+
describe( 'parameterized', () => {
614+
615+
it( 'creates a parameterized string with a single value', () => {
616+
617+
const [ string, params ] = parameterized`SELECT * FROM users WHERE id = ${ 1 }`
618+
619+
expect( string ).toBe( 'SELECT * FROM users WHERE id = ?' )
620+
expect( params ).toEqual( [ 1 ] )
621+
622+
} )
623+
624+
625+
it( 'creates a parameterized string with multiple single values', () => {
626+
627+
const [ string, params ] = parameterized`SELECT * FROM users WHERE id = ${ 1 } AND name = ${ 'John' }`
628+
629+
expect( string ).toBe( 'SELECT * FROM users WHERE id = ? AND name = ?' )
630+
expect( params ).toEqual( [ 1, 'John' ] )
631+
632+
} )
633+
634+
635+
it( 'expands array values into multiple placeholders', () => {
636+
637+
const [ string, params ] = parameterized`SELECT * FROM users WHERE status IN ( ${ [ 'active', 'pending' ] } )`
638+
639+
expect( string ).toBe( 'SELECT * FROM users WHERE status IN ( ?, ? )' )
640+
expect( params ).toEqual( [ 'active', 'pending' ] )
641+
642+
} )
643+
644+
645+
it( 'handles mixed single and array values', () => {
646+
647+
const [ string, params ] = parameterized`SELECT * FROM users WHERE id = ${ 1 } AND status IN ( ${ [ 'active', 'pending' ] } )`
648+
649+
expect( string ).toBe( 'SELECT * FROM users WHERE id = ? AND status IN ( ?, ? )' )
650+
expect( params ).toEqual( [ 1, 'active', 'pending' ] )
651+
652+
} )
653+
654+
655+
it( 'normalizes whitespace in the string string', () => {
656+
657+
const [ string, params ] = parameterized`SELECT * FROM users WHERE id = ${ 1 }`
658+
659+
expect( string ).toBe( 'SELECT * FROM users WHERE id = ?' )
660+
expect( params ).toEqual( [ 1 ] )
661+
662+
} )
663+
664+
665+
it( 'trims leading and trailing whitespace', () => {
666+
667+
const [ string, params ] = parameterized` SELECT * FROM users WHERE id = ${ 1 } `
668+
669+
expect( string ).toBe( 'SELECT * FROM users WHERE id = ?' )
670+
expect( params ).toEqual( [ 1 ] )
671+
672+
} )
673+
674+
675+
it( 'skips undefined values', () => {
676+
677+
// @ts-expect-error negative testing
678+
const [ string, params ] = parameterized`SELECT * FROM users WHERE id = ${ undefined } AND name = ${ 'John' }`
679+
680+
expect( string ).toBe( 'SELECT * FROM users WHERE id = AND name = ?' )
681+
expect( params ).toEqual( [ 'John' ] )
682+
683+
} )
684+
685+
686+
it( 'handles boolean values', () => {
687+
688+
const [ string, params ] = parameterized`SELECT * FROM users WHERE active = ${ true }`
689+
690+
expect( string ).toBe( 'SELECT * FROM users WHERE active = ?' )
691+
expect( params ).toEqual( [ true ] )
692+
693+
} )
694+
695+
696+
it( 'handles numeric values including bigint', () => {
697+
698+
const [ string, params ] = parameterized`SELECT * FROM users WHERE id = ${ 42 } OR count = ${ BigInt( 999999999999 ) }`
699+
700+
expect( string ).toBe( 'SELECT * FROM users WHERE id = ? OR count = ?' )
701+
expect( params ).toEqual( [ 42, BigInt( 999999999999 ) ] )
702+
703+
} )
704+
705+
706+
it( 'handles empty array values', () => {
707+
708+
const [ string, params ] = parameterized`SELECT * FROM users WHERE id IN (${ [] })`
709+
710+
expect( string ).toBe( 'SELECT * FROM users WHERE id IN ()' )
711+
expect( params ).toEqual( [] )
712+
713+
} )
714+
715+
716+
it( 'handles array with single value', () => {
717+
718+
const [ string, params ] = parameterized`SELECT * FROM users WHERE status IN (${ [ 'active' ] })`
719+
720+
expect( string ).toBe( 'SELECT * FROM users WHERE status IN (?)' )
721+
expect( params ).toEqual( [ 'active' ] )
722+
723+
} )
724+
725+
726+
it( 'handles multiple arrays in a string', () => {
727+
728+
const [ string, params ] = parameterized`SELECT * FROM users WHERE status IN (${ [ 'active', 'pending' ] }) AND id IN (${ [ 1, 2, 3 ] })`
729+
730+
expect( string ).toBe( 'SELECT * FROM users WHERE status IN (?, ?) AND id IN (?, ?, ?)' )
731+
expect( params ).toEqual( [ 'active', 'pending', 1, 2, 3 ] )
732+
733+
} )
734+
735+
736+
it( 'returns correct tuple structure', () => {
737+
738+
const result = parameterized`SELECT * FROM users WHERE id = ${ 1 }`
739+
740+
expect( Array.isArray( result ) ).toBe( true )
741+
expect( result.length ).toBe( 2 )
742+
expect( typeof result[ 0 ] ).toBe( 'string' )
743+
expect( Array.isArray( result[ 1 ] ) ).toBe( true )
744+
745+
} )
746+
747+
748+
it( 'handles newlines and tabs in template', () => {
749+
750+
const [ string, params ] = parameterized`
751+
SELECT * FROM users
752+
WHERE id = ${ 1 }
753+
`
754+
755+
expect( string ).toBe( 'SELECT * FROM users WHERE id = ?' )
756+
expect( params ).toEqual( [ 1 ] )
757+
758+
} )
759+
610760
} )

0 commit comments

Comments
 (0)