Skip to content

Commit

Permalink
Fix flaky OffsetPagination test (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iron-Ham authored and gh-action-runner committed Aug 21, 2024
1 parent 8b9de50 commit b15d910
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions Tests/ApolloPaginationTests/OffsetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,38 +89,43 @@ final class OffsetTests: XCTestCase {
}

let pager = await createPager()
var results: [ViewModel]?
let cancellable = pager.map { value in

let fetchExpectation = expectation(description: "Initial Fetch")
fetchExpectation.assertForOverFulfill = false
let subscriptionExpectation = expectation(description: "Subscription")
subscriptionExpectation.expectedFulfillmentCount = 2
var expectedViewModels: [ViewModel] = []
let subscriber = pager.compactMap { value in
switch value {
case .success(let output):
let friends = output.allData.flatMap { data in
data.hero.friends.map { friend in
ViewModel(name: friend.name)
}
}
return Result<[ViewModel], any Error>.success(friends)
return friends
case .failure(let error):
return .failure(error)
}
}.sink { result in
switch result {
case .success((let viewModels)):
results = viewModels
default:
XCTFail("Failed to get view models from pager.")
XCTFail(error.localizedDescription)
return nil
}
}.sink { viewModels in
expectedViewModels = viewModels
fetchExpectation.fulfill()
subscriptionExpectation.fulfill()
}

await fetchFirstPage(pager: pager)
XCTAssertEqual(results?.count, 2)
XCTAssertEqual(results?.map(\.name), ["Luke Skywalker", "Han Solo"])
await fulfillment(of: [fetchExpectation], timeout: 1)
XCTAssertEqual(expectedViewModels.count, 2)
XCTAssertEqual(expectedViewModels.map(\.name), ["Luke Skywalker", "Han Solo"])
let canLoadNext = await pager.canLoadNext
XCTAssertTrue(canLoadNext)

try await fetchSecondPage(pager: pager)
XCTAssertEqual(results?.count, 3)
XCTAssertEqual(results?.map(\.name), ["Luke Skywalker", "Han Solo", "Leia Organa"])
cancellable.cancel()
await fulfillment(of: [subscriptionExpectation], timeout: 1)
XCTAssertEqual(expectedViewModels.count, 3)
XCTAssertEqual(expectedViewModels.map(\.name), ["Luke Skywalker", "Han Solo", "Leia Organa"])
subscriber.cancel()
}

}

0 comments on commit b15d910

Please sign in to comment.