2021-02-21 17:17:28 +01:00
/* eslint-disable line-comment-position */
2020-11-05 14:01:23 +01:00
import { get } from 'svelte/store' ;
2020-01-16 01:47:43 +01:00
2020-12-03 19:08:34 +01:00
import type {
2020-11-23 14:50:58 +01:00
JSONGetter ,
MessageFormatter ,
TimeFormatter ,
DateFormatter ,
NumberFormatter ,
} from '../../../src/runtime/types/index' ;
2020-01-16 01:47:43 +01:00
import {
$format ,
$formatTime ,
$formatDate ,
$formatNumber ,
2020-11-23 15:00:59 +01:00
$getJSON ,
2020-11-05 14:01:23 +01:00
} from '../../../src/runtime/stores/formatters' ;
import { init } from '../../../src/runtime/configs' ;
import { addMessages } from '../../../src/runtime/stores/dictionary' ;
import { $locale } from '../../../src/runtime/stores/locale' ;
let formatMessage : MessageFormatter ;
let formatTime : TimeFormatter ;
let formatDate : DateFormatter ;
let formatNumber : NumberFormatter ;
2020-11-23 14:50:58 +01:00
let getJSON : JSONGetter ;
2020-01-23 14:11:04 +01:00
2020-01-16 01:47:43 +01:00
$locale . subscribe ( ( ) = > {
2020-11-05 14:01:23 +01:00
formatMessage = get ( $format ) ;
formatTime = get ( $formatTime ) ;
formatDate = get ( $formatDate ) ;
formatNumber = get ( $formatNumber ) ;
2021-10-11 13:57:46 +02:00
getJSON = get ( $getJSON ) as JSONGetter ;
2020-11-05 14:01:23 +01:00
} ) ;
addMessages ( 'en' , require ( '../../fixtures/en.json' ) ) ;
addMessages ( 'en-GB' , require ( '../../fixtures/en-GB.json' ) ) ;
addMessages ( 'pt' , require ( '../../fixtures/pt.json' ) ) ;
addMessages ( 'pt-BR' , require ( '../../fixtures/pt-BR.json' ) ) ;
addMessages ( 'pt-PT' , require ( '../../fixtures/pt-PT.json' ) ) ;
2020-01-16 01:47:43 +01:00
beforeEach ( ( ) = > {
2020-11-05 14:01:23 +01:00
init ( { fallbackLocale : 'en' } ) ;
} ) ;
2020-11-23 15:00:59 +01:00
2020-11-23 14:50:58 +01:00
describe ( 'format message' , ( ) = > {
it ( 'formats a message by its id and the current locale' , ( ) = > {
expect ( formatMessage ( { id : 'form.field_1_name' } ) ) . toBe ( 'Name' ) ;
} ) ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
it ( 'formats a message by its id and the a passed locale' , ( ) = > {
expect ( formatMessage ( { id : 'form.field_1_name' , locale : 'pt' } ) ) . toBe (
'Nome' ,
) ;
} ) ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
it ( 'formats a message with interpolated values' , ( ) = > {
expect ( formatMessage ( { id : 'photos' , values : { n : 0 } } ) ) . toBe (
'You have no photos.' ,
) ;
expect ( formatMessage ( { id : 'photos' , values : { n : 1 } } ) ) . toBe (
'You have one photo.' ,
) ;
expect ( formatMessage ( { id : 'photos' , values : { n : 21 } } ) ) . toBe (
'You have 21 photos.' ,
) ;
} ) ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
it ( 'formats the default value with interpolated values' , ( ) = > {
expect (
formatMessage ( {
id : 'non-existent' ,
default : '{food}' ,
values : { food : 'potato' } ,
} ) ,
) . toBe ( 'potato' ) ;
} ) ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
it ( 'formats the key with interpolated values' , ( ) = > {
expect (
formatMessage ( {
id : '{food}' ,
values : { food : 'potato' } ,
} ) ,
) . toBe ( 'potato' ) ;
} ) ;
2020-11-05 21:52:22 +01:00
2020-11-23 14:50:58 +01:00
it ( 'accepts a message id as first argument' , ( ) = > {
expect ( formatMessage ( 'form.field_1_name' ) ) . toBe ( 'Name' ) ;
} ) ;
2020-11-05 21:52:22 +01:00
2020-11-23 14:50:58 +01:00
it ( 'accepts a message id as first argument and formatting options as second' , ( ) = > {
expect ( formatMessage ( 'form.field_1_name' , { locale : 'pt' } ) ) . toBe ( 'Nome' ) ;
} ) ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
it ( 'throws if no locale is set' , ( ) = > {
$locale . set ( null ) ;
expect ( ( ) = > formatMessage ( 'form.field_1_name' ) ) . toThrow (
'[svelte-i18n] Cannot format a message without first setting the initial locale.' ,
) ;
} ) ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
it ( 'uses a missing message default value' , ( ) = > {
expect ( formatMessage ( 'missing' , { default : 'Missing Default' } ) ) . toBe (
'Missing Default' ,
) ;
} ) ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
it ( 'errors out when value found is not string' , ( ) = > {
2021-02-15 17:32:20 +01:00
const spy = jest . spyOn ( global . console , 'warn' ) . mockImplementation ( ) ;
2020-11-05 14:01:23 +01:00
2020-11-23 14:50:58 +01:00
expect ( typeof formatMessage ( 'form' ) ) . toBe ( 'object' ) ;
2021-02-15 17:32:20 +01:00
expect ( spy ) . toBeCalledWith (
2020-11-23 14:50:58 +01:00
` [svelte-i18n] Message with id "form" must be of type "string", found: "object". Gettin its value through the " $ format" method is deprecated; use the "json" method instead. ` ,
) ;
2020-01-16 01:47:43 +01:00
2021-02-15 17:32:20 +01:00
spy . mockRestore ( ) ;
2020-11-23 14:50:58 +01:00
} ) ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
it ( 'warn on missing messages' , ( ) = > {
2021-02-15 17:32:20 +01:00
const spy = jest . spyOn ( global . console , 'warn' ) . mockImplementation ( ) ;
2020-01-16 01:47:43 +01:00
2020-11-23 14:50:58 +01:00
formatMessage ( 'missing' ) ;
2021-02-15 17:32:20 +01:00
expect ( spy ) . toBeCalledWith (
2020-11-23 14:50:58 +01:00
` [svelte-i18n] The message "missing" was not found in "en". ` ,
) ;
2021-02-15 17:32:20 +01:00
spy . mockRestore ( ) ;
} ) ;
it ( 'does not throw with invalid syntax' , ( ) = > {
$locale . set ( 'en' ) ;
const spy = jest . spyOn ( global . console , 'warn' ) . mockImplementation ( ) ;
// eslint-disable-next-line line-comment-position
formatMessage ( 'with-syntax-error' , { values : { name : 'John' } } ) ;
expect ( spy ) . toHaveBeenCalledWith (
expect . stringContaining (
` [svelte-i18n] Message "with-syntax-error" has syntax error: ` ,
) ,
expect . anything ( ) ,
) ;
spy . mockRestore ( ) ;
2020-11-05 14:01:23 +01:00
} ) ;
2020-11-23 14:50:58 +01:00
} ) ;
test ( 'format time' , ( ) = > {
expect ( formatTime ( new Date ( 2019 , 0 , 1 , 20 , 37 ) ) ) . toBe ( '8:37 PM' ) ;
} ) ;
test ( 'format date' , ( ) = > {
expect ( formatDate ( new Date ( 2019 , 0 , 1 , 20 , 37 ) ) ) . toBe ( '1/1/19' ) ;
} ) ;
test ( 'format number' , ( ) = > {
expect ( formatNumber ( 123123123 ) ) . toBe ( '123,123,123' ) ;
} ) ;
test ( 'get raw JSON data from the current locale dictionary' , ( ) = > {
expect ( getJSON ( 'form' ) ) . toMatchObject ( {
field_1_name : 'Name' ,
field_2_name : 'Lastname' ,
2020-11-05 14:01:23 +01:00
} ) ;
2020-11-23 14:50:58 +01:00
expect ( getJSON ( 'non-existing' ) ) . toBeUndefined ( ) ;
2020-11-05 14:01:23 +01:00
} ) ;