Snapshot Tests - Mock Store #2135
-
Hi folks, I'm currently trying to add snapshot tests to one of my views. I'm having some issues with the effects that are triggered when the snapshot runs and the My current structure is the following View struct HomeView: View {
let store: StoreOf<HomeFeature>
@ObservedObject var viewStore: ViewStoreOf<HomeFeature>
init(store: StoreOf<HomePageFeature>) {
self.store = store
viewStore = ViewStore(store)
}
var body: some View {
Group {
if let error = viewStore.state.error {
ErrorView(...)
} else {
homeView
}
}
.onAppear {
viewStore.send(.onAppear)
}
.isLoading(viewStore.state.isLoading)
}
The Store struct HomeFeature: ReducerProtocol {
enum Action: Equatable {
case onAppear
...
...
}
struct State: Equatable {
var isLoading: Bool = false
var error: EquatableError?
}
var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
let effect: EffectTask<Action>
switch action {
case .onAppear:
state.isLoading = true
(... some long running effect e.g: network call)
}
}
}
I'm then trying to run a snapshot test for when the func testErrorState() {
let nsError = NSError(
domain: "test",
code: 0, userInfo: [
NSLocalizedDescriptionKey: "Error message"
]
)
let error = EquatableError(nsError)
let store = StoreOf<HomeFeature>(initialState: .init(error: error), reducer: HomeFeature())
let sut = HomePageView(store: mockedStore(error: error))
.frame(width: 390, height: 850)
assertViewSnapshot(sut)
} The issue, however, is that since Is there a way to make it so that either: • The Store I'm initializing the I've tried both using the Ha anyone faced a similar issue and was able to find a solution for it? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can create your store with no reducer to represent a completely inert store that does nothing when you send actions to it. That allows you to craft whatever you state you want and just hand it to the view. This is how it can look: let store = StoreOf<HomeFeature>(initialState: .init(error: error), reducer: EmptyReducer()) Or if you are using the newest version of TCA that allows specifying the reducer in a trailing builder closure: let store = StoreOf<HomeFeature>(initialState: .init(error: error)) {
} |
Beta Was this translation helpful? Give feedback.
You can create your store with no reducer to represent a completely inert store that does nothing when you send actions to it. That allows you to craft whatever you state you want and just hand it to the view.
This is how it can look:
Or if you are using the newest version of TCA that allows specifying the reducer in a trailing builder closure: