Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the package access modifier and update the store's interface #101

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Sources/OneWay/OneWay.docc/Articles/Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ Before using the `expect` function, make sure to import the **OneWayTesting** mo
import OneWayTesting
```

When testing a reducer, you need to use `Store` instead of `ViewStore` for the `expect` function to be available.

```swift
let sut = Store(
reducer: TestReducer(),
state: TestReducer.State(count: 0)
)
await sut.send(.increment)
await sut.expect(\.count, 1)
```

The completion of `send`'s `await` literally means that `send` has finished. It does not mean that the state has fully changed. The state change always happpens asynchronously. Therefore, tests should be written using the `expect` function.

#### When using Testing

You can use the `expect` function to easily check the state value.
Expand All @@ -35,7 +48,7 @@ func test_incrementTwice() async {
await sut.send(.increment)
await sut.send(.increment)

await sut.xctExpect(\.count, 2)
await sut.expect(\.count, 2)
}
```

Expand Down
7 changes: 6 additions & 1 deletion Sources/OneWay/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ where R.Action: Sendable, R.State: Sendable & Equatable {
/// state changes.
public var states: AsyncStream<State>

package var isIdle: Bool { !isProcessing && tasks.isEmpty }
/// Returns `true` if the store is idle, meaning it's not processing and there are no pending
/// tasks for side effects.
public var isIdle: Bool {
!isProcessing && tasks.isEmpty
}

private let reducer: any Reducer<Action, State>
private let continuation: AsyncStream<State>.Continuation
private var isProcessing: Bool = false
Expand Down
2 changes: 1 addition & 1 deletion Sources/OneWay/ViewStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where R.Action: Sendable, R.State: Sendable & Equatable {
/// state changes
public let states: AsyncViewStateSequence<State>

package let store: Store<R>
private let store: Store<R>
private let continuation: AsyncStream<State>.Continuation
private var task: Task<Void, Never>?

Expand Down
162 changes: 0 additions & 162 deletions Sources/OneWayTesting/ViewStore+Testing.swift

This file was deleted.

31 changes: 16 additions & 15 deletions Tests/OneWayTestingTests/TestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
// Copyright (c) 2022-2024 SeungYeop Yeom ( https://github.com/DevYeom ).
//

#if canImport(Testing)
#if canImport(Testing) && canImport(Darwin)
import Darwin
import Testing
import OneWay

Expand Down Expand Up @@ -38,33 +39,28 @@ struct TestingTests {
await store.expect(\.nested.doubleNested.value, 1.23)
}

#if !os(Linux)
@Test
func viewStoreExpect() async {
let store = await ViewStore(
func storeExpectWithManyActions() async {
let store = Store(
reducer: TestReducer(),
state: TestReducer.State(count: 0)
)
await store.expect(\.count, 0)

await store.send(.increment)
await store.expect(\.count, 1)

await store.send(.increment)
await store.expect(\.count, 2)

await store.send(.setName("hello"))
await store.expect(\.nested.name, "hello")
for _ in 0..<10_000 {
await store.send(.increment)
}
await store.expect(\.count, 10_000)

await store.send(.setValue(1.23))
await store.expect(\.nested.doubleNested.value, 1.23)
await store.send(.delayedIncrement)
await store.expect(\.count, 10_001)
}
#endif
}

private struct TestReducer: Reducer {
enum Action: Sendable {
case increment
case delayedIncrement
case setName(String)
case setValue(Double)
}
Expand All @@ -86,6 +82,11 @@ private struct TestReducer: Reducer {
case .increment:
state.count += 1
return .none
case .delayedIncrement:
return .single {
try! await Task.sleep(nanoseconds: NSEC_PER_MSEC * 100)
return .increment
}
case let .setName(name):
state.nested.name = name
return .none
Expand Down
31 changes: 17 additions & 14 deletions Tests/OneWayTestingTests/XCTestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Copyright (c) 2022-2024 SeungYeop Yeom ( https://github.com/DevYeom ).
//

#if canImport(XCTest) && canImport(Darwin)
import Darwin
import XCTest
import OneWay

Expand Down Expand Up @@ -35,32 +37,27 @@ final class XCTestTests: XCTestCase {
await store.expect(\.nested.doubleNested.value, 1.23)
}

#if !os(Linux)
func test_viewStoreExpect() async {
let store = await ViewStore(
func test_storeExpectWithManyActions() async {
let store = Store(
reducer: TestReducer(),
state: TestReducer.State(count: 0)
)
await store.expect(\.count, 0)

await store.send(.increment)
await store.expect(\.count, 1)

await store.send(.increment)
await store.expect(\.count, 2)

await store.send(.setName("hello"))
await store.expect(\.nested.name, "hello")
for _ in 0..<10_000 {
await store.send(.increment)
}
await store.expect(\.count, 10_000)

await store.send(.setValue(1.23))
await store.expect(\.nested.doubleNested.value, 1.23)
await store.send(.delayedIncrement)
await store.expect(\.count, 10_001)
}
#endif
}

private struct TestReducer: Reducer {
enum Action: Sendable {
case increment
case delayedIncrement
case setName(String)
case setValue(Double)
}
Expand All @@ -82,6 +79,11 @@ private struct TestReducer: Reducer {
case .increment:
state.count += 1
return .none
case .delayedIncrement:
return .single {
try! await Task.sleep(nanoseconds: NSEC_PER_MSEC * 100)
return .increment
}
case let .setName(name):
state.nested.name = name
return .none
Expand All @@ -91,3 +93,4 @@ private struct TestReducer: Reducer {
}
}
}
#endif
10 changes: 9 additions & 1 deletion Tests/OneWayTests/ViewStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ final class ViewStoreTests: XCTestCase {
sut.send(.increment)
sut.send(.twice)

await sut.expect(\.count, 4)
var result: [Int] = []
for await state in sut.states {
result.append(state.count)
if result.count > 4 {
break
}
}

XCTAssertEqual(result, [0, 1, 2, 3, 4])
}

@MainActor
Expand Down