Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky OffsetPagination test #465

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}

}
Loading