-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathwith_localization_test.js
138 lines (111 loc) · 4.24 KB
/
with_localization_test.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
import React from 'react';
import assert from 'assert';
import { mount, shallow } from 'enzyme';
import { FluentBundle, FluentResource } from '../../fluent-bundle/src';
import ReactLocalization from '../src/localization';
import { withLocalization, LocalizationProvider } from '../src';
class DummyComponent extends React.Component {
static doStaticThing() {
return "done";
}
render() {
return <div />;
}
}
suite('withLocalization', function() {
test('render inside of a LocalizationProvider', function() {
const EnhancedComponent = withLocalization(DummyComponent);
const wrapper = shallow(
<LocalizationProvider bundles={[]}>
<EnhancedComponent />
</LocalizationProvider>
);
assert.strictEqual(wrapper.length, 1);
});
test('render outside of a LocalizationProvider', function() {
const EnhancedComponent = withLocalization(DummyComponent);
const wrapper = shallow(
<EnhancedComponent />,
);
assert.strictEqual(wrapper.length, 1);
});
test('getString with access to the l10n context', function() {
const bundle = new FluentBundle("en", {useIsolating: false});
const l10n = new ReactLocalization([bundle]);
const EnhancedComponent = withLocalization(DummyComponent);
bundle.addResource(new FluentResource(`
foo = FOO
bar = BAR {$arg}
`));
const wrapper = shallow(
<EnhancedComponent />,
{ context: { l10n } }
);
const getString = wrapper.prop('getString');
// Returns the translation.
assert.strictEqual(getString('foo', {}), 'FOO');
assert.strictEqual(getString('bar', {arg: 'ARG'}), 'BAR ARG');
// Doesn't throw on formatting errors.
assert.strictEqual(getString('bar', {}), 'BAR {$arg}');
});
test('getString with access to the l10n context, with fallback value', function() {
const bundle = new FluentBundle("en", {useIsolating: false});
const l10n = new ReactLocalization([bundle]);
const EnhancedComponent = withLocalization(DummyComponent);
bundle.addResource(new FluentResource(`
foo = FOO
bar = BAR {$arg}
`));
const wrapper = shallow(
<EnhancedComponent />,
{ context: { l10n } }
);
const getString = wrapper.prop('getString');
// Returns the translation, even if fallback value provided.
assert.strictEqual(getString('foo', {}, 'fallback'), 'FOO');
// Returns the fallback.
assert.strictEqual(getString('missing', {}, 'fallback'), 'fallback');
assert.strictEqual(getString('bar', {arg: 'ARG'}), 'BAR ARG');
// Doesn't throw on formatting errors.
assert.strictEqual(getString('bar', {}), 'BAR {$arg}');
});
test('getString without access to the l10n context', function() {
const EnhancedComponent = withLocalization(DummyComponent);
const wrapper = shallow(
<EnhancedComponent />
);
const getString = wrapper.prop('getString');
// Returns the id if no fallback.
assert.strictEqual(getString('foo', {arg: 1}), 'foo');
});
test('getString without access to the l10n context, with fallback value', function() {
const EnhancedComponent = withLocalization(DummyComponent);
const wrapper = shallow(
<EnhancedComponent />
);
const getString = wrapper.prop('getString');
// Returns the fallback if provided.
assert.strictEqual(getString('foo', {arg: 1}, 'fallback message'), 'fallback message');
});
test('getString with access to the l10n context, with message changes', function() {
const initialBundle = new FluentBundle();
const l10n = new ReactLocalization([initialBundle]);
const EnhancedComponent = withLocalization(({ getString }) => getString('foo'));
initialBundle.addResource(new FluentResource('foo = FOO'));
const wrapper = mount(
<EnhancedComponent />,
{ context: { l10n } }
);
assert.strictEqual(wrapper.text(), 'FOO');
const newBundle = new FluentBundle();
newBundle.addResource(new FluentResource('foo = BAR'));
l10n.setBundles([newBundle]);
wrapper.update();
assert.strictEqual(wrapper.text(), 'BAR');
})
test('static function is hoisted onto the wrapped component', function() {
const EnhancedComponent = withLocalization(DummyComponent);
const result = EnhancedComponent.doStaticThing();
assert.strictEqual(result, "done");
})
});