diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/FloatingTextField.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/FloatingTextField.xcscheme new file mode 100644 index 0000000..ed7189a --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/FloatingTextField.xcscheme @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..d2dfc75 --- /dev/null +++ b/Package.resolved @@ -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 +} diff --git a/Package.swift b/Package.swift index 434fcd4..5e617fd 100644 --- a/Package.swift +++ b/Package.swift @@ -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"), + ] + ), ] ) diff --git a/Tests/FloatingTextFieldTests/FloatingTextFieldTests.swift b/Tests/FloatingTextFieldTests/FloatingTextFieldTests.swift deleted file mode 100644 index d7d7821..0000000 --- a/Tests/FloatingTextFieldTests/FloatingTextFieldTests.swift +++ /dev/null @@ -1,8 +0,0 @@ -// -// floating-text-field -// Copyright © 2023 Space Code. All rights reserved. -// - -import XCTest - -final class FloatingTextFieldTests: XCTestCase {} diff --git a/Tests/FloatingTextFieldTests/SnapshotTests/FloatingTextFieldSnapshotTests.swift b/Tests/FloatingTextFieldTests/SnapshotTests/FloatingTextFieldSnapshotTests.swift new file mode 100644 index 0000000..e712fb4 --- /dev/null +++ b/Tests/FloatingTextFieldTests/SnapshotTests/FloatingTextFieldSnapshotTests.swift @@ -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 = "name@gmail.com" + 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) +} diff --git a/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsCompactedPlaceholder_whenTextIsNotEmpty.1.png b/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsCompactedPlaceholder_whenTextIsNotEmpty.1.png new file mode 100644 index 0000000..4ff805d Binary files /dev/null and b/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsCompactedPlaceholder_whenTextIsNotEmpty.1.png differ diff --git a/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsCustomizedView.1.png b/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsCustomizedView.1.png new file mode 100644 index 0000000..9be8f5b Binary files /dev/null and b/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsCustomizedView.1.png differ diff --git a/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsOnlyPlaceholder_whenTextIsEmpty.1.png b/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsOnlyPlaceholder_whenTextIsEmpty.1.png new file mode 100644 index 0000000..beaf829 Binary files /dev/null and b/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsOnlyPlaceholder_whenTextIsEmpty.1.png differ diff --git a/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsSecureField.1.png b/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsSecureField.1.png new file mode 100644 index 0000000..fa44173 Binary files /dev/null and b/Tests/FloatingTextFieldTests/SnapshotTests/__Snapshots__/FloatingTextFieldSnapshotTests/test_thatFloatingTextFieldShowsSecureField.1.png differ diff --git a/Tests/FloatingTextFieldTests/UnitTests/FloatingTextFieldTests.swift b/Tests/FloatingTextFieldTests/UnitTests/FloatingTextFieldTests.swift new file mode 100644 index 0000000..cbbe0fc --- /dev/null +++ b/Tests/FloatingTextFieldTests/UnitTests/FloatingTextFieldTests.swift @@ -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" +}