UNPKG

4.57 kBJavaScriptView Raw
1const {expect} = require('chai');
2const I = require('immutable');
3import generate from 'babel-generator';
4
5import parsing from '../src/parsing';
6import transformation from '../src/transformation';
7
8
9describe('Message node transformation', function() {
10
11 it('properly handles whitespace and quotes', function() {
12 var messagesToBeTransformed = I.List([
13 [
14 '<I18N>Hello, world. <Component />{foo}<p>{bar.baz}</p></I18N>',
15 '<I18N message={"Hello, world. <Component />{foo}<p>{bar.baz}</p>"} context={this} args={[Component, foo, bar]}' +
16 ' fallback={function () {\n return <span>Hello, world. <Component />{foo}<p>{bar.baz}</p></span>;\n}} />;'
17 ],
18
19 // ensure namespaced tags are de-namespaced
20 [
21 '<I18N>Hello, world. <Component />{foo}<p:beluga data-caviar>{bar.baz}</p:beluga></I18N>',
22 '<I18N message={"Hello, world. <Component />{foo}<p:beluga>{bar.baz}</p:beluga>"} context={this} args={[Component, foo, bar]}' +
23 ' fallback={function () {\n return <span>Hello, world. <Component />{foo}<p data-caviar>{bar.baz}</p></span>;\n}} />;'
24 ],
25
26 // this test ensures whitespace is handled properly
27 [
28 `<I18N>
29 <div>Hello, world. <Component.SubComponent i18n-id="comp.sub" snoochie={boochies} />{this.bar.baz}</div>
30</I18N>`,
31 '<I18N message={"<div>Hello, world. <Component.SubComponent:comp.sub />{this.bar.baz}</div>"} context={this} args={[Component, boochies]}' +
32 ' fallback={function () {\n return <span>\n <div>Hello, world. <Component.SubComponent snoochie={boochies} />{this.bar.baz}</div>\n </span>;\n}} />;'
33 ],
34
35 // this test ensures whitespace is handled properly in multi-line extractions
36 [
37 `<I18N>
38 <div>
39 Hello, world.
40 <Component.SubComponent i18n-id="comp.sub" snoochie={boochies} />
41 {this.bar.baz}
42 </div>
43</I18N>`,
44 '<I18N message={"<div>\\n Hello, world.\\n <Component.SubComponent:comp.sub />\\n {this.bar.baz}\\n </div>"} context={this} args={[Component, boochies]}' +
45 ' fallback={function () {\n return <span>\n <div>\n Hello, world.\n <Component.SubComponent snoochie={boochies} />\n {this.bar.baz}\n </div>\n </span>;\n}} />;'
46 ],
47
48 // Namespaced Component with sanitized attributes:
49 [
50 '<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>',
51 '<I18N message={"<Pluralize:items><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>"}' +
52 ' context={this} args={[Pluralize, Match]} fallback={function () {\n return <span><Pluralize 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></span>;\n}} />;'
53 ],
54
55 // whitespace + escaping quotes
56 [
57 '<I18N>Hello, \n"world".</I18N>',
58 '<I18N message={"Hello, \\n\\\"world\\\"."} context={this} args={[]}' +
59 ' fallback={function () {\n return <span>Hello, \n"world".</span>;\n}} />;'
60 ],
61
62 [
63 "i18n('Well golly gee')",
64 "i18n('Well golly gee')"
65 ],
66
67 [
68 "i18n('Well \"golly\" gee')",
69 "i18n('Well \"golly\" gee')"
70 ],
71
72 [
73 "i18n('Well \\'golly\\' gee')",
74 "i18n('Well \\'golly\\' gee')"
75 ]
76 ]);
77
78 messagesToBeTransformed.forEach(([message, transformedMessage]) => {
79 try {
80 const ast = parsing.parseExpression(message);
81 const transformedMarker = transformation.transformMarker(ast);
82 expect(generate(transformedMarker).code).to.equal(transformedMessage);
83 } catch(e) {
84 console.warn("Encountered error testing", message);
85 throw e;
86 }
87 });
88 });
89});