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

Added Snapshot tests #10

Merged
merged 5 commits into from
Jun 15, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/Packages
/*.xcodeproj
xcuserdata/
/.swiftpm/xcode
Package.resolved
12 changes: 9 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ let package = Package(
targets: ["SegmentedPicker"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(
url: "https://github.com/pointfreeco/swift-snapshot-testing",
from: "1.12.0"
)

],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -26,6 +29,9 @@ let package = Package(
dependencies: []),
.testTarget(
name: "SegmentedPickerTests",
dependencies: ["SegmentedPicker"]),
dependencies: [
"SegmentedPicker",
.product(name: "SnapshotTesting", package: "swift-snapshot-testing")
]),
]
)
23 changes: 23 additions & 0 deletions Tests/SegmentedPickerTests/CapsuleSelectionViewTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// File.swift
//
//
// Created by Sergey Kazakov on 15/06/2024.
//

import SwiftUI
import XCTest
import SnapshotTesting
@testable import SegmentedPicker

final class CapsuleSelectionViewTests: XCTestCase {
func test_WhenSelectionIsNotProvided_SelectionIsNotVisible_Snapshot() {
let view = CapsuleSelectionView(titles: ["One", "Two", "Three"])
assertSnapshots(of: view, as: [.image(layout: .device(config: .iPhone13Pro))])
}

func test_WhenSelectionIsProvided_SelectionIsVisible_Snapshot() {
let view = CapsuleSelectionView(titles: ["One", "Two", "Three"], selectedIndex: 1)
assertSnapshots(of: view, as: [.image(layout: .device(config: .iPhone13Pro))])
}
}
36 changes: 36 additions & 0 deletions Tests/SegmentedPickerTests/TestViews/CapsuleSelectionView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// File.swift
//
//
// Created by Sergey Kazakov on 15/06/2024.
//

import SwiftUI
@testable import SegmentedPicker

struct CapsuleSelectionView: View {
let titles: [String]
@State var selectedIndex: Int?

var body: some View {
VStack {
SegmentedPicker(
titles,
selectedIndex: Binding(
get: { selectedIndex },
set: { selectedIndex = $0 }),
content: { item, isSelected in
Text(item)
.foregroundColor(isSelected ? Color.white : Color.gray )
.padding(.horizontal, 16)
.padding(.vertical, 8)
},
selection: {
Capsule()
.fill(Color.gray)
})
}

Spacer()
}
}
39 changes: 39 additions & 0 deletions Tests/SegmentedPickerTests/TestViews/UnderlineSelectionView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// File.swift
//
//
// Created by Sergey Kazakov on 15/06/2024.
//

import SwiftUI
@testable import SegmentedPicker

struct UnderlineSelectionView: View {
let titles: [String]
@State var selectedIndex: Int?

var body: some View {
VStack {
SegmentedPicker(
titles,
selectedIndex: Binding(
get: { selectedIndex },
set: { selectedIndex = $0 }),
selectionAlignment: .bottom,
content: { item, isSelected in
Text(item)

.foregroundColor(isSelected ? Color.black : Color.gray )
.padding(.horizontal, 16)
.padding(.vertical, 8)
},
selection: {
VStack(spacing: 0) {
Spacer()
Color.black.frame(height: 1)
}
})
Spacer()
}
}
}
23 changes: 23 additions & 0 deletions Tests/SegmentedPickerTests/UnderlineSelectionViewTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// File.swift
//
//
// Created by Sergey Kazakov on 15/06/2024.
//

import SwiftUI
import XCTest
import SnapshotTesting
@testable import SegmentedPicker

final class UnderlineSelectionViewTest: XCTestCase {
func test_WhenSelectionIsNotProvided_SelectionIsNotVisible_Snapshot() {
let view = UnderlineSelectionView(titles: ["One", "Two", "Three"])
assertSnapshots(of: view, as: [.image(layout: .device(config: .iPhone13Pro))])
}

func test_WhenSelectionIsProvided_SelectionIsVisible_Snapshot() {
let view = UnderlineSelectionView(titles: ["One", "Two", "Three"], selectedIndex: 1)
assertSnapshots(of: view, as: [.image(layout: .device(config: .iPhone13Pro))])
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.