Files
swift-composable-architectu…/Tests/ComposableArchitectureTests/ReducerTests.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

117 lines
3.4 KiB
Swift

import Combine
@_spi(Internals) import ComposableArchitecture
import CustomDump
import XCTest
import os.signpost
@MainActor
final class ReducerTests: BaseTCATestCase {
var cancellables: Set<AnyCancellable> = []
func testCallableAsFunction() {
let reducer = Reduce<Int, Void> { state, _ in
state += 1
return .none
}
var state = 0
_ = reducer.reduce(into: &state, action: ())
XCTAssertEqual(state, 1)
}
#if (canImport(RegexBuilder) || !os(macOS) && !targetEnvironment(macCatalyst))
@Reducer
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
fileprivate struct Feature_testCombine_EffectsAreMerged {
typealias State = Int
enum Action { case increment }
@Dependency(\.continuousClock) var clock
let delay: Duration
let setValue: @Sendable () async -> Void
var body: some Reducer<State, Action> {
Reduce { state, action in
state += 1
return .run { _ in
try await self.clock.sleep(for: self.delay)
await self.setValue()
}
}
}
}
func testCombine_EffectsAreMerged() async throws {
if #available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) {
var fastValue: Int? = nil
var slowValue: Int? = nil
let clock = TestClock()
let store = TestStore(initialState: 0) {
Feature_testCombine_EffectsAreMerged(
delay: .seconds(1), setValue: { @MainActor in fastValue = 42 })
Feature_testCombine_EffectsAreMerged(
delay: .seconds(2), setValue: { @MainActor in slowValue = 1729 })
} withDependencies: {
$0.continuousClock = clock
}
await store.send(.increment) {
$0 = 2
}
// Waiting a second causes the fast effect to fire.
await clock.advance(by: .seconds(1))
try await Task.sleep(nanoseconds: NSEC_PER_SEC / 3)
XCTAssertEqual(fastValue, 42)
XCTAssertEqual(slowValue, nil)
// Waiting one more second causes the slow effect to fire. This proves that the effects
// are merged together, as opposed to concatenated.
await clock.advance(by: .seconds(1))
await store.finish()
XCTAssertEqual(fastValue, 42)
XCTAssertEqual(slowValue, 1729)
}
}
#endif
@Reducer
fileprivate struct Feature_testCombine {
typealias State = Int
enum Action { case increment }
let effect: @Sendable () async -> Void
var body: some Reducer<State, Action> {
Reduce { state, action in
state += 1
return .run { _ in
await self.effect()
}
}
}
}
func testCombine() async {
var first = false
var second = false
let store = TestStore(initialState: 0) {
Feature_testCombine(effect: { @MainActor in first = true })
Feature_testCombine(effect: { @MainActor in second = true })
}
await store
.send(.increment) { $0 = 2 }
.finish()
XCTAssertTrue(first)
XCTAssertTrue(second)
}
func testDefaultSignpost() async {
let reducer = EmptyReducer<Int, Void>().signpost(log: .default)
var n = 0
for await _ in reducer.reduce(into: &n, action: ()).actions {}
}
func testDisabledSignpost() async {
let reducer = EmptyReducer<Int, Void>().signpost(log: .disabled)
var n = 0
for await _ in reducer.reduce(into: &n, action: ()).actions {}
}
}