From e0d5b6957120d20b1123bb95c35821385b015654 Mon Sep 17 00:00:00 2001 From: vixer93 Date: Fri, 26 Jul 2024 19:52:33 +0900 Subject: [PATCH] Add ContributorFeatureTests --- .../ContributorReducer.swift | 10 +++- .../ContributorFeatureTests.swift | 57 +++++++++++++++++-- .../TimetableDetailTests.swift | 1 + 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/app-ios/Sources/ContributorFeature/ContributorReducer.swift b/app-ios/Sources/ContributorFeature/ContributorReducer.swift index bede8a50e..4391b7910 100644 --- a/app-ios/Sources/ContributorFeature/ContributorReducer.swift +++ b/app-ios/Sources/ContributorFeature/ContributorReducer.swift @@ -38,9 +38,13 @@ public struct ContributorReducer: Sendable { switch action { case .onAppear: return .run { send in - try await contributorClient.refresh() - for try await contributors in try contributorClient.streamContributors() { - await send(.response(.success(contributors))) + do { + try await contributorClient.refresh() + for try await contributors in try contributorClient.streamContributors() { + await send(.response(.success(contributors))) + } + } catch { + await send(.response(.failure(error))) } } .cancellable(id: CancelID.request) diff --git a/app-ios/Tests/ContributorFeatureTests/ContributorFeatureTests.swift b/app-ios/Tests/ContributorFeatureTests/ContributorFeatureTests.swift index 2ff79c124..b380a3ea1 100644 --- a/app-ios/Tests/ContributorFeatureTests/ContributorFeatureTests.swift +++ b/app-ios/Tests/ContributorFeatureTests/ContributorFeatureTests.swift @@ -1,17 +1,66 @@ import XCTest import ComposableArchitecture +import Model +@preconcurrency import shared @testable import ContributorFeature final class ContributorFeatureTests: XCTestCase { @MainActor - func testExample() async throws { - let store = TestStore(initialState: ContributorReducer.State(text: "HOGE")) { + func testOnAppear() async throws { + let contributors = Contributor.companion.fakes() + let store = TestStore(initialState: ContributorReducer.State()) { ContributorReducer() + } withDependencies: { + $0.contributorClient.refresh = {} + $0.contributorClient.streamContributors = { + AsyncThrowingStream { + $0.yield(contributors) + $0.finish() + } + } } - await store.send(.onAppear) { - $0.text = "Contributor Feature" + + await store.send(.onAppear) + await store.receive(\.response) { + $0.contributors = contributors + } + } + + @MainActor + func testOnAppearFail() async throws { + let contributors = Contributor.companion.fakes() + let store = TestStore(initialState: ContributorReducer.State()) { + ContributorReducer() + } withDependencies: { + $0.contributorClient.refresh = { + throw ContributorTestError.fail + } + $0.contributorClient.streamContributors = { + AsyncThrowingStream { + $0.yield(contributors) + $0.finish() + } + } + } + + await store.send(.onAppear) + await store.receive(\.response) + } + + @MainActor + func testContributorButtonTapped() async throws { + let url = URL(string: "https://github.com/DroidKaigi/conference-app-2023")! + let store = TestStore(initialState: ContributorReducer.State()) { + ContributorReducer() + } + + await store.send(.view(.contributorButtonTapped(url))) { + $0.url = IdentifiableURL(url) } } +} +enum ContributorTestError: Error { + case fail } diff --git a/app-ios/Tests/TimetableDetailFeatureTests/TimetableDetailTests.swift b/app-ios/Tests/TimetableDetailFeatureTests/TimetableDetailTests.swift index 1b4a99d6d..71b695b25 100644 --- a/app-ios/Tests/TimetableDetailFeatureTests/TimetableDetailTests.swift +++ b/app-ios/Tests/TimetableDetailFeatureTests/TimetableDetailTests.swift @@ -1,6 +1,7 @@ import XCTest import ComposableArchitecture import shared +import Model @testable import TimetableDetailFeature final class TimetableDetail_iosTests: XCTestCase {