1- import assert from "assert" ;
21import { FluentBundle , FluentResource } from "@fluent/bundle" ;
2+ import { afterEach , beforeAll , expect , vi } from "vitest" ;
33import { Localization } from "../src/localization.ts" ;
4- import { vi } from "vitest" ;
54
65async function * mockGenerateMessages ( ) {
76 const bundle = new FluentBundle ( [ "en-US" ] ) ;
@@ -10,21 +9,35 @@ async function* mockGenerateMessages() {
109 yield bundle ;
1110}
1211
13- suite ( "formatMessages" , function ( ) {
14- test ( "returns a translation" , async function ( ) {
15- const loc = new Localization ( [ "test.ftl" ] , mockGenerateMessages ) ;
16- const translations = await loc . formatMessages ( [ { id : "key1" } ] ) ;
12+ beforeAll ( ( ) => vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } ) ) ;
13+ afterEach ( vi . clearAllMocks ) ;
1714
18- assert . strictEqual ( translations [ 0 ] . value , "Key 1" ) ;
19- } ) ;
15+ for ( const [ name , keys ] of [
16+ [ "string keys" , [ "missing_key" , "key1" ] ] ,
17+ [ "object keys" , [ { id : "missing_key" } , { id : "key1" } ] ] ,
18+ ] ) {
19+ suite ( name , ( ) => {
20+ test ( "formatMessages" , async function ( ) {
21+ const loc = new Localization ( [ "test.ftl" ] , mockGenerateMessages ) ;
22+ const translations = await loc . formatMessages ( keys ) ;
23+
24+ expect ( translations ) . toEqual ( [
25+ undefined ,
26+ { value : "Key 1" , attributes : null } ,
27+ ] ) ;
28+ expect ( console . warn . mock . calls ) . toEqual ( [
29+ [ "[fluent] Missing translations in en-US: missing_key" ] ,
30+ ] ) ;
31+ } ) ;
2032
21- test ( "returns undefined for a missing translation" , async function ( ) {
22- vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } ) ;
23- const loc = new Localization ( [ "test.ftl" ] , mockGenerateMessages ) ;
24- const translations = await loc . formatMessages ( [ { id : "missing_key" } ] ) ;
33+ test ( "formatValues" , async function ( ) {
34+ const loc = new Localization ( [ "test.ftl" ] , mockGenerateMessages ) ;
35+ const translations = await loc . formatValues ( keys ) ;
2536
26- // Make sure that the returned value here is `undefined`.
27- // This allows bindings to handle missing translations.
28- assert . strictEqual ( translations [ 1 ] , undefined ) ;
37+ expect ( translations ) . toEqual ( [ undefined , "Key 1" ] ) ;
38+ expect ( console . warn . mock . calls ) . toEqual ( [
39+ [ "[fluent] Missing translations in en-US: missing_key" ] ,
40+ ] ) ;
41+ } ) ;
2942 } ) ;
30- } ) ;
43+ }
0 commit comments