I have an issue with my project and I'm not sure if there is a workaround or I need to change strategy.
The project is using TCA and I've setup snapshot tests for some of the screen. A lot of screens have multiple states, such as loading, error, and populated. I preview these states using PreviewProviders
, creating a preview of the screen with different stores.
struct PetsScreenLoading_Previews: PreviewProvider {
static var previews: some View {
PetsScreen(
store: Store(
initialState: PetsState(
loadingStage: .loading
),
reducer: petsReducer,
environment: .noop()
)
)
.previewDisplayName("Loading")
}
}
struct PetsScreenError_Previews: PreviewProvider {
static var previews: some View {
PetsScreen(
store: Store(
initialState: PetsState(
hasError: true
),
reducer: petsReducer,
environment: .noop()
)
)
.previewDisplayName("Error")
}
}
Along with viewing the previews while developing, I would like to test each of these states. For each of these previews, I'm taking a snapshot:
func testLoadingSnapshot() {
assertSnapshots(views: PetsScreenLoading_Previews.previews, named: "PetsScreenLoadingSnapshot")
}
func testErrorSnapshot() {
assertSnapshots(views: PetsScreenError_Previews.previews, named: "PetsScreenErrorSnapshot")
}
The issue I'm running into is that the loading process is triggered by an .onAppear
action that is fired when the screen appears.
.onAppear {
viewStore.send(.loadPets)
}
This results in the screen reverting to a loading state for each snapshot.
Is there something simple I'm missing, or is this a fundamentally flawed setup? I don't know if there is a way to stop the .onAppear triggering. I can't find a way to stop the action from having an effect, other than adding something hacky like an isTesting
property.