From 1601642c041beb03833b477d619429050ce53788 Mon Sep 17 00:00:00 2001 From: Steve Streza Date: Tue, 29 Oct 2024 22:38:30 -0700 Subject: [PATCH] Fix test --- .../PPOViewModelTests.swift | 40 +++++++++++++------ KsApi/MockService.swift | 5 ++- Library/PaginatingList.swift | 6 ++- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/Kickstarter-iOS/Features/PledgedProjectsOverview/PPOViewModelTests.swift b/Kickstarter-iOS/Features/PledgedProjectsOverview/PPOViewModelTests.swift index fec368f849..05738d6b20 100644 --- a/Kickstarter-iOS/Features/PledgedProjectsOverview/PPOViewModelTests.swift +++ b/Kickstarter-iOS/Features/PledgedProjectsOverview/PPOViewModelTests.swift @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) @@ -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) @@ -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) diff --git a/KsApi/MockService.swift b/KsApi/MockService.swift index d45d75baa2..eff153caf4 100644 --- a/KsApi/MockService.swift +++ b/KsApi/MockService.swift @@ -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) diff --git a/Library/PaginatingList.swift b/Library/PaginatingList.swift index ab97f84ec8..1ab850737e 100644 --- a/Library/PaginatingList.swift +++ b/Library/PaginatingList.swift @@ -2,7 +2,11 @@ import Foundation import SwiftUI /// A List wrapper that handles pagination and refreshing -public struct PaginatingList: View where Data: Identifiable, Data: Hashable, Cell: View, Header: View { +public struct PaginatingList: View where + Data: Identifiable, + Data: Hashable, + Cell: View, + Header: View { var data: [Data] = [] var canLoadMore: Bool var selectedItem: Binding?