-
Notifications
You must be signed in to change notification settings - Fork 27
/
async-locale-data.spec.js
160 lines (143 loc) 路 4.58 KB
/
async-locale-data.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
import React from 'react';
import { shallow } from 'enzyme';
import { reportErrorToSentry } from '@commercetools-frontend/sentry';
import loadI18n from '../load-i18n';
import AsyncLocaleData from './async-locale-data';
jest.mock('@commercetools-frontend/sentry');
jest.mock('../load-i18n', () => jest.fn());
const ChildComponent = () => <div>Child</div>;
const createTestProps = props => ({
children: jest.fn(() => <ChildComponent />),
locale: 'en-US',
applicationMessages: {
en: { 'CustomApp.title': 'Title en' },
},
...props,
});
describe('rendering', () => {
let wrapper;
let props;
describe('if there is an error', () => {
let error;
beforeEach(() => {
error = new Error('oh no!');
loadI18n.mockClear();
loadI18n.mockImplementation(jest.fn(() => Promise.reject(error)));
props = createTestProps();
wrapper = shallow(<AsyncLocaleData {...props} />);
});
describe('when component is mounted', () => {
beforeEach(() => {
reportErrorToSentry.mockClear();
wrapper.instance().componentDidMount();
});
it('should report the error to sentry', () => {
expect(reportErrorToSentry).toHaveBeenCalledWith(error, {});
});
});
});
describe('if there is no error', () => {
beforeEach(() => {
loadI18n.mockClear();
loadI18n.mockImplementation(
jest.fn(() => Promise.resolve({ title: 'Title en' }))
);
props = createTestProps();
wrapper = shallow(<AsyncLocaleData {...props} />);
});
describe('when component is mounted', () => {
beforeEach(() => {
wrapper.instance().componentDidMount();
});
it('should call `loadIntl`', () => {
expect(loadI18n).toHaveBeenCalled();
});
it('should call `children` with state', () => {
expect(props.children).toHaveBeenCalledWith({
isLoading: false,
locale: 'en-US',
messages: { title: 'Title en', ...props.applicationMessages.en },
});
});
});
describe('when component is updated but with same locale', () => {
beforeEach(() => {
wrapper.instance().componentDidUpdate({
locale: 'en-in',
});
});
it('should call `loadIntl`', () => {
expect(loadI18n).toHaveBeenCalledTimes(1);
});
});
describe('when component is updated with new locale', () => {
beforeEach(() => {
loadI18n.mockClear();
props = createTestProps({
locale: 'de-AT',
});
wrapper = shallow(<AsyncLocaleData {...props} />);
wrapper.instance().componentDidUpdate({
locale: 'en-CA',
});
});
it('should call `loadIntl`', () => {
expect(loadI18n).toHaveBeenCalled();
});
it('should call `loadIntl` with `de`', () => {
expect(loadI18n).toHaveBeenCalledWith('de-AT');
});
});
});
describe('when applicationMessages is a function', () => {
beforeEach(() => {
loadI18n.mockClear();
loadI18n.mockClear();
loadI18n.mockImplementation(
jest.fn(() => Promise.resolve({ title: 'Title en' }))
);
props = createTestProps({
locale: 'en-CA',
applicationMessages: jest
.fn(() => Promise.resolve({ 'CustomApp.title': 'New title en' }))
.mockName('applicationMessages'),
});
wrapper = shallow(<AsyncLocaleData {...props} />);
});
describe('when component is mounted', () => {
beforeEach(() => {
wrapper.instance().componentDidMount();
});
it('should call `applicationMessagePromise`', () => {
expect(props.applicationMessages).toHaveBeenCalled();
});
it('should call `applicationMessagePromise` with `en-CA`', () => {
expect(props.applicationMessages).toHaveBeenCalledWith('en-CA');
});
it('should call `children` with state', () => {
expect(props.children).toHaveBeenCalledWith({
isLoading: false,
locale: 'en-CA',
messages: { title: 'Title en', 'CustomApp.title': 'New title en' },
});
});
});
});
describe('when locale is not defined', () => {
beforeEach(() => {
loadI18n.mockClear();
props = createTestProps({ locale: null });
wrapper = shallow(<AsyncLocaleData {...props} />);
});
it('should not call `loadIntl`', () => {
expect(loadI18n).not.toHaveBeenCalled();
});
it('should call `children` with state', () => {
expect(props.children).toHaveBeenCalledWith({
isLoading: true,
locale: null,
messages: null,
});
});
});
});