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

[React Native] Add tests to paper renderer for measure, measureLayout #15323

Merged
merged 3 commits into from
Apr 9, 2019

Conversation

TheSavior
Copy link
Member

We will be changing these functions for Fabric so starting by adding some tests to validate the current behavior.

Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

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

Not sure how I feel about some of the assertions in the new tests. Going to think on it a bit.

I realize I approved a PR (#15126) recently with similar tests. I guess I didn't read the tests closely enough to notice the thing that's nagging me/

const args = UIManager.measure.mock.calls[0];
expect(successCallback).not.toBeCalled();
args[1]('success');
expect(successCallback).toBeCalledWith('success');
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems a little janky too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here, I agree with you. The coupling is tight as a side effect of an array being used for the arguments instead of a hash with a named key. Given the design of the code, I’m not sure what else to do except make a test helper function that wraps this detail and give it an intention-revealing name.

Copy link
Member Author

Choose a reason for hiding this comment

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

The specific reason I wanted to test this is because one of the UIManager measure calls takes the arguments fail, success, but the host component takes the arguments success, fail and flips the order when passed to the UIManager. I wanted to validate that this stays the correct order because it is trivially easy to break (or think that the existing code is wrong).

I can't assert that the callback is the callback that is passed in because it is wrapped, so calling it and validating the argument felt like the most appropriate thing.

expect(UIManager.measure).toHaveBeenCalledWith(
expect.any(Number),
expect.any(Function),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems a little janky. Like it's kind of testing internal API details.

Copy link
Contributor

Choose a reason for hiding this comment

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

testing the module’s interactions with the public API of its collaborators, rather than the aggregate behavior of said multiple modules, is how you decouple a test like this from implementation details.

from Xunit patterns book:

A Mock Object provides the system under test with both indirect input and a way to verify indirect output. All the expectations are configured before the calling of the method under test.

Asserting that no outbound calls to UIManager.measure happen before the viewRef.measure call is a good pinning test and test as documentation IMO.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think my comments were probably swapped. This seems janky and the other one seems like it's monkeying with internals a bit too much for my comfort.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to change this to instead have a mock implementation that will throw if passed anything other than a number in the first argument and to call the passed callback with arguments. Then my assertion will just validate that the success callback was called with the expected arguments.

@TheSavior
Copy link
Member Author

TheSavior commented Apr 9, 2019

Alright, I updated the tests to match the new style we talked about, including the other tests in this file that was the old style.

@TheSavior TheSavior merged commit c7a9599 into facebook:master Apr 9, 2019
@TheSavior TheSavior deleted the measure-tests branch April 9, 2019 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants