diff --git a/Sources/ComposableArchitecture/Store.swift b/Sources/ComposableArchitecture/Store.swift index 590686fe8184..ebdc1b26bb57 100644 --- a/Sources/ComposableArchitecture/Store.swift +++ b/Sources/ComposableArchitecture/Store.swift @@ -166,11 +166,13 @@ public final class Store { /// /// - Parameters: /// - initialState: The state to start the application in. + /// - initialAction: An action to send the store when it is created. /// - reducer: The reducer that powers the business logic of the application. /// - prepareDependencies: A closure that can be used to override dependencies that will be accessed /// by the reducer. public convenience init>( initialState: @autoclosure () -> R.State, + initialAction: @autoclosure () -> R.Action? = nil, @ReducerBuilder reducer: () -> R, withDependencies prepareDependencies: ((inout DependencyValues) -> Void)? = nil ) { @@ -182,6 +184,9 @@ public final class Store { initialState: initialState, reducer: reducer.dependency(\.self, dependencies) ) + if let initialAction = initialAction() { + send(initialAction) + } } init() { diff --git a/Tests/ComposableArchitectureTests/StoreTests.swift b/Tests/ComposableArchitectureTests/StoreTests.swift index 0f52e5b90bb5..4b6f25a68ec3 100644 --- a/Tests/ComposableArchitectureTests/StoreTests.swift +++ b/Tests/ComposableArchitectureTests/StoreTests.swift @@ -1213,6 +1213,17 @@ final class StoreTests: BaseTCATestCase { } } } + + @MainActor + func testInitialAction() async { + let store = Store(initialState: 0, initialAction: ()) { + Reduce { state, _ in + state += 1 + return .none + } + } + XCTAssertEqual(store.currentState, 1) + } } #if canImport(Testing)