Skip to content

Commit

Permalink
Implement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ns-vasilev committed Oct 17, 2023
1 parent 73795b2 commit 7d8741a
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 20 deletions.
103 changes: 103 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/FloatingTextField.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
version = "1.8">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FloatingTextField"
BuildableName = "FloatingTextField"
BlueprintName = "FloatingTextField"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FloatingTextFieldTests"
BuildableName = "FloatingTextFieldTests"
BlueprintName = "FloatingTextFieldTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES">
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FloatingTextField"
BuildableName = "FloatingTextField"
BlueprintName = "FloatingTextField"
ReferencedContainer = "container:">
</BuildableReference>
</CodeCoverageTargets>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FloatingTextFieldTests"
BuildableName = "FloatingTextFieldTests"
BlueprintName = "FloatingTextFieldTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FloatingTextField"
BuildableName = "FloatingTextField"
BlueprintName = "FloatingTextField"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
23 changes: 23 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"pins" : [
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "bb0ea08db8e73324fe6c3727f755ca41a23ff2f4",
"version" : "1.14.2"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "74203046135342e4a4a627476dd6caf8b28fe11b",
"version" : "509.0.0"
}
}
],
"version" : 2
}
26 changes: 14 additions & 12 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ import PackageDescription

let package = Package(
name: "FloatingTextField",
platforms: [
.iOS(.v15),
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "FloatingTextField",
targets: ["FloatingTextField"]),
.library(name: "FloatingTextField", targets: ["FloatingTextField"]),
],
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.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "FloatingTextField",
dependencies: []),
.target(name: "FloatingTextField", dependencies: []),
.testTarget(
name: "FloatingTextFieldTests",
dependencies: ["FloatingTextField"]),
dependencies: [
"FloatingTextField",
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
]
),
]
)
8 changes: 0 additions & 8 deletions Tests/FloatingTextFieldTests/FloatingTextFieldTests.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// floating-text-field
// Copyright © 2023 Space Code. All rights reserved.
//

@testable import FloatingTextField
import SnapshotTesting
import SwiftUI
import XCTest

// MARK: - FloatingTextFieldSnapshotTests

final class FloatingTextFieldSnapshotTests: XCTestCase {
// MARK: Tests

func test_thatFloatingTextFieldShowsOnlyPlaceholder_whenTextIsEmpty() {
assertSnapshots(
of: FloatingTextField(.constant(.empty), placeholder: .placeholder),
as: [.image(layout: .default, traits: .light)]
)
}

func test_thatFloatingTextFieldShowsCompactedPlaceholder_whenTextIsNotEmpty() {
assertSnapshots(
of: FloatingTextField(.constant(.text), placeholder: .placeholder),
as: [.image(layout: .default, traits: .light)]
)
}

func test_thatFloatingTextFieldShowsSecureField() {
assertSnapshots(
of: FloatingTextField(.constant(.text), placeholder: .placeholder)
.secureField(true),
as: [.image(layout: .default, traits: .light)]
)
}

func test_thatFloatingTextFieldShowsCustomizedView() {
assertSnapshots(
of: FloatingTextField(.constant(.text), placeholder: .placeholder)
.textFont(Font.system(size: 17.0))
.borderWidth(2.0)
.borderColor(.black)
.focusedBorderColor(.black)
.titleColor(Color.black)
.placeholderFont(Font.system(size: 14.0))
.placeholderColor(.gray)
.borderCornerRadius(12.0),
as: [.image(layout: .default, traits: .light)]
)
}
}

// MARK: - Constants

private extension String {
static let empty = ""
static let text = "[email protected]"
static let placeholder = "Username"
}

private extension SwiftUISnapshotLayout {
static let `default` = SwiftUISnapshotLayout.fixed(width: 375, height: 60)
}

private extension UITraitCollection {
static let light = UITraitCollection(userInterfaceStyle: .light)
}
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.
135 changes: 135 additions & 0 deletions Tests/FloatingTextFieldTests/UnitTests/FloatingTextFieldTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//
// floating-text-field
// Copyright © 2023 Space Code. All rights reserved.
//

@testable import FloatingTextField
import SwiftUI
import XCTest

// MARK: - FloatingTextFieldTests

final class FloatingTextFieldTests: XCTestCase {
// MARK: Properties

private var sut: FloatingTextField!

// MARK: XCTestCase

override func setUp() {
super.setUp()
sut = FloatingTextField(
.constant(.text),
placeholder: .placeholder
)
}

override func tearDown() {
sut = nil
super.tearDown()
}

// MARK: Tests

func test_thatFloatingTextFieldChangesForegroundColor_whenTitleColorDidSet() {
// when
_ = sut.titleColor(.red)

// then
XCTAssertEqual(sut.configuration.titleColor, .red)
}

func test_thatFloatingTextFieldChangesSecureEntry_whenSecureEntryDidSet() {
// when
_ = sut.secureField(true)

// then
XCTAssertTrue(sut.configuration.isSecureEntry)
}

func test_thatFloatingTextFieldChangesPlaceholderColor_whenPlaceholderColorDidSet() {
// when
_ = sut.placeholderColor(.red)

// then
XCTAssertEqual(sut.configuration.placeholderColor, .red)
}

func test_thatFloatingTextFieldChangesPlaceholderFont_whenPlaceholderFontDidSet() {
// given
let font = Font.system(size: 17.0)

// when
_ = sut.placeholderFont(font)

// then
XCTAssertEqual(sut.configuration.placeholderFont, font)
}

func test_thatFloatingTextFieldChangesFont_whenFontDidSet() {
// given
let font = Font.system(size: 17.0)

// when
_ = sut.textFont(font)

// then
XCTAssertEqual(sut.configuration.font, font)
}

func test_thatFloatingTextFieldChangesBorderWidth_whenBorderWidthDidSet() {
// given
let borderWidth: CGFloat = 1

// when
_ = sut.borderWidth(borderWidth)

// then
XCTAssertEqual(sut.configuration.borderWidth, borderWidth)
}

func test_thatFloatingTextFieldChangesBorderColor_whenBorderColorDidSet() {
// when
_ = sut.borderColor(.red)

// then
XCTAssertEqual(sut.configuration.borderColor, .red)
}

func test_thatFloatingTextFieldChangesFocusedBorderColor_whenFocusedBorderColorDidSet() {
// when
_ = sut.focusedBorderColor(.red)

// then
XCTAssertEqual(sut.configuration.focusedBorderColor, .red)
}

func test_thatFloatingTextFieldChangesBorderCornerRadius_whenFocusedBorderCornerRadiusDidSet() {
// given
let cornerRadius: CGFloat = 12

// when
_ = sut.borderCornerRadius(cornerRadius)

// then
XCTAssertEqual(sut.configuration.borderCornerRadius, cornerRadius)
}

func test_thatFloatingTextFieldChangesPlaceholderBottomPadding_whenPlaceholderBottomPaddingDidSet() {
// given
let padding: CGFloat = 12

// when
_ = sut.placeholderBottomPadding(padding)

// then
XCTAssertEqual(sut.configuration.placeholderBottomPadding, padding)
}
}

// MARK: - Constants

private extension String {
static let text = "text"
static let placeholder = "placeholder"
}

0 comments on commit 7d8741a

Please sign in to comment.