Skip to content

Commit

Permalink
Fix failure block getting called twice in PostCategoryService (#20201)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio authored Feb 24, 2023
2 parents f4f2d9a + 8768880 commit cf32db5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
15 changes: 10 additions & 5 deletions WordPress/Classes/Services/PostCategoryService.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ - (void)syncCategoriesForBlog:(Blog *)blog
id<TaxonomyServiceRemote> remote = [self remoteForBlog:blog];
NSManagedObjectID *blogID = blog.objectID;
[remote getCategoriesWithSuccess:^(NSArray *categories) {
NSError * __block error = nil;
[[ContextManager sharedInstance] performAndSaveUsingBlock:^(NSManagedObjectContext *context) {
Blog *blog = (Blog *)[context existingObjectWithID:blogID error:nil];
if (!blog) {
if (failure) {
failure([self serviceErrorNoBlog]);
}
error = [self serviceErrorNoBlog];
return;
}
[self mergeCategories:categories forBlog:blog inContext:context];
} completion: ^{
if (success) {
success();
if (error) {
if (failure) {
failure(error);
}
} else {
if (success) {
success();
}
}
} onQueue: dispatch_get_main_queue()];
} failure:failure];
Expand Down
26 changes: 26 additions & 0 deletions WordPress/WordPressTest/PostCategoryServiceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,30 @@ - (void)testThatCreateCategoryWithNameWorks
failure:^(NSError * _Nonnull error) {}];
}

- (void)testSyncSuccessShouldBeCalledOnce
{
TaxonomyServiceRemoteREST *remote = self.service.remoteForStubbing;

XCTestExpectation *completion = [self expectationWithDescription:@"Only the success block is called"];
OCMStub([remote getCategoriesWithSuccess:[OCMArg invokeBlock]
failure:[OCMArg isNotNil]]);
[self.service syncCategoriesForBlog:self.blog
success:^{ [completion fulfill]; }
failure:^(NSError * _Nonnull error) {[completion fulfill]; }];
[self waitForExpectations:@[completion] timeout:1];
}

- (void)testSyncFailureShouldBeCalledOnce
{
TaxonomyServiceRemoteREST *remote = self.service.remoteForStubbing;

XCTestExpectation *completion = [self expectationWithDescription:@"Only the failure block is called"];
OCMStub([remote getCategoriesWithSuccess:[OCMArg isNotNil]
failure:[OCMArg invokeBlock]]);
[self.service syncCategoriesForBlog:self.blog
success:^{ [completion fulfill]; }
failure:^(NSError * _Nonnull error) {[completion fulfill]; }];
[self waitForExpectations:@[completion] timeout:1];
}

@end

0 comments on commit cf32db5

Please sign in to comment.