From 94b84abb8f1d04c6048ca9343256c65d6247bdbb Mon Sep 17 00:00:00 2001 From: Stephano Telolahy Date: Mon, 6 Jan 2025 08:57:41 +0100 Subject: [PATCH] wip --- .../Misc/CreateGameStoreWithSideEffects.swift | 17 ----- .../Tests/Misc/GameStateBuilder+Cards.swift | 7 +- .../Cards/Tests/Misc/GameStore+Dispatch.swift | 67 +++++++++---------- .../Data/Cards/Tests/SimulationTest.swift | 3 +- 4 files changed, 39 insertions(+), 55 deletions(-) delete mode 100644 WildWestOnline/Data/Cards/Tests/Misc/CreateGameStoreWithSideEffects.swift diff --git a/WildWestOnline/Data/Cards/Tests/Misc/CreateGameStoreWithSideEffects.swift b/WildWestOnline/Data/Cards/Tests/Misc/CreateGameStoreWithSideEffects.swift deleted file mode 100644 index 43b8bf993..000000000 --- a/WildWestOnline/Data/Cards/Tests/Misc/CreateGameStoreWithSideEffects.swift +++ /dev/null @@ -1,17 +0,0 @@ -// -// CreateGameStoreWithSideEffects.swift -// WildWestOnline -// -// Created by Hugues Stephano TELOLAHY on 06/01/2025. -// - -import Redux -import GameCore - -@MainActor func createGameStoreWithSideEffects(initialState: GameState) -> Store { - .init( - initialState: initialState, - reducer: gameReducer, - dependencies: () - ) -} diff --git a/WildWestOnline/Data/Cards/Tests/Misc/GameStateBuilder+Cards.swift b/WildWestOnline/Data/Cards/Tests/Misc/GameStateBuilder+Cards.swift index 8dda0dea4..562064633 100644 --- a/WildWestOnline/Data/Cards/Tests/Misc/GameStateBuilder+Cards.swift +++ b/WildWestOnline/Data/Cards/Tests/Misc/GameStateBuilder+Cards.swift @@ -9,15 +9,16 @@ import CardsData extension GameState { static func makeBuilderWithAllCards() -> Builder { - makeBuilder().withCards(Cards.all) + makeBuilder() + .withCards(Cards.all) } } extension GameState.Builder { func withDummyCards(_ names: [String]) -> Self { - let extraCards = names.reduce(into: [String: Card]()) { partialResult, element in + let dummyCards = names.reduce(into: [String: Card]()) { partialResult, element in partialResult[element] = .init(name: element) } - return withCards(extraCards) + return withCards(dummyCards) } } diff --git a/WildWestOnline/Data/Cards/Tests/Misc/GameStore+Dispatch.swift b/WildWestOnline/Data/Cards/Tests/Misc/GameStore+Dispatch.swift index 51856a7e1..0a5e26a04 100644 --- a/WildWestOnline/Data/Cards/Tests/Misc/GameStore+Dispatch.swift +++ b/WildWestOnline/Data/Cards/Tests/Misc/GameStore+Dispatch.swift @@ -17,47 +17,45 @@ func dispatchUntilCompleted( file: StaticString = #file, line: UInt = #line ) async throws -> [GameAction] { - let expectation = XCTestExpectation(description: "Game idle") - - let store = Store( - initial: state, - reducer: GameReducer().reduce, - middlewares: [ - Middlewares.updateGame, - Middlewares.performChoices(.init(choices: expectedChoices)) - ] - ) { - expectation.fulfill() + let sut = await createGameStoreWithSideEffects(initialState: state) + var receivedActions: [Action] = [] + var receivedErrors: [Error] = [] + var cancellables: Set = [] + await MainActor.run { + sut.eventPublisher + .sink { receivedActions.append($0) } + .store(in: &cancellables) + sut.errorPublisher + .sink { receivedErrors.append($0) } + .store(in: &cancellables) } - var subscriptions: Set = [] - var ocurredEvents: [GameAction] = [] - var ocurredError: Error? + // When + await sut.dispatch(action) - store.eventPublisher.sink { event in - if let gameAction = event as? GameAction, - gameAction.isRenderable { - ocurredEvents.append(gameAction) - } + // Then + guard receivedErrors.isEmpty else { + throw receivedErrors[0] } - .store(in: &subscriptions) - store.errorPublisher.sink { error in - ocurredError = error + let gameActions = receivedActions.compactMap { action in + if let action = action as? GameAction, + action.isRenderable { + return action + } else { + return nil + } } - .store(in: &subscriptions) - - store.dispatch(action) - let waiter = XCTWaiter() - await waiter.fulfillment(of: [expectation]) - subscriptions.removeAll() - - if let ocurredError { - throw ocurredError - } + return gameActions +} - return ocurredEvents +@MainActor private func createGameStoreWithSideEffects(initialState: GameState) -> Store { + .init( + initialState: initialState, + reducer: gameReducer, + dependencies: () + ) } struct Choice { @@ -72,7 +70,7 @@ private class ChoicesWrapper { self.choices = choices } } - +/* private extension Middlewares { static func performChoices(_ expectedChoicesWrapper: ChoicesWrapper) -> Middleware { { state, _ in @@ -94,3 +92,4 @@ private extension Middlewares { } } } +*/ diff --git a/WildWestOnline/Data/Cards/Tests/SimulationTest.swift b/WildWestOnline/Data/Cards/Tests/SimulationTest.swift index 8a8a72dcd..7c9e7eeb3 100644 --- a/WildWestOnline/Data/Cards/Tests/SimulationTest.swift +++ b/WildWestOnline/Data/Cards/Tests/SimulationTest.swift @@ -10,7 +10,7 @@ import XCTest import Redux import GameCore import CardsData - +/* struct SimulationTest { @Test func simulate2PlayersGame_shouldComplete() async throws { try await simulateGame(playersCount: 3) @@ -76,3 +76,4 @@ private class StateWrapper: @unchecked Sendable { self.value = value } } +*/