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

[MBL-1495] [MBL-1502] PPO UI implementation #2186

Merged
merged 19 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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()
stevestreza-ksr marked this conversation as resolved.
Show resolved Hide resolved

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