@@ -5,7 +5,7 @@ jest.mock('@intlify/shared', () => ({
5
5
...jest . requireActual < object > ( '@intlify/shared' ) ,
6
6
warn : jest . fn ( )
7
7
} ) )
8
- import { warn } from '@intlify/shared'
8
+ import { warn , isString , isNumber , isBoolean } from '@intlify/shared'
9
9
10
10
import { createCoreContext as context , NOT_REOSLVED } from '../src/context'
11
11
import { translate } from '../src/translate'
@@ -18,8 +18,12 @@ import {
18
18
import { compileToFunction } from '../src/compile'
19
19
import { fallbackWithLocaleChain } from '../src/fallbacker'
20
20
import { resolveValue } from '../src/resolver'
21
+ import { createTextNode } from './helper'
21
22
22
23
import type { MessageContext } from '../src/runtime'
24
+ import type { VNode } from './helper'
25
+ import type { MessageType , MessageProcessor } from '../src/runtime'
26
+ import type { PickupKeys } from '../src/types/utils'
23
27
24
28
beforeEach ( ( ) => {
25
29
registerMessageCompiler ( compileToFunction )
@@ -810,4 +814,152 @@ test('fallback context', () => {
810
814
811
815
expect ( translate ( ctx , 'hi' ) ) . toEqual ( 'hi! hello man!' )
812
816
} )
817
+
818
+ describe ( 'processor' , ( ) => {
819
+ // VNode processor
820
+ function normalize (
821
+ values : MessageType < string | VNode > [ ]
822
+ ) : MessageType < VNode > [ ] {
823
+ return values . map ( val =>
824
+ isString ( val ) || isNumber ( val ) || isBoolean ( val )
825
+ ? createTextNode ( String ( val ) )
826
+ : val
827
+ )
828
+ }
829
+ const interpolate = ( val : unknown ) : MessageType < VNode > => val as VNode
830
+ const processor = {
831
+ normalize,
832
+ interpolate,
833
+ type : 'vnode'
834
+ } as MessageProcessor < VNode >
835
+
836
+ test ( 'basic' , ( ) => {
837
+ const ctx = context < VNode > ( {
838
+ locale : 'en' ,
839
+ messages : {
840
+ en : { hi : 'hi kazupon !' }
841
+ }
842
+ } )
843
+ ctx . processor = processor
844
+ expect (
845
+ translate < typeof ctx , string , PickupKeys < typeof ctx . messages > , VNode > (
846
+ ctx ,
847
+ 'hi'
848
+ )
849
+ ) . toEqual ( [ { __v_isVNode : true , children : 'hi kazupon !' } ] )
850
+ } )
851
+
852
+ test ( 'list' , ( ) => {
853
+ const ctx = context < VNode > ( {
854
+ locale : 'en' ,
855
+ messages : {
856
+ en : { hi : 'hi {0} !' , nest : { foo : '' } }
857
+ }
858
+ } )
859
+ ctx . processor = processor
860
+ expect (
861
+ translate < typeof ctx , string , PickupKeys < typeof ctx . messages > , VNode > (
862
+ ctx ,
863
+ 'hi' ,
864
+ [ 'kazupon' ]
865
+ )
866
+ ) . toEqual ( [
867
+ { __v_isVNode : true , children : 'hi ' } ,
868
+ { __v_isVNode : true , children : 'kazupon' } ,
869
+ { __v_isVNode : true , children : ' !' }
870
+ ] )
871
+ } )
872
+
873
+ test ( 'named' , ( ) => {
874
+ const ctx = context < VNode > ( {
875
+ locale : 'en' ,
876
+ messages : {
877
+ en : { hi : 'hi {name} !' }
878
+ }
879
+ } )
880
+ ctx . processor = processor
881
+ expect (
882
+ translate < typeof ctx , string , PickupKeys < typeof ctx . messages > , VNode > (
883
+ ctx ,
884
+ 'hi' ,
885
+ { name : 'kazupon' }
886
+ )
887
+ ) . toEqual ( [
888
+ { __v_isVNode : true , children : 'hi ' } ,
889
+ { __v_isVNode : true , children : 'kazupon' } ,
890
+ { __v_isVNode : true , children : ' !' }
891
+ ] )
892
+ } )
893
+
894
+ test ( 'linked' , ( ) => {
895
+ const ctx = context < VNode > ( {
896
+ locale : 'en' ,
897
+ messages : {
898
+ en : {
899
+ name : 'kazupon' ,
900
+ hi : 'hi @.upper:name !'
901
+ }
902
+ }
903
+ } )
904
+ ctx . processor = processor
905
+ expect (
906
+ translate < typeof ctx , string , PickupKeys < typeof ctx . messages > , VNode > (
907
+ ctx ,
908
+ 'hi'
909
+ )
910
+ ) . toEqual ( [
911
+ { __v_isVNode : true , children : 'hi ' } ,
912
+ { __v_isVNode : true , children : 'KAZUPON' } ,
913
+ { __v_isVNode : true , children : ' !' }
914
+ ] )
915
+ } )
916
+
917
+ test ( 'plural' , ( ) => {
918
+ const ctx = context < VNode > ( {
919
+ locale : 'en' ,
920
+ messages : {
921
+ en : { apple : 'no apples | one apple | {count} apples from {name}' }
922
+ }
923
+ } )
924
+ ctx . processor = processor
925
+ expect (
926
+ translate < typeof ctx , string , PickupKeys < typeof ctx . messages > , VNode > (
927
+ ctx ,
928
+ 'apple' ,
929
+ 0
930
+ )
931
+ ) . toEqual ( [ { __v_isVNode : true , children : 'no apples' } ] )
932
+ expect (
933
+ translate < typeof ctx , string , PickupKeys < typeof ctx . messages > , VNode > (
934
+ ctx ,
935
+ 'apple' ,
936
+ 1
937
+ )
938
+ ) . toEqual ( [ { __v_isVNode : true , children : 'one apple' } ] )
939
+ expect (
940
+ translate < typeof ctx , string , PickupKeys < typeof ctx . messages > , VNode > (
941
+ ctx ,
942
+ 'apple' ,
943
+ 10
944
+ )
945
+ ) . toEqual ( [
946
+ { __v_isVNode : true , children : '10' } ,
947
+ { __v_isVNode : true , children : ' apples from ' } ,
948
+ undefined
949
+ ] )
950
+ expect (
951
+ translate < typeof ctx , string , PickupKeys < typeof ctx . messages > , VNode > (
952
+ ctx ,
953
+ 'apple' ,
954
+ { count : 20 , name : 'kazupon' } ,
955
+ 10
956
+ )
957
+ ) . toEqual ( [
958
+ { __v_isVNode : true , children : '20' } ,
959
+ { __v_isVNode : true , children : ' apples from ' } ,
960
+ { __v_isVNode : true , children : 'kazupon' }
961
+ ] )
962
+ } )
963
+ } )
964
+
813
965
/* eslint-enable @typescript-eslint/no-empty-function, @typescript-eslint/no-explicit-any */
0 commit comments