11import { interpolate } from '@utils/interpolate' ;
22
33describe ( 'interpolate' , ( ) => {
4+ it ( 'returns empty string if input is empty' , ( ) => {
5+ expect ( interpolate ( '' , { name : 'Test' } ) ) . toBe ( '' ) ;
6+ } ) ;
7+
8+ it ( 'returns the same string if no placeholders are present' , ( ) => {
9+ expect ( interpolate ( 'No interpolation needed.' , { } ) ) . toBe (
10+ 'No interpolation needed.'
11+ ) ;
12+ } ) ;
13+
414 it ( 'replaces simple variables' , ( ) => {
515 expect ( interpolate ( 'Hello {{name}}!' , { name : 'Test' } ) ) . toBe (
616 'Hello Test!'
717 ) ;
818 } ) ;
919
10- it ( 'replaces plural based on value' , ( ) => {
20+ it ( 'removes variable if missing' , ( ) => {
21+ expect ( interpolate ( 'Hello {{name}}!' , { } ) ) . toBe ( 'Hello !' ) ;
22+ } ) ;
23+
24+ it ( 'replaces plural based on numeric value' , ( ) => {
1125 expect ( interpolate ( '{{count|item|items}}' , { count : 1 } ) ) . toBe ( 'item' ) ;
1226 expect ( interpolate ( '{{count|item|items}}' , { count : 3 } ) ) . toBe ( 'items' ) ;
1327 } ) ;
1428
15- it ( 'replaces variable and plural in same string' , ( ) => {
29+ it ( 'falls back to plural if variable is not a number' , ( ) => {
30+ expect ( interpolate ( '{{count|item|items}}' , { count : 'abc' } ) ) . toBe ( 'items' ) ;
31+ } ) ;
32+
33+ it ( 'replaces variable and plural in the same string' , ( ) => {
1634 expect ( interpolate ( '{{count}} {{count|item|items}}' , { count : 2 } ) ) . toBe (
1735 '2 items'
1836 ) ;
19- } ) ;
20-
21- it ( 'removes variable if missing' , ( ) => {
22- expect ( interpolate ( 'Hello {{name}}!' , { } ) ) . toBe ( 'Hello !' ) ;
23- } ) ;
24-
25- it ( 'falls back to plural if count is invalid' , ( ) => {
26- expect ( interpolate ( '{{count|item|items}}' , { count : 'not-a-number' } ) ) . toBe (
27- 'items'
37+ expect ( interpolate ( '{{count}} {{count|item|items}}' , { count : 1 } ) ) . toBe (
38+ '1 item'
2839 ) ;
2940 } ) ;
3041
@@ -36,4 +47,13 @@ describe('interpolate', () => {
3647 )
3748 ) . toBe ( '2 characters as 1 text message' ) ;
3849 } ) ;
50+
51+ it ( 'handles missing variables by not including them' , ( ) => {
52+ expect (
53+ interpolate (
54+ '{{characters}} {{characters|character|characters}} and {{missing}} {{missing|apple|apples}}' ,
55+ { characters : 1 }
56+ )
57+ ) . toBe ( '1 character and apples' ) ;
58+ } ) ;
3959} ) ;
0 commit comments