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 all commits
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
2 changes: 1 addition & 1 deletion lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function keyChanged(key, data, hasNewValue = true) {
*/
function sendDataToConnection(config, val, key) {
if (config.withOnyxInstance) {
config.withOnyxInstance.setInitialState(config.statePropertyName, val);
config.withOnyxInstance.setWithOnyxState(config.statePropertyName, val);
} else if (_.isFunction(config.callback)) {
config.callback(val, key);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/withOnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function (mapOnyxToState) {
constructor(props) {
super(props);

this.setInitialState = this.setInitialState.bind(this);
this.setWithOnyxState = this.setWithOnyxState.bind(this);

// This stores all the Onyx connection IDs to be used when the component unmounts so everything can be
// disconnected. It is a key value store with the format {[mapping.key]: connectionID}.
Expand Down Expand Up @@ -86,9 +86,9 @@ export default function (mapOnyxToState) {
* @param {String} statePropertyName
* @param {*} val
*/
setInitialState(statePropertyName, val) {
setWithOnyxState(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');
this.setState({[statePropertyName]: val});
return;
}

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