Unit test cell selection, display, and scroll dragging events

Summary:
Fixes #192, #193

- [x] All tests pass. Demo project builds and runs.
- [x] I added tests, an experiment, or detailed why my change isn't tested.
- [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/master/CONTRIBUTING.md)
Closes https://github.com/Instagram/IGListKit/pull/219

Reviewed By: rnystrom

Differential Revision: D4201039

Pulled By: jessesquires

fbshipit-source-id: 39cd7d712d1ebef52c0ec3e148f532d5f62e56c8
This commit is contained in:
Ryan Nystrom 2016-11-17 20:11:59 -08:00 committed by Facebook Github Bot
parent 24174dcbf6
commit 87f02e8ca7
3 changed files with 94 additions and 1 deletions

View file

@ -742,4 +742,93 @@ XCTAssertEqual(CGPointEqualToPoint(point, p), YES); \
[mockDelegate verify];
}
- (void)test_whenSelectingCell_thatCollectionViewDelegateReceivesMethod {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockDelegate = [OCMockObject mockForProtocol:@protocol(UICollectionViewDelegate)];
self.adapter.collectionViewDelegate = mockDelegate;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[[mockDelegate expect] collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
// simulates the collectionview telling its delegate that it was tapped
[self.adapter collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
[mockDelegate verify];
}
- (void)test_whenSelectingCell_thatSectionControllerReceivesMethod {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
// simulates the collectionview telling its delegate that it was tapped
[self.adapter collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
IGListTestSection *s0 = [self.adapter sectionControllerForObject:@0];
IGListTestSection *s1 = [self.adapter sectionControllerForObject:@1];
IGListTestSection *s2 = [self.adapter sectionControllerForObject:@2];
XCTAssertTrue(s0.wasSelected);
XCTAssertFalse(s1.wasSelected);
XCTAssertFalse(s2.wasSelected);
}
- (void)test_whenDisplayingCell_thatCollectionViewDelegateReceivesMethod {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockDelegate = [OCMockObject mockForProtocol:@protocol(UICollectionViewDelegate)];
self.adapter.collectionViewDelegate = mockDelegate;
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
UICollectionViewCell *cell = [UICollectionViewCell new];
[[mockDelegate expect] collectionView:self.collectionView willDisplayCell:cell forItemAtIndexPath:indexPath];
// simulates the collectionview telling its delegate that a cell will be displayed
[self.adapter collectionView:self.collectionView willDisplayCell:cell forItemAtIndexPath:indexPath];
[mockDelegate verify];
}
- (void)test_whenWillBeginDragging_thatScrollViewDelegateReceivesMethod {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockCollectionDelegate = [OCMockObject mockForProtocol:@protocol(UICollectionViewDelegate)];
id mockScrollDelegate = [OCMockObject mockForProtocol:@protocol(UIScrollViewDelegate)];
self.adapter.collectionViewDelegate = mockCollectionDelegate;
self.adapter.scrollViewDelegate = mockScrollDelegate;
[[mockCollectionDelegate reject] scrollViewWillBeginDragging:self.collectionView];
[[mockScrollDelegate expect] scrollViewWillBeginDragging:self.collectionView];
// simulates the scrollview delegate telling the adapter that it will begin dragging
[self.adapter scrollViewWillBeginDragging:self.collectionView];
[mockCollectionDelegate verify];
[mockScrollDelegate verify];
}
- (void)test_whenDidEndDragging_thatScrollViewDelegateReceivesMethod {
self.dataSource.objects = @[@0, @1, @2];
[self.adapter reloadDataWithCompletion:nil];
id mockCollectionDelegate = [OCMockObject mockForProtocol:@protocol(UICollectionViewDelegate)];
id mockScrollDelegate = [OCMockObject mockForProtocol:@protocol(UIScrollViewDelegate)];
self.adapter.collectionViewDelegate = mockCollectionDelegate;
self.adapter.scrollViewDelegate = mockScrollDelegate;
[[mockCollectionDelegate reject] scrollViewDidEndDragging:self.collectionView willDecelerate:NO];
[[mockScrollDelegate expect] scrollViewDidEndDragging:self.collectionView willDecelerate:NO];
// simulates the scrollview delegate telling the adapter that it will end dragging
[self.adapter scrollViewDidEndDragging:self.collectionView willDecelerate:NO];
[mockCollectionDelegate verify];
[mockScrollDelegate verify];
}
@end

View file

@ -16,4 +16,6 @@
@property (nonatomic, assign) NSInteger items;
@property (nonatomic, assign) BOOL wasSelected;
@end

View file

@ -35,6 +35,8 @@
}
}
- (void)didSelectItemAtIndex:(NSInteger)index {}
- (void)didSelectItemAtIndex:(NSInteger)index {
self.wasSelected = YES;
}
@end