Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prop dependent withOnyx updates + add test #98

Merged
merged 2 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/withOnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ export default function (mapOnyxToState) {
* @param {*} val
*/
setInitialState(statePropertyName, val) {
if (!this.state.loading) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly out of curiosity: why are we reversing this if?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic has changed from what we had before which was incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to invert the logic though it's just how I did it. Updated so this is unchanged but the logic is the same.

console.error('withOnyx.setInitialState() called after loading: false');
return;
}
if (this.state.loading) {
this.tempState[statePropertyName] = val;

this.tempState[statePropertyName] = val;
// All state keys should exist and at least have a value of null
if (_.some(requiredKeysForInit, key => _.isUndefined(this.tempState[key]))) {
return;
}

// All state keys should exist and at least have a value of null
if (_.some(requiredKeysForInit, key => _.isUndefined(this.tempState[key]))) {
this.setState({...this.tempState, loading: false});
delete this.tempState;
return;
}

this.setState({...this.tempState, loading: false});
delete this.tempState;
this.setState({[statePropertyName]: val});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/components/ViewWithText.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const propTypes = {

const ViewWithText = props => (
<View>
<Text>{props.text}</Text>
<Text testID="text-element">{props.text}</Text>
</View>
);

Expand Down
30 changes: 30 additions & 0 deletions tests/unit/withOnyxTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ describe('withOnyx', () => {
});
});

it('should update if a prop dependent key changes', () => {
let rerender;
let getByTestId;
const onRender = jest.fn();
const TestComponentWithOnyx = withOnyx({
text: {
key: props => `${ONYX_KEYS.COLLECTION.TEST_KEY}${props.collectionID}`,
},
})(ViewWithText);
Onyx.set(`${ONYX_KEYS.COLLECTION.TEST_KEY}1`, 'test_1');
Onyx.set(`${ONYX_KEYS.COLLECTION.TEST_KEY}2`, 'test_2');
return waitForPromisesToResolve()
.then(() => {
const result = render(<TestComponentWithOnyx onRender={onRender} collectionID="1" />);
rerender = result.rerender;
getByTestId = result.getByTestId;
return waitForPromisesToResolve();
})
.then(() => {
expect(getByTestId('text-element').props.children).toEqual('test_1');
})
.then(() => {
rerender(<TestComponentWithOnyx collectionID="2" />);
return waitForPromisesToResolve();
})
.then(() => {
expect(getByTestId('text-element').props.children).toEqual('test_2');
});
});

it('should pass a prop from one connected component to another', () => {
const collectionItemID = 1;
const onRender = jest.fn();
Expand Down