-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathintl.store.spec.js
171 lines (155 loc) · 4.9 KB
/
intl.store.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import proxyFn from "proxyquire";
const proxyquire = proxyFn.noPreserveCache().noCallThru();
import lux from "lux.js";
describe( "lux-i18n - intl.store", () => {
let store, numberStub, formatStub;
beforeEach( () => {
formatStub = { format: arg => arg.toString() };
numberStub = sinon.stub( window.Intl, "NumberFormat" ).returns( formatStub );
store = proxyquire( "./intl.store", {} );
} );
afterEach( () => {
store.dispose();
numberStub.restore();
} );
it( "should init with default state", () => {
store.getState().should.eql( {
defaultLocale: "en-US",
currentLocale: "en-US",
translations: {}
} );
} );
describe( "when handling changeLocale action", () => {
it( "should update the locale", () => {
store.getState().currentLocale.should.eql( "en-US" );
lux.dispatch( "changeLocale", "es-MX" );
store.getState().currentLocale.should.eql( "es-MX" );
} );
} );
describe( "when handling receiveTranslation action", () => {
it( "should update translations state", () => {
store.getState().translations.should.eql( {} );
lux.dispatch( "receiveTranslation", {
"es-MX": {
"hello.world": "hola mundo"
}
} );
store.getState().translations.should.eql( {
"es-MX": {
"hello.world": "hola mundo"
}
} );
} );
it( "should merge new and old translations, not overwrite them", () => {
store.getState().translations.should.eql( {} );
lux.dispatch( "receiveTranslation", {
"es-MX": {
"hello.world": "hola mundo"
},
"en-US": {
"hello.world": "HAY Y'ALL!"
}
} );
const { "es-MX": origES, "en-US": origEN } = store.getState().translations;
lux.dispatch( "receiveTranslation", {
"es-MX": {
"my.house": "mi casa"
},
"en-US": {
"my.house": "muh crib"
}
} );
store.getState().translations.should.eql( {
"es-MX": {
"hello.world": "hola mundo",
"my.house": "mi casa"
},
"en-US": {
"hello.world": "HAY Y'ALL!",
"my.house": "muh crib"
}
} );
const { "es-MX": newES, "en-US": newEN } = store.getState().translations;
newES.should.not.equal( origES );
newEN.should.not.equal( origEN );
} );
} );
describe( "read accessor methods", () => {
beforeEach( () => {
store.getState().translations = {
"en-US": {
"hello.world": "oh, hai world"
},
fr: {
"hello.world": "oh, salut tout le monde"
}
};
} );
describe( "when calling getCurrentTranslations", () => {
it( "should return expected state for an exact region match", () => {
store.getCurrentTranslations().should.eql( {
locale: "en-US",
messages: {
"hello.world": "oh, hai world"
}
} );
} );
it( "should fall back to a generic language for an unmatched region", () => {
store.getState().currentLocale = "fr-FR";
store.getCurrentTranslations().should.eql( {
locale: "fr-FR",
messages: {
"hello.world": "oh, salut tout le monde"
}
} );
} );
} );
describe( "when calling getFormattedMessage", () => {
it( "should return expected message", () => {
store.getFormattedMessage( "hello.world" ).should.equal( "oh, hai world" );
} );
it( "should return expected message that takes data into account", () => {
const msgArray = [
"You have said hello to {numWorlds, plural, ",
"=0 {no worlds.}",
"=1 {one world.}",
"other {# worlds.}}"
];
store.getState().translations = {
"en-US": {
"hello.world": msgArray.join( "" )
}
};
store.getFormattedMessage( "hello.world", { numWorlds: 42 } ).should.equal( "You have said hello to 42 worlds." );
} );
it( "should return the key if no message is found", () => {
store.getFormattedMessage( "nope.nope" ).should.equal( "nope.nope" );
} );
it( "should return the default value if no message is found & default is provided", () => {
store.getFormattedMessage( "nope.nope", {}, "absonopely" ).should.equal( "absonopely" );
} );
it( "should return the default value if no message is found & default is empty string", () => {
store.getFormattedMessage( "nope.nope", {}, "" ).should.equal( "" );
} );
it( "should return the default value taking data into account", () => {
const msgArray = [
"You have said hello to {numWorlds, plural, ",
"=0 {no worlds.}",
"=1 {one world.}",
"other {# worlds.}}"
];
store.getFormattedMessage( "hello.sample", { numWorlds: 42 }, msgArray.join( "" ) ).should.equal( "You have said hello to 42 worlds." );
} );
} );
describe( "when calling getFormattedNumber", () => {
it( "should return expected message", () => {
store.getFormattedNumber( 20 ).should.equal( "20" );
} );
} );
describe( "when calling getFormattedDate", () => {
it( "should return expected message", () => {
store.getFormattedDate( new Date( 2020, 3, 1 ), { year: "numeric", month: "short", day: "numeric" } ).should.equal( "Apr 1, 2020" );
} );
} );
} );
} );