Files
swift-composable-architectu…/Tests/ComposableArchitectureTests/DependencyKeyWritingReducerTests.swift
Stephen Celis 57e804f1cc Macro bonanza (#2553)
* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Silence test warnings

* wip

* wip

* wip

* update a bunch of docs

* wip

* wip

* fix

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Kill integration tests for now

* wip

* wip

* wip

* wip

* updating docs for @Reducer macro

* replaced more Reducer protocols with @Reducer

* Fixed some broken docc references

* wip

* Some @Reducer docs

* more docs

* convert some old styles to new style

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* bump

* update tutorials to use body

* update tutorials to use DML on destination state enum

* Add diagnostic

* wip

* updated a few more tests

* wip

* wip

* Add another gotcha

* wip

* wip

* wip

* fixes

* wip

* wip

* wip

* wip

* wip

* fix

* wip

* remove for now

* wip

* wip

* updated some docs

* migration guides

* more migration guide

* fix ci

* fix

* soft deprecate all apis using AnyCasePath

* wip

* Fix

* fix tests

* swift-format 509 compatibility

* wip

* wip

* Update Sources/ComposableArchitecture/Macros.swift

Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com>

* wip

* wip

* update optional state case study

* remove initializer

* Don't use @State for BasicsView integration demo

* fix tests

* remove reduce diagnostics for now

* diagnose error not warning

* Update Sources/ComposableArchitecture/Macros.swift

Co-authored-by: Jesse Tipton <jesse@jessetipton.com>

* wip

* move integration tests to cron

* Revert "move integration tests to cron"

This reverts commit f9bdf2f04b.

* disable flakey tests on CI

* wip

* wip

* Revert "Revert "move integration tests to cron""

This reverts commit 66aafa7327.

* fix

* wip

* fix

---------

Co-authored-by: Brandon Williams <mbrandonw@hey.com>
Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com>
Co-authored-by: Brandon Williams <135203+mbrandonw@users.noreply.github.com>
Co-authored-by: Jesse Tipton <jesse@jessetipton.com>
2023-11-13 12:57:35 -08:00

150 lines
3.7 KiB
Swift

import ComposableArchitecture
import XCTest
@MainActor
final class DependencyKeyWritingReducerTests: BaseTCATestCase {
func testWritingFusion() async {
let reducer: _DependencyKeyWritingReducer<Feature> = Feature()
.dependency(\.myValue, 42)
.dependency(\.myValue, 1729)
.dependency(\.myValue, 1)
.dependency(\.myValue, 2)
.dependency(\.myValue, 3)
XCTAssertTrue((reducer as Any) is _DependencyKeyWritingReducer<Feature>)
}
func testTransformFusion() async {
let reducer: _DependencyKeyWritingReducer<Feature> = Feature()
.transformDependency(\.myValue) { $0 = 42 }
.transformDependency(\.myValue) { $0 = 1729 }
.transformDependency(\.myValue) { $0 = 1 }
.transformDependency(\.myValue) { $0 = 2 }
.transformDependency(\.myValue) { $0 = 3 }
XCTAssertTrue((reducer as Any) is _DependencyKeyWritingReducer<Feature>)
}
func testWritingFusionOrder() async {
let store = TestStore(initialState: Feature.State()) {
Feature()
.dependency(\.myValue, 42)
.dependency(\.myValue, 1729)
}
await store.send(.tap) {
$0.value = 42
}
}
func testTransformFusionOrder() async {
let store = TestStore(initialState: Feature.State()) {
Feature()
.transformDependency(\.myValue) { $0 = 42 }
.transformDependency(\.myValue) { $0 = 1729 }
}
await store.send(.tap) {
$0.value = 42
}
}
func testWritingOrder() async {
let store = TestStore(initialState: Feature.State()) {
CombineReducers {
Feature()
.dependency(\.myValue, 42)
}
.dependency(\.myValue, 1729)
}
await store.send(.tap) {
$0.value = 42
}
}
func testTransformOrder() async {
let store = TestStore(initialState: Feature.State()) {
CombineReducers {
Feature()
.transformDependency(\.myValue) { $0 = 42 }
}
.transformDependency(\.myValue) { $0 = 1729 }
}
await store.send(.tap) {
$0.value = 42
}
}
@Reducer
fileprivate struct Feature_testDependency_EffectOfEffect {
struct State: Equatable { var count = 0 }
enum Action: Equatable {
case tap
case response(Int)
case otherResponse(Int)
}
@Dependency(\.myValue) var myValue
var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case .tap:
state.count += 1
return .run { send in await send(.response(self.myValue)) }
case let .response(value):
state.count = value
return .run { send in await send(.otherResponse(self.myValue)) }
case let .otherResponse(value):
state.count = value
return .none
}
}
}
}
func testDependency_EffectOfEffect() async {
let store = TestStore(initialState: Feature_testDependency_EffectOfEffect.State()) {
Feature_testDependency_EffectOfEffect()
.dependency(\.myValue, 42)
}
await store.send(.tap) {
$0.count = 1
}
await store.receive(.response(42)) {
$0.count = 42
}
await store.receive(.otherResponse(42))
}
}
@Reducer
private struct Feature {
@Dependency(\.myValue) var myValue
struct State: Equatable { var value = 0 }
enum Action { case tap }
var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case .tap:
state.value = self.myValue
return .none
}
}
}
}
private enum MyValue: DependencyKey {
static let liveValue = 0
static let testValue = 0
}
extension DependencyValues {
var myValue: Int {
get { self[MyValue.self] }
set { self[MyValue.self] = newValue }
}
}