mirror of
https://github.com/Instagram/IGListKit
synced 2026-05-06 06:58:26 +00:00
Summary: Rename *ItemController unit tests to *SectionController, addressing issue #126 - [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/129 Differential Revision: D4095496 Pulled By: rnystrom fbshipit-source-id: 277be38ed2789f14199cee9d711a838552921f10
120 lines
5.2 KiB
Objective-C
120 lines
5.2 KiB
Objective-C
/**
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
#import "IGTestCell.h"
|
|
#import "IGTestSingleItemDataSource.h"
|
|
|
|
#define genTestObject(k, v) [[IGTestObject alloc] initWithKey:k value:v]
|
|
|
|
#define genExpectation [self expectationWithDescription:NSStringFromSelector(_cmd)]
|
|
|
|
@interface IGListSingleSectionControllerTests : XCTestCase
|
|
|
|
@property (nonatomic, strong) IGListCollectionView *collectionView;
|
|
@property (nonatomic, strong) IGListAdapter *adapter;
|
|
@property (nonatomic, strong) IGListAdapterUpdater *updater;
|
|
@property (nonatomic, strong) IGTestSingleItemDataSource *dataSource;
|
|
@property (nonatomic, strong) UIWindow *window;
|
|
|
|
@end
|
|
|
|
@implementation IGListSingleSectionControllerTests
|
|
|
|
- (void)setUp {
|
|
[super setUp];
|
|
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
|
|
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
|
|
self.collectionView = [[IGListCollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) collectionViewLayout:layout];
|
|
[self.window addSubview:self.collectionView];
|
|
self.dataSource = [[IGTestSingleItemDataSource alloc] init];
|
|
self.updater = [[IGListAdapterUpdater alloc] init];
|
|
self.adapter = [[IGListAdapter alloc] initWithUpdater:self.updater viewController:nil workingRangeSize:2];
|
|
}
|
|
|
|
- (void)tearDown {
|
|
[super tearDown];
|
|
self.window = nil;
|
|
self.collectionView = nil;
|
|
self.adapter = nil;
|
|
self.dataSource = nil;
|
|
}
|
|
|
|
- (void)setupWithObjects:(NSArray *)objects {
|
|
self.dataSource.objects = objects;
|
|
self.adapter.collectionView = self.collectionView;
|
|
self.adapter.dataSource = self.dataSource;
|
|
[self.collectionView layoutIfNeeded];
|
|
}
|
|
|
|
- (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);
|
|
}
|
|
|
|
- (void)test_whenDisplayingCollectionView_thatCellsAreConfigured {
|
|
[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");
|
|
}
|
|
|
|
- (void)test_whenDisplayingCollectionView_thatCellsAreSized {
|
|
[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];
|
|
}];
|
|
[self waitForExpectationsWithTimeout:15 handler:nil];
|
|
}
|
|
|
|
@end
|