forked from facebook/docusaurus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translate.test.tsx
80 lines (70 loc) · 2.5 KB
/
Translate.test.tsx
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import renderer from 'react-test-renderer';
import Translate, {translate} from '../Translate';
describe('translate', () => {
it('accepts id and uses it as fallback', () => {
expect(translate({id: 'some-id'})).toBe('some-id');
});
it('accepts message and uses it as fallback', () => {
expect(translate({message: 'some-message'})).toBe('some-message');
});
it('accepts id+message and uses message as fallback', () => {
expect(translate({id: 'some-id', message: 'some-message'})).toBe(
'some-message',
);
});
it('rejects when no id or message', () => {
// TODO tests are not resolving type defs correctly. We need to include test
// files in a tsconfig file
// @ts-expect-error: TS should protect when both id/message are missing
expect(() => translate({})).toThrowErrorMatchingInlineSnapshot(
`"Docusaurus translation declarations must have at least a translation id or a default translation message"`,
);
});
});
describe('<Translate>', () => {
it('accepts id and uses it as fallback', () => {
expect(renderer.create(<Translate id="some-id" />).toJSON()).toBe(
'some-id',
);
});
it('accepts message and uses it as fallback', () => {
expect(renderer.create(<Translate>some-message</Translate>).toJSON()).toBe(
'some-message',
);
});
it('accepts id+message and uses message as fallback', () => {
expect(
renderer
.create(<Translate id="some-id">some-message</Translate>)
.toJSON(),
).toBe('some-message');
});
it('rejects when no id or message', () => {
expect(() =>
// @ts-expect-error: TS should protect when both id/message are missing
renderer.create(<Translate />),
).toThrowErrorMatchingInlineSnapshot(
`"Docusaurus translation declarations must have at least a translation id or a default translation message"`,
);
});
it('rejects when children is not a string', () => {
expect(() =>
renderer.create(
// eslint-disable-next-line @docusaurus/string-literal-i18n-messages
<Translate id="foo">
{/* @ts-expect-error: for test */}
<span>aaa</span>
</Translate>,
),
).toThrowErrorMatchingInlineSnapshot(
`"The Docusaurus <Translate> component only accept simple string values"`,
);
});
});