UNPKG

1.95 kBJavaScriptView Raw
1const {expect} = require('chai');
2
3import freeVariablesInMessage from '../src/free-variables';
4import parsing from '../src/parsing';
5
6
7describe('freeVariables', function() {
8 const examples = {
9 '<I18N>{foo}</I18N>': ['foo'],
10 '<I18N>{this.foo}</I18N>': [],
11 '<I18N>{this.foo()}</I18N>': [],
12 '<I18N><span foo>{foo}</span></I18N>': ['foo'],
13 '<I18N><span foo={bar}>{foo}</span></I18N>': ['foo', 'bar'],
14 '<I18N><span foo={bar} {...baz}>{foo}</span></I18N>': ['foo', 'bar', 'baz'],
15 '<I18N><span foo={{one: 1, [two]: three, ...four}}>{foo}</span></I18N>': ['foo', 'two', 'three', 'four'],
16 '<I18N><span i18n-id="stat" className="stat"><ReactIntl.FormattedNumber value={dailyVisitors}/></span>daily visitors</I18N>':
17 ['ReactIntl', 'dailyVisitors'],
18 '<I18N>Hello, world. {/* no variables here bruh */}<Component />{foo}<p>{bar.baz}</p></I18N>':
19 ['Component', 'foo', 'bar'],
20 '<I18N>Hello, world. <Component.SubComponent snoochie={boochies} />{this.bar.baz}</I18N>':
21 ['Component', 'boochies'],
22 '<I18N>Hello, world. <Component.SubComponent snoochie={{boochie: poochies, [foo]: bar}} />{this.bar.baz}</I18N>':
23 ['Component', 'poochies', 'foo', 'bar'],
24 '<I18N><Pluralize:items on={this.state.count}><Match when="=0">You have no items in your cart</Match><Match when="one">You have one item in your cart</Match><Match when="other">You have {this.state.count} items in your cart</Match></Pluralize:items></I18N>':
25 ['Pluralize', 'Match'],
26 };
27
28 Object.keys(examples).forEach(src => {
29 const expectedVariables = examples[src];
30 it(`in ${src}`, function() {
31 const expression = parsing.parse(src).program.body[0].expression;
32 const variables = freeVariablesInMessage(expression);
33 expect(variables).to.have.members(expectedVariables);
34 });
35 });
36});