-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[ASScrollNode] Fix small bugs and add unit tests #637
Conversation
nguyenhuy
commented
Oct 24, 2017
•
edited
Loading
edited
- Currently, ASScrollNode's calculated size is based on its parent size. However, there are cases in which the size range passed to the scroll node to calculate a layout against doesn't include the parent size. In such cases, we should clamp the parent size against the size range to make sure the range is always respected.
- When scrollable directions change, invalidate the calculated layout because these directions affect the size range that is used to calculate the content's layout.
- Add unit tests to cover these bugs and other common use cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful! Comments and unit tests and useful fixes 💯
Tests/ASScrollNodeTests.m
Outdated
subnodeSizeRange = sizeRange; | ||
subnodeSizeRange.max.width = CGFLOAT_MAX; | ||
|
||
XCTAssertTrue(ASSizeRangeEqualToSizeRange(_subnode.constrainedSizeForCalculatedLayout, subnodeSizeRange)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Let's use ASXCTAssertEqualSizeRanges
and ASXCTAssertEqualSizes
where possible, so that we get better error messages. Import ASXCTExtensions.h
to get 'em.
Tests/ASScrollNodeTests.m
Outdated
_subnode.style.preferredSize = subnodeSize; | ||
|
||
ASLayout *layout = [_scrollNode layoutThatFits:sizeRange parentSize:parentSize]; | ||
[_scrollNode layout]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another nit: I prefer to name ivars without underscores in unit test files. It makes things more readable and it's usually safe since a new instance is created for each test method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks <3
7d650c3
to
5b60aea
Compare
🚫 CI failed with log |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great to see improvements in test code. Thank you, @nguyenhuy !
Tests/ASScrollNodeTests.m
Outdated
ASLayout *layout = [scrollNode layoutThatFits:sizeRange parentSize:parentSize]; | ||
[scrollNode layout]; | ||
ASLayout *layout = [self.scrollNode layoutThatFits:sizeRange parentSize:parentSize]; | ||
[self.scrollNode layout]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems better (perhaps) to call [scrollNode.layer layoutIfNeeded], to test the full layout stack as it is triggered in most apps?
Tests/ASScrollNodeTests.m
Outdated
|
||
ASSizeRange subnodeSizeRange = sizeRange; | ||
subnodeSizeRange.max.height = CGFLOAT_MAX; | ||
XCTAssertEqual(scrollNode.scrollableDirections, ASScrollDirectionVerticalDirections); | ||
ASXCTAssertEqualSizeRanges(subnode.constrainedSizeForCalculatedLayout, subnodeSizeRange); | ||
XCTAssertEqual(self.scrollNode.scrollableDirections, ASScrollDirectionVerticalDirections); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than self. everywhere, using the ivar like _scrollNode would be more readable (especially in property chains & methods that use it a lot).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a follow-up to this, I find that using non-underscored ivar names in test classes provides the best readability.
Since the object is destroyed after -tearDown
is run, and since test classes usually have trivial accessors, direct ivar access is safe and convenient.
d35f053
to
8d33fd4
Compare
* Add unit tests for ASScrollNode * Make sure ASScrollNode's size is clamped against its size range * Invalidate ASScrollNode's calculated layout if its scrollable directions changed * Update comment * Update CHANGELOG * Address Adlai's comments