Commit 75a407d4 by Gabriel O'Flaherty-Chan

Merge pull request #108 from Shopify/feature/collection-pagination

Pagination Support for Collections
parents b668e82e 63e49cc2
...@@ -215,6 +215,57 @@ ...@@ -215,6 +215,57 @@
}]; }];
} }
- (void)testCollectionsFromFirstPage
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetCollection_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollectionsPage:1 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {
XCTAssertEqual(1, page);
XCTAssertNotNil((collections));
XCTAssertNil(error);
XCTAssertNotNil([collections.firstObject title]);
XCTAssertNotNil([collections.firstObject handle]);
XCTAssertNotNil([collections.firstObject collectionId]);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
XCTAssertNil(error);
}];
}
- (void)testCollectionsFromEmptyPage
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetOutOfIndexCollectionPage_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollectionsPage:999 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {
XCTAssertEqual(999, page);
XCTAssertNotNil((collections));
XCTAssert(collections.count == 0);
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
XCTAssertNil(error);
}];
}
- (void)testProductsInCollection - (void)testProductsInCollection
{ {
if (self.collection == nil) { if (self.collection == nil) {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -152,6 +152,14 @@ typedef void (^BUYDataShippingRatesBlock)(NSArray *shippingRates, BUYStatus stat ...@@ -152,6 +152,14 @@ typedef void (^BUYDataShippingRatesBlock)(NSArray *shippingRates, BUYStatus stat
typedef void (^BUYDataShopBlock)(BUYShop *shop, NSError *error); typedef void (^BUYDataShopBlock)(BUYShop *shop, NSError *error);
/** /**
* Return block containing a list of BUYCollection objects and/or an NSError
*
* @param collections An array of BUYCollection objects
* @param error Optional NSError
*/
typedef void (^BUYDataCollectionsBlock)(NSArray<BUYCollection *> *collections, NSError *error);
/**
* Return block containing a BUYProduct and/or an NSError * Return block containing a BUYProduct and/or an NSError
* *
* @param product A BUYProduct * @param product A BUYProduct
...@@ -165,15 +173,17 @@ typedef void (^BUYDataProductBlock)(BUYProduct *product, NSError *error); ...@@ -165,15 +173,17 @@ typedef void (^BUYDataProductBlock)(BUYProduct *product, NSError *error);
* @param products An array of BUYProduct objects * @param products An array of BUYProduct objects
* @param error Optional NSError * @param error Optional NSError
*/ */
typedef void (^BUYDataProductsBlock)(NSArray *products, NSError *error); typedef void (^BUYDataProductsBlock)(NSArray<BUYProduct *> *products, NSError *error);
/** /**
* Return block containing list of collections * Return block containing a list of BUYCollection objects
* *
* @param collections An array of BUYCollection objects * @param collections An array of BUYCollection objects
* @param error Optional NSError * @param page Index of the page requested
* @param reachedEnd Boolean indicating whether additional pages exist
* @param error An optional NSError
*/ */
typedef void (^BUYDataCollectionsBlock)(NSArray *collections, NSError *error); typedef void (^BUYDataCollectionsListBlock)(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error);
/** /**
* Return block containing a list of BUYProduct objects, the page requested, a boolean to determine whether the end of the list has been reach and/or an optional NSError * Return block containing a list of BUYProduct objects, the page requested, a boolean to determine whether the end of the list has been reach and/or an optional NSError
...@@ -183,7 +193,7 @@ typedef void (^BUYDataCollectionsBlock)(NSArray *collections, NSError *error); ...@@ -183,7 +193,7 @@ typedef void (^BUYDataCollectionsBlock)(NSArray *collections, NSError *error);
* @param reachedEnd Boolean indicating whether additional pages exist * @param reachedEnd Boolean indicating whether additional pages exist
* @param error An optional NSError * @param error An optional NSError
*/ */
typedef void (^BUYDataProductListBlock)(NSArray *products, NSUInteger page, BOOL reachedEnd, NSError *error); typedef void (^BUYDataProductListBlock)(NSArray<BUYProduct *> *products, NSUInteger page, BOOL reachedEnd, NSError *error);
/** /**
* Return block containing a list of BUYProductImage objects and/or an NSError * Return block containing a list of BUYProductImage objects and/or an NSError
...@@ -314,6 +324,16 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error); ...@@ -314,6 +324,16 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error);
- (NSURLSessionDataTask *)getCollections:(BUYDataCollectionsBlock)block; - (NSURLSessionDataTask *)getCollections:(BUYDataCollectionsBlock)block;
/** /**
* Fetches collections based off page
*
* @param page Index of the page requested
* @param block (^BUYDataCollectionsBlock)(NSArray *collections, NSError *error)
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block;
/**
* Fetches the products in the given collection with the collection's * Fetches the products in the given collection with the collection's
* default sort order set in the shop's admin * default sort order set in the shop's admin
* *
......
...@@ -180,15 +180,23 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_publi ...@@ -180,15 +180,23 @@ static NSString *const kBUYClientPathCollectionPublications = @"collection_publi
- (NSURLSessionDataTask *)getCollections:(BUYDataCollectionsBlock)block - (NSURLSessionDataTask *)getCollections:(BUYDataCollectionsBlock)block
{ {
NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathCollectionPublications queryItems:nil]; return [self getCollectionsPage:1 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {
block(collections, error);
}];
}
- (NSURLSessionDataTask *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block
{
NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathCollectionPublications
queryItems:@{@"limit" : [NSString stringWithFormat:@"%lu", (unsigned long)self.pageSize],
@"page" : [NSString stringWithFormat:@"%lu", (unsigned long)page]}];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSArray *collections = nil; NSArray *collections = nil;
if (json && error == nil) { if (json && error == nil) {
collections = [BUYCollection convertJSONArray:json[kBUYClientPathCollectionPublications]]; collections = [BUYCollection convertJSONArray:json[kBUYClientPathCollectionPublications]];
} }
block(collections, error); block(collections, page, [self hasReachedEndOfPage:collections], error);
}]; }];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment