2019-12-19 17:32:49 +00:00
|
|
|
/*
|
2023-04-06 09:44:16 +00:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2016-09-07 22:37:59 +00:00
|
|
|
*
|
2018-05-01 21:33:50 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-09-07 22:37:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
2019-12-19 17:32:49 +00:00
|
|
|
|
2016-12-13 15:22:49 +00:00
|
|
|
#import <OCMock/OCMock.h>
|
2016-09-07 22:37:59 +00:00
|
|
|
|
2016-12-13 15:22:49 +00:00
|
|
|
#import "IGListAdapterInternal.h"
|
2019-12-19 17:32:49 +00:00
|
|
|
#import "IGListTestCase.h"
|
2016-09-07 22:37:59 +00:00
|
|
|
#import "IGTestCell.h"
|
|
|
|
|
#import "IGTestSingleItemDataSource.h"
|
2017-12-18 20:17:48 +00:00
|
|
|
#import "IGTestSingleWithoutDeselectionDelegate.h"
|
2016-09-07 22:37:59 +00:00
|
|
|
|
2017-04-20 21:35:26 +00:00
|
|
|
@interface IGListSingleSectionControllerTests : IGListTestCase
|
2016-09-07 22:37:59 +00:00
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation IGListSingleSectionControllerTests
|
|
|
|
|
|
|
|
|
|
- (void)setUp {
|
2017-04-20 21:35:26 +00:00
|
|
|
self.dataSource = [IGTestSingleItemDataSource new];
|
2019-12-12 19:10:22 +00:00
|
|
|
self.frame = CGRectMake(0, 0, 100, 1000);
|
2016-09-07 22:37:59 +00:00
|
|
|
[super setUp];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)test_whenDisplayingCollectionView_thatSectionsHaveOneItem {
|
|
|
|
|
[self setupWithObjects:@[
|
|
|
|
|
genTestObject(@1, @"Foo"),
|
|
|
|
|
genTestObject(@2, @"Bar"),
|
|
|
|
|
genTestObject(@3, @"Baz"),
|
|
|
|
|
]];
|
|
|
|
|
XCTAssertEqual([self.collectionView numberOfSections], 3);
|
|
|
|
|
XCTAssertEqual([self.collectionView numberOfItemsInSection:0], 1);
|
|
|
|
|
XCTAssertEqual([self.collectionView numberOfItemsInSection:1], 1);
|
|
|
|
|
XCTAssertEqual([self.collectionView numberOfItemsInSection:2], 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 19:10:22 +00:00
|
|
|
- (void)test_whenDisplayingCollectionView_thatCellsAreConfigured {
|
2016-09-07 22:37:59 +00:00
|
|
|
[self setupWithObjects:@[
|
|
|
|
|
genTestObject(@1, @"Foo"),
|
|
|
|
|
genTestObject(@2, @"Bar"),
|
|
|
|
|
genTestObject(@3, @"Baz"),
|
|
|
|
|
]];
|
|
|
|
|
IGTestCell *cell1 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
|
|
|
|
|
IGTestCell *cell2 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
|
|
|
|
|
IGTestCell *cell3 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]];
|
|
|
|
|
XCTAssertEqualObjects(cell1.label.text, @"Foo");
|
|
|
|
|
XCTAssertEqualObjects(cell2.label.text, @"Bar");
|
|
|
|
|
XCTAssertEqualObjects(cell3.label.text, @"Baz");
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-12 19:10:22 +00:00
|
|
|
- (void)test_whenDisplayingCollectionView_thatCellsAreSized {
|
2016-09-07 22:37:59 +00:00
|
|
|
[self setupWithObjects:@[
|
|
|
|
|
genTestObject(@1, @"Foo"),
|
|
|
|
|
genTestObject(@2, @"Bar"),
|
|
|
|
|
genTestObject(@3, @"Baz"),
|
|
|
|
|
]];
|
|
|
|
|
IGTestCell *cell1 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
|
|
|
|
|
IGTestCell *cell2 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
|
|
|
|
|
IGTestCell *cell3 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]];
|
|
|
|
|
XCTAssertEqual(cell1.frame.size.height, 44);
|
|
|
|
|
XCTAssertEqual(cell2.frame.size.height, 44);
|
|
|
|
|
XCTAssertEqual(cell3.frame.size.height, 44);
|
|
|
|
|
XCTAssertEqual(cell1.frame.size.width, 100);
|
|
|
|
|
XCTAssertEqual(cell2.frame.size.width, 100);
|
|
|
|
|
XCTAssertEqual(cell3.frame.size.width, 100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)test_whenItemUpdated_thatCellIsConfigured {
|
|
|
|
|
[self setupWithObjects:@[
|
|
|
|
|
genTestObject(@1, @"Foo"),
|
|
|
|
|
genTestObject(@2, @"Bar"),
|
|
|
|
|
genTestObject(@3, @"Baz"),
|
|
|
|
|
]];
|
|
|
|
|
self.dataSource.objects = @[
|
|
|
|
|
genTestObject(@1, @"Foo"),
|
|
|
|
|
genTestObject(@2, @"Qux"), // new value
|
|
|
|
|
genTestObject(@3, @"Baz"),
|
|
|
|
|
];
|
|
|
|
|
XCTestExpectation *expectation = genExpectation;
|
|
|
|
|
[self.adapter performUpdatesAnimated:YES completion:^(BOOL finished) {
|
|
|
|
|
IGTestCell *cell2 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]];
|
|
|
|
|
XCTAssertEqualObjects(cell2.label.text, @"Qux");
|
|
|
|
|
[expectation fulfill];
|
|
|
|
|
}];
|
2017-04-19 19:50:20 +00:00
|
|
|
[self waitForExpectationsWithTimeout:30 handler:nil];
|
2016-09-07 22:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-13 15:22:49 +00:00
|
|
|
- (void)test_whenSelected_thatDelegateReceivesEvent {
|
|
|
|
|
[self setupWithObjects:@[
|
|
|
|
|
genTestObject(@1, @"a")
|
|
|
|
|
]];
|
|
|
|
|
IGListSingleSectionController *section = [self.adapter sectionControllerForObject:self.dataSource.objects.firstObject];
|
|
|
|
|
id mockDelegate = [OCMockObject mockForProtocol:@protocol(IGListSingleSectionControllerDelegate)];
|
|
|
|
|
section.selectionDelegate = mockDelegate;
|
2017-01-11 23:01:13 +00:00
|
|
|
[[mockDelegate expect] didSelectSectionController:section withObject:self.dataSource.objects.firstObject];
|
2016-12-13 15:22:49 +00:00
|
|
|
[self.adapter collectionView:self.collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
|
|
|
|
|
[mockDelegate verify];
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-18 20:17:48 +00:00
|
|
|
- (void)test_whenDeselected_thatDelegateReceivesEvent {
|
|
|
|
|
[self setupWithObjects:@[
|
|
|
|
|
genTestObject(@1, @"a")
|
|
|
|
|
]];
|
|
|
|
|
IGListSingleSectionController *section = [self.adapter sectionControllerForObject:self.dataSource.objects.firstObject];
|
|
|
|
|
id mockDelegate = [OCMockObject mockForProtocol:@protocol(IGListSingleSectionControllerDelegate)];
|
|
|
|
|
section.selectionDelegate = mockDelegate;
|
|
|
|
|
[[mockDelegate expect] didDeselectSectionController:section withObject:self.dataSource.objects.firstObject];
|
|
|
|
|
[self.adapter collectionView:self.collectionView didDeselectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
|
|
|
|
|
[mockDelegate verify];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)test_whenDeselected_withoutImplementation_thatNoOps {
|
|
|
|
|
[self setupWithObjects:@[
|
|
|
|
|
genTestObject(@1, @"a")
|
|
|
|
|
]];
|
|
|
|
|
IGListSingleSectionController *section = [self.adapter sectionControllerForObject:self.dataSource.objects.firstObject];
|
|
|
|
|
IGTestSingleWithoutDeselectionDelegate *delegate = [IGTestSingleWithoutDeselectionDelegate new];
|
|
|
|
|
section.selectionDelegate = delegate;
|
|
|
|
|
|
|
|
|
|
[self.adapter collectionView:self.collectionView didDeselectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
|
|
|
|
|
XCTAssertFalse(delegate.selected);
|
|
|
|
|
|
|
|
|
|
[self.adapter collectionView:self.collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
|
|
|
|
|
XCTAssertTrue(delegate.selected);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 22:37:59 +00:00
|
|
|
@end
|