From 87f02e8ca73637f26697712ec7ff6e9e0d992944 Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Thu, 17 Nov 2016 20:11:59 -0800 Subject: [PATCH] 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 --- Tests/IGListAdapterTests.m | 89 +++++++++++++++++++++++++++++++ Tests/Objects/IGListTestSection.h | 2 + Tests/Objects/IGListTestSection.m | 4 +- 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/Tests/IGListAdapterTests.m b/Tests/IGListAdapterTests.m index cfc19535..1253a478 100644 --- a/Tests/IGListAdapterTests.m +++ b/Tests/IGListAdapterTests.m @@ -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 diff --git a/Tests/Objects/IGListTestSection.h b/Tests/Objects/IGListTestSection.h index 56543536..6f5179b8 100644 --- a/Tests/Objects/IGListTestSection.h +++ b/Tests/Objects/IGListTestSection.h @@ -16,4 +16,6 @@ @property (nonatomic, assign) NSInteger items; +@property (nonatomic, assign) BOOL wasSelected; + @end diff --git a/Tests/Objects/IGListTestSection.m b/Tests/Objects/IGListTestSection.m index 2a8a3623..360ba8c8 100644 --- a/Tests/Objects/IGListTestSection.m +++ b/Tests/Objects/IGListTestSection.m @@ -35,6 +35,8 @@ } } -- (void)didSelectItemAtIndex:(NSInteger)index {} +- (void)didSelectItemAtIndex:(NSInteger)index { + self.wasSelected = YES; +} @end