|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#import <XCTest/XCTest.h> |
| 9 | + |
| 10 | +#import "IGTestCell.h" |
| 11 | +#import "IGListTestCase.h" |
| 12 | +#import "IGListAdapterInternal.h" |
| 13 | +#import "IGTestCell.h" |
| 14 | +#import "IGTestBindingSingleItemDataSource.h" |
| 15 | + |
| 16 | + |
| 17 | +@interface IGListBindingSingleSectionControllerTests : IGListTestCase |
| 18 | + |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation IGListBindingSingleSectionControllerTests |
| 22 | + |
| 23 | +- (void)setUp { |
| 24 | + self.dataSource = [IGTestBindingSingleItemDataSource new]; |
| 25 | + self.frame = CGRectMake(0, 0, 100, 1000); |
| 26 | + [super setUp]; |
| 27 | +} |
| 28 | + |
| 29 | +- (void)test_whenSetupWithObjects_collectionViewHasSections { |
| 30 | + [self setupWithObjects:@[ |
| 31 | + genTestObject(@1, @"Foo"), |
| 32 | + genTestObject(@2, @"Bar"), |
| 33 | + genTestObject(@3, @"Baz"), |
| 34 | + ]]; |
| 35 | + XCTAssertEqual([self.collectionView numberOfSections], 3); |
| 36 | + XCTAssertEqual([self.collectionView numberOfItemsInSection:0], 1); |
| 37 | + XCTAssertEqual([self.collectionView numberOfItemsInSection:1], 1); |
| 38 | + XCTAssertEqual([self.collectionView numberOfItemsInSection:2], 1); |
| 39 | +} |
| 40 | + |
| 41 | +- (void)test_whenSetupWithObjects_sizeIsCalled { |
| 42 | + [self setupWithObjects:@[ |
| 43 | + genTestObject(@1, @"Foo"), |
| 44 | + genTestObject(@2, @"Bar"), |
| 45 | + genTestObject(@3, @"Baz"), |
| 46 | + ]]; |
| 47 | + IGTestCell *cell1 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 48 | + IGTestCell *cell2 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 49 | + IGTestCell *cell3 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]]; |
| 50 | + |
| 51 | + // Check the size is set in `IGTestBindingSingleSectionController` |
| 52 | + XCTAssertEqual(cell1.frame.size.height, 44); |
| 53 | + XCTAssertEqual(cell2.frame.size.height, 44); |
| 54 | + XCTAssertEqual(cell3.frame.size.height, 44); |
| 55 | + XCTAssertEqual(cell1.frame.size.width, 100); |
| 56 | + XCTAssertEqual(cell2.frame.size.width, 100); |
| 57 | + XCTAssertEqual(cell3.frame.size.width, 100); |
| 58 | +} |
| 59 | + |
| 60 | +- (void)test_whenSetupWithObjects_cellsAreConfigured { |
| 61 | + [self setupWithObjects:@[ |
| 62 | + genTestObject(@1, @"Foo"), |
| 63 | + genTestObject(@2, @"Bar"), |
| 64 | + genTestObject(@3, @"Baz"), |
| 65 | + ]]; |
| 66 | + IGTestCell *cell1 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 67 | + IGTestCell *cell2 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1]]; |
| 68 | + IGTestCell *cell3 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:2]]; |
| 69 | + |
| 70 | + // Check the cell is configured in `IGTestBindingSingleSectionController` |
| 71 | + XCTAssertEqualObjects(cell1.label.text, @"Foo"); |
| 72 | + XCTAssertEqualObjects(cell2.label.text, @"Bar"); |
| 73 | + XCTAssertEqualObjects(cell3.label.text, @"Baz"); |
| 74 | +} |
| 75 | + |
| 76 | +- (void)test_whenSetupWithObjects_cellClassIsExpected { |
| 77 | + [self setupWithObjects:@[ |
| 78 | + genTestObject(@1, @"Foo"), |
| 79 | + genTestObject(@2, @"Bar"), |
| 80 | + genTestObject(@3, @"Baz"), |
| 81 | + ]]; |
| 82 | + UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 83 | + XCTAssertTrue([cell isKindOfClass:[IGTestCell class]]); |
| 84 | +} |
| 85 | + |
| 86 | +- (void)test_whenDidSelectIsCalled_subclassIsCalled { |
| 87 | + [self setupWithObjects:@[ |
| 88 | + genTestObject(@1, @"Foo"), |
| 89 | + ]]; |
| 90 | + IGListSectionController *controller = [self.adapter sectionControllerForSection:0]; |
| 91 | + [controller didSelectItemAtIndex:0]; |
| 92 | + IGTestCell *cell1 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 93 | + |
| 94 | + // Check the cell label is updated in `IGTestBindingSingleSectionController` |
| 95 | + XCTAssertEqualObjects(cell1.label.text, @"did-select"); |
| 96 | +} |
| 97 | + |
| 98 | +- (void)test_whenDidDeselectIsCalled_subclassIsCalled { |
| 99 | + [self setupWithObjects:@[ |
| 100 | + genTestObject(@1, @"Foo"), |
| 101 | + ]]; |
| 102 | + IGListSectionController *controller = [self.adapter sectionControllerForSection:0]; |
| 103 | + [controller didDeselectItemAtIndex:0]; |
| 104 | + IGTestCell *cell1 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 105 | + |
| 106 | + // Check the cell label is updated in `IGTestBindingSingleSectionController` |
| 107 | + XCTAssertEqualObjects(cell1.label.text, @"did-deselect"); |
| 108 | +} |
| 109 | + |
| 110 | +- (void)test_whenDidHighlightIsCalled_subclassIsCalled { |
| 111 | + [self setupWithObjects:@[ |
| 112 | + genTestObject(@1, @"Foo"), |
| 113 | + ]]; |
| 114 | + IGListSectionController *controller = [self.adapter sectionControllerForSection:0]; |
| 115 | + [controller didHighlightItemAtIndex:0]; |
| 116 | + IGTestCell *cell1 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 117 | + |
| 118 | + // Check the cell label is updated in `IGTestBindingSingleSectionController` |
| 119 | + XCTAssertEqualObjects(cell1.label.text, @"did-highlight"); |
| 120 | +} |
| 121 | + |
| 122 | +- (void)test_whenDidUnhighlightIsCalled_subclassIsCalled { |
| 123 | + [self setupWithObjects:@[ |
| 124 | + genTestObject(@1, @"Foo"), |
| 125 | + ]]; |
| 126 | + IGListSectionController *controller = [self.adapter sectionControllerForSection:0]; |
| 127 | + [controller didUnhighlightItemAtIndex:0]; |
| 128 | + IGTestCell *cell1 = (IGTestCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; |
| 129 | + |
| 130 | + // Check the cell label is updated in `IGTestBindingSingleSectionController` |
| 131 | + XCTAssertEqualObjects(cell1.label.text, @"did-unhighlight"); |
| 132 | +} |
| 133 | + |
| 134 | +@end |
0 commit comments