Skip to content

Commit 964159b

Browse files
committed
fix: rm componentWillReceiveProps from relative
1 parent ec8ff4b commit 964159b

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

src/components/relative.tsx

+13-9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export interface Props extends FormatRelativeTimeOptions {
6868
}
6969

7070
interface State {
71+
prevUnit?: Unit;
72+
prevValue?: number;
7173
currentValueInSeconds: number;
7274
}
7375

@@ -91,6 +93,8 @@ class FormattedRelativeTime extends React.PureComponent<Props, State> {
9193
unit: 'second',
9294
};
9395
state: State = {
96+
prevUnit: this.props.unit,
97+
prevValue: this.props.value,
9498
currentValueInSeconds: canIncrement(this.props.unit)
9599
? valueToSeconds(this.props.value, this.props.unit)
96100
: 0,
@@ -152,17 +156,17 @@ class FormattedRelativeTime extends React.PureComponent<Props, State> {
152156
this._updateTimer = null;
153157
}
154158

155-
componentWillReceiveProps(nextProps: Props) {
156-
if (
157-
this.props.value !== nextProps.value ||
158-
this.props.unit !== nextProps.unit
159-
) {
160-
this.setState({
161-
currentValueInSeconds: canIncrement(nextProps.unit)
162-
? valueToSeconds(nextProps.value, nextProps.unit)
159+
static getDerivedStateFromProps(props: Props, state: State) {
160+
if (props.unit !== state.prevUnit || props.value !== state.prevValue) {
161+
return {
162+
prevValue: props.value,
163+
prevUnit: props.unit,
164+
currentValueInSeconds: canIncrement(props.unit)
165+
? valueToSeconds(props.value, props.unit)
163166
: 0,
164-
});
167+
};
165168
}
169+
return null;
166170
}
167171

168172
render() {

test/unit/components/html-message.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('<FormattedHTMLMessage>', () => {
103103
const spy = jest.fn().mockImplementation(() => <p>Jest</p>);
104104
const rendered = mountWithProvider({...descriptor, children: spy}, intl);
105105

106-
expect(spy).toHaveBeenCalledTimes(1);
106+
expect(spy).toHaveBeenCalledTimes(2);
107107
expect(spy.mock.calls[0]).toEqual([intl.formatHTMLMessage(descriptor)]);
108108

109109
expect(rendered.text()).toBe('Jest');

test/unit/components/message.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe('<FormattedMessage>', () => {
105105

106106
const rendered = mountWithProvider({...descriptor, children: spy}, intl);
107107

108-
expect(spy).toHaveBeenCalledTimes(1);
108+
expect(spy).toHaveBeenCalledTimes(2);
109109
expect(spy.mock.calls[0]).toEqual([intl.formatMessage(descriptor)]);
110110

111111
expect(rendered.text()).toBe('Jest');
@@ -207,14 +207,14 @@ describe('<FormattedMessage>', () => {
207207
...values, // create new object instance with same values to test shallow equality check
208208
},
209209
});
210-
expect(spy).toHaveBeenCalledTimes(1); // expect only 1 render as the value object instance changed but not its values
210+
expect(spy).toHaveBeenCalledTimes(2); // expect only 1 render as the value object instance changed but not its values
211211

212212
injectIntlContext.setProps({
213213
...descriptor,
214214
values: {
215215
name: 'Enzyme',
216216
},
217217
});
218-
expect(spy).toHaveBeenCalledTimes(2); // expect a rerender after having changed the name
218+
expect(spy).toHaveBeenCalledTimes(4); // expect a rerender after having changed the name
219219
});
220220
});

test/unit/components/relative.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('<FormattedRelative>', () => {
4545
const spy = jest.fn().mockImplementation(() => null);
4646
mountWithProvider({value: 0, children: spy}, intl);
4747
mountWithProvider({value: 1, children: spy}, intl);
48-
expect(spy).toHaveBeenCalledTimes(2);
48+
expect(spy).toHaveBeenCalledTimes(4);
4949
});
5050

5151
it('should re-render when context changes', () => {
@@ -56,7 +56,7 @@ describe('<FormattedRelative>', () => {
5656
mountWithProvider({value: 0, children: spy}, intl);
5757
mountWithProvider({value: 0, children: spy}, otherIntl);
5858

59-
expect(spy).toHaveBeenCalledTimes(2);
59+
expect(spy).toHaveBeenCalledTimes(4);
6060
});
6161

6262
it('accepts valid IntlRelativeTimeFormat options as props', () => {
@@ -110,7 +110,7 @@ describe('<FormattedRelative>', () => {
110110
const spy = jest.fn().mockImplementation(() => <b>Jest</b>);
111111
const rendered = mountWithProvider({value: 0, children: spy}, intl);
112112

113-
expect(spy).toHaveBeenCalledTimes(1);
113+
expect(spy).toHaveBeenCalledTimes(2);
114114
expect(spy.mock.calls[0]).toEqual([intl.formatRelativeTime(0)]);
115115

116116
expect(

test/unit/testUtils.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import {mount} from 'enzyme';
33
import {WithIntlProps} from '../../src/components/injectIntl';
44
import Provider, {Props as ProviderProps} from '../../src/components/provider';
55

6+
function StrictProvider(props: ProviderProps) {
7+
return (
8+
<React.StrictMode>
9+
<Provider {...props} />
10+
</React.StrictMode>
11+
);
12+
}
13+
614
export function mountFormattedComponentWithProvider<P>(
715
Comp: React.ComponentType<P>
816
) {
@@ -13,7 +21,7 @@ export function mountFormattedComponentWithProvider<P>(
1321
return mount(
1422
<Comp {...props} />,
1523
{
16-
wrappingComponent: Provider,
24+
wrappingComponent: StrictProvider,
1725
wrappingComponentProps: providerProps,
1826
} as any // Seems like DefinitelyTyped types are outdated
1927
);

0 commit comments

Comments
 (0)