Skip to content

Commit

Permalink
Add SponsorFeatureTests (#50)
Browse files Browse the repository at this point in the history
* Add SponsorFeatureTests

* move Sponsors mocks to test module
  • Loading branch information
takehilo authored Mar 22, 2024
1 parent 1012fbe commit 4890b53
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
7 changes: 7 additions & 0 deletions App/App/App.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
"identifier" : "ScheduleFeatureTests",
"name" : "ScheduleFeatureTests"
}
},
{
"target" : {
"containerPath" : "container:..\/MyLibrary",
"identifier" : "SponsorFeatureTests",
"name" : "SponsorFeatureTests"
}
}
],
"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 @@ -96,5 +96,13 @@ let package = Package(
.product(name: "ComposableArchitecture", package: "swift-composable-architecture"),
]
),
.testTarget(
name: "SponsorFeatureTests",
dependencies: [
"SponsorFeature",
"SharedModels",
.product(name: "ComposableArchitecture", package: "swift-composable-architecture"),
]
),
]
)
3 changes: 1 addition & 2 deletions MyLibrary/Sources/SharedModels/Sponsors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public enum Plan: String, Codable, Sendable, CaseIterable {
}

public struct Sponsors: Codable, Equatable, Hashable, Sendable {
public var id: UUID { .init() }
let platinum: [Sponsor]
let gold: [Sponsor]
let silver: [Sponsor]
Expand All @@ -35,7 +34,7 @@ public struct Sponsors: Codable, Equatable, Hashable, Sendable {
]
}

init(
public init(
platinum: [Sponsor], gold: [Sponsor], silver: [Sponsor], bronze: [Sponsor],
diversity: [Sponsor], student: [Sponsor], community: [Sponsor], individual: [Sponsor]
) {
Expand Down
1 change: 1 addition & 0 deletions MyLibrary/Sources/SponsorFeature/Sponsors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public struct SponsorsList {
case binding(BindingAction<State>)
case view(View)

@CasePathable
public enum View {
case onAppear
case sponsorTapped(Sponsor)
Expand Down
31 changes: 31 additions & 0 deletions MyLibrary/Tests/SponsorFeatureTests/Mocks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation
import SharedModels

extension Sponsors {
static let mock = Self(
platinum: [.platinumMock],
gold: [.goldMock],
silver: [],
bronze: [],
diversity: [],
student: [],
community: [],
individual: []
)
}

extension Sponsor {
static let platinumMock = Self(
id: 1,
name: "platinaum sponsor",
imageName: "platinum_image",
link: URL(string: "https://example.com/platinum")!
)

static let goldMock = Self(
id: 2,
name: "gold sponsor",
imageName: "gold_image",
link: URL(string: "https://example.com/gold")!
)
}
45 changes: 45 additions & 0 deletions MyLibrary/Tests/SponsorFeatureTests/SponsorsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import ComposableArchitecture
import DataClient
import SharedModels
import DependencyExtra
import XCTest

@testable import SponsorFeature

final class SponsorsTests: XCTestCase {
@MainActor
func testOnAppear() async {
let store = TestStore(initialState: SponsorsList.State()) {
SponsorsList()
} withDependencies: {
$0[DataClient.self].fetchSponsors = { @Sendable in .mock }
}

await store.send(\.view.onAppear) {
$0.sponsors = .mock
}
}

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

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

await store.send(\.view.sponsorTapped, .platinumMock)
await receivedUrl.withValue {
XCTAssertEqual($0, Sponsor.platinumMock.link)
}
}
}

0 comments on commit 4890b53

Please sign in to comment.