Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
stevestreza-ksr committed Oct 30, 2024
1 parent d66ac8d commit 1601642
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,18 @@ class PPOViewModelTests: XCTestCase {
}

func testPullToRefresh_Once() async throws {
let expectation = XCTestExpectation(description: "Pull to refresh")
expectation.expectedFulfillmentCount = 5
let initialLoadExpectation = XCTestExpectation(description: "Initial load")
initialLoadExpectation.expectedFulfillmentCount = 3
let fullyLoadedExpectation = XCTestExpectation(description: "Pull to refresh")
fullyLoadedExpectation.expectedFulfillmentCount = 5

var values: [PPOViewModelPaginator.Results] = []

self.viewModel.$results
.sink { value in
values.append(value)
expectation.fulfill()
initialLoadExpectation.fulfill()
fullyLoadedExpectation.fulfill()
}
.store(in: &self.cancellables)

Expand All @@ -102,13 +105,15 @@ class PPOViewModelTests: XCTestCase {
self.viewModel.viewDidAppear() // Initial load
}

await fulfillment(of: [initialLoadExpectation], timeout: 0.1)

await withEnvironment(apiService: MockService(
fetchPledgedProjectsResult: Result.success(try self.pledgedProjectsData(cursors: 1...2))
)) { () async in
await self.viewModel.refresh() // Refresh
}

await fulfillment(of: [expectation], timeout: 0.1)
await fulfillment(of: [fullyLoadedExpectation], timeout: 0.1)

XCTAssertEqual(values.count, 5)

Expand All @@ -131,15 +136,18 @@ class PPOViewModelTests: XCTestCase {
}

func testPullToRefresh_Twice() async throws {
let expectation = XCTestExpectation(description: "Pull to refresh twice")
expectation.expectedFulfillmentCount = 7
let initialLoadExpectation = XCTestExpectation(description: "Initial load")
initialLoadExpectation.expectedFulfillmentCount = 3
let fullyLoadedExpectation = XCTestExpectation(description: "Pull to refresh twice")
fullyLoadedExpectation.expectedFulfillmentCount = 7

var values: [PPOViewModelPaginator.Results] = []

self.viewModel.$results
.sink { value in
values.append(value)
expectation.fulfill()
initialLoadExpectation.fulfill()
fullyLoadedExpectation.fulfill()
}
.store(in: &self.cancellables)

Expand All @@ -149,6 +157,8 @@ class PPOViewModelTests: XCTestCase {
self.viewModel.viewDidAppear() // Initial load
}

await fulfillment(of: [initialLoadExpectation], timeout: 0.1)

await withEnvironment(apiService: MockService(
fetchPledgedProjectsResult: Result.success(try self.pledgedProjectsData(cursors: 1...2))
)) { () async in
Expand All @@ -161,7 +171,7 @@ class PPOViewModelTests: XCTestCase {
await self.viewModel.refresh() // Refresh a second time
}

await fulfillment(of: [expectation], timeout: 0.1)
await fulfillment(of: [fullyLoadedExpectation], timeout: 0.1)

XCTAssertEqual(values.count, 7)

Expand Down Expand Up @@ -192,15 +202,18 @@ class PPOViewModelTests: XCTestCase {
}

func testLoadMore() async throws {
let expectation = XCTestExpectation(description: "Load more")
expectation.expectedFulfillmentCount = 5
let initialLoadExpectation = XCTestExpectation(description: "Initial load")
initialLoadExpectation.expectedFulfillmentCount = 3
let fullyLoadedExpectation = XCTestExpectation(description: "Load more")
fullyLoadedExpectation.expectedFulfillmentCount = 5

var values: [PPOViewModelPaginator.Results] = []

self.viewModel.$results
.sink { value in
values.append(value)
expectation.fulfill()
initialLoadExpectation.fulfill()
fullyLoadedExpectation.fulfill()
}
.store(in: &self.cancellables)

Expand All @@ -212,13 +225,16 @@ class PPOViewModelTests: XCTestCase {
)) {
self.viewModel.viewDidAppear() // Initial load
}

await fulfillment(of: [initialLoadExpectation], timeout: 0.1)

await withEnvironment(apiService: MockService(
fetchPledgedProjectsResult: Result.success(try self.pledgedProjectsData(cursors: 5...7))
)) { () async in
await self.viewModel.loadMore() // Load next page
}

await fulfillment(of: [expectation], timeout: 0.1)
await fulfillment(of: [fullyLoadedExpectation], timeout: 0.1)

XCTAssertEqual(values.count, 5)

Expand Down
5 changes: 4 additions & 1 deletion KsApi/MockService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,10 @@

switch response {
case let .success(pledgedProjectsData):
return Just(pledgedProjectsData).setFailureType(to: ErrorEnvelope.self).eraseToAnyPublisher()
return Just(pledgedProjectsData).setFailureType(to: ErrorEnvelope.self).delay(
for: 0.01,
scheduler: DispatchQueue.main
).eraseToAnyPublisher()

case let .failure(envelope):
return Fail(outputType: GraphAPI.FetchPledgedProjectsQuery.Data.self, failure: envelope)
Expand Down
6 changes: 5 additions & 1 deletion Library/PaginatingList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import Foundation
import SwiftUI

/// A List wrapper that handles pagination and refreshing
public struct PaginatingList<Data, Cell, Header>: View where Data: Identifiable, Data: Hashable, Cell: View, Header: View {
public struct PaginatingList<Data, Cell, Header>: View where
Data: Identifiable,
Data: Hashable,
Cell: View,
Header: View {
var data: [Data] = []
var canLoadMore: Bool
var selectedItem: Binding<Data?>?
Expand Down

0 comments on commit 1601642

Please sign in to comment.