Skip to content

Commit

Permalink
Add trySwiftTests (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
takehilo committed Mar 26, 2024
1 parent 9816020 commit a3fb997
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 1 deletion.
7 changes: 7 additions & 0 deletions App/App/App.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
"identifier" : "SponsorFeatureTests",
"name" : "SponsorFeatureTests"
}
},
{
"target" : {
"containerPath" : "container:..\/MyLibrary",
"identifier" : "trySwiftFeatureTests",
"name" : "trySwiftFeatureTests"
}
}
],
"version" : 1
Expand Down
8 changes: 8 additions & 0 deletions MyLibrary/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,13 @@ let package = Package(
.product(name: "ComposableArchitecture", package: "swift-composable-architecture"),
]
),
.testTarget(
name: "trySwiftFeatureTests",
dependencies: [
"trySwiftFeature",
"SharedModels",
.product(name: "ComposableArchitecture", package: "swift-composable-architecture"),
]
),
]
)
9 changes: 9 additions & 0 deletions MyLibrary/Sources/trySwiftFeature/Organizers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ public struct Organizers {
public struct State: Equatable {
var organizers = IdentifiedArrayOf<Organizer>()
@Presents var destination: Destination.State?

public init(
organizers: IdentifiedArrayOf<Organizer> = [],
destination: Destination.State? = nil
) {
self.organizers = organizers
self.destination = destination
}
}

public enum Action: ViewAction {
Expand All @@ -21,6 +29,7 @@ public struct Organizers {
case _organizerTapped(Organizer)
}

@CasePathable
public enum Delegate {
case organizerTapped(Organizer)
}
Expand Down
8 changes: 7 additions & 1 deletion MyLibrary/Sources/trySwiftFeature/trySwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ public struct TrySwift {
@ObservableState
public struct State: Equatable {
var path = StackState<Path.State>()
public init() {}

public init(
path: StackState<Path.State> = .init()
) {
self.path = path
}
}

public enum Action: BindableAction, ViewAction {
case path(StackAction<Path.State, Path.Action>)
case binding(BindingAction<State>)
case view(View)

@CasePathable
public enum View {
case organizerTapped
case codeOfConductTapped
Expand Down
30 changes: 30 additions & 0 deletions MyLibrary/Tests/trySwiftFeatureTests/Mocks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Foundation
import SharedModels

extension Organizer {
static let alice = Self(
id: 1,
name: "alice",
imageName: "Alice",
bio: "",
links: [
Link(
name: "@alice",
url: URL(string: "https://example.com/alice")!
)
]
)

static let bob = Self(
id: 2,
name: "bob",
imageName: "Bob",
bio: "",
links: [
Link(
name: "@bob",
url: URL(string: "https://example.com/bob")!
)
]
)
}
143 changes: 143 additions & 0 deletions MyLibrary/Tests/trySwiftFeatureTests/trySwiftTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import ComposableArchitecture
import SharedModels
import DependencyExtra
import XCTest

@testable import trySwiftFeature

final class trySwiftTests: XCTestCase {
@MainActor
func testOrganizerTapped() async {
let store = TestStore(initialState: TrySwift.State()) {
TrySwift()
}

await store.send(\.view.organizerTapped) {
$0.path[id: 0] = .organizers(.init())
}
}

@MainActor
func testCodeOfConductTapped() async {
let receivedUrl = ActorIsolated<URL?>(nil)

let store = TestStore(initialState: TrySwift.State()) {
TrySwift()
} withDependencies: {
$0.safari = { @Sendable in
SafariEffect { url in
await receivedUrl.withValue {
$0 = url
return true
}
}
}()
}

await store.send(\.view.codeOfConductTapped)
await receivedUrl.withValue {
XCTAssertEqual($0, URL(string: "https://tryswift.jp/code-of-conduct_en")!)
}
}

@MainActor
func testPrivacyPolicyTapped() async {
let receivedUrl = ActorIsolated<URL?>(nil)

let store = TestStore(initialState: TrySwift.State()) {
TrySwift()
} withDependencies: {
$0.safari = { @Sendable in
SafariEffect { url in
await receivedUrl.withValue {
$0 = url
return true
}
}
}()
}

await store.send(\.view.privacyPolicyTapped)
await receivedUrl.withValue {
XCTAssertEqual($0, URL(string: "https://tryswift.jp/privacy-policy_en")!)
}
}

@MainActor
func testAcknowledgementsTapped() async {
let store = TestStore(initialState: TrySwift.State()) {
TrySwift()
}

await store.send(\.view.acknowledgementsTapped) {
$0.path[id: 0] = .acknowledgements(.init())
}
}

@MainActor
func testEventBriteTapped() async {
let receivedUrl = ActorIsolated<URL?>(nil)

let store = TestStore(initialState: TrySwift.State()) {
TrySwift()
} withDependencies: {
$0.safari = { @Sendable in
SafariEffect { url in
await receivedUrl.withValue {
$0 = url
return true
}
}
}()
}

await store.send(\.view.eventbriteTapped)
await receivedUrl.withValue {
XCTAssertEqual($0, URL(string: "https://www.eventbrite.com/e/try-swift-tokyo-2024-tickets-712565200697")!)
}
}

@MainActor
func testWebsiteTapped() async {
let receivedUrl = ActorIsolated<URL?>(nil)

let store = TestStore(initialState: TrySwift.State()) {
TrySwift()
} withDependencies: {
$0.safari = { @Sendable in
SafariEffect { url in
await receivedUrl.withValue {
$0 = url
return true
}
}
}()
}

await store.send(\.view.websiteTapped)
await receivedUrl.withValue {
XCTAssertEqual($0, URL(string: "https://tryswift.jp/_en")!)
}
}

@MainActor
func testProfileNavigation() async {
let store = TestStore(
initialState: TrySwift.State(
path: StackState([
.organizers(
Organizers.State(
organizers: .init(arrayLiteral: .alice, .bob)
)
)
])
)
) {
TrySwift()
}

await store.send(\.path[id: 0].organizers.delegate.organizerTapped, .alice) {
$0.path[id: 1] = .profile(.init(organizer: .alice))
}
}
}

0 comments on commit a3fb997

Please sign in to comment.