Skip to content

Commit

Permalink
Merge pull request #514 from adevinta/refacto/tag/snapshot
Browse files Browse the repository at this point in the history
[Snapshot-1544] Update the Tag snapshot
  • Loading branch information
robergro authored Oct 17, 2023
2 parents baa5ba6 + f896762 commit 0bc9a80
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 142 deletions.
196 changes: 196 additions & 0 deletions core/Sources/Components/Tag/View/Common/TagScenarioSnapshotTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
//
// TagScenarioSnapshotTestsTests.swift
// SparkCoreSnapshotTests
//
// Created by robin.lemaire on 10/10/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

@testable import SparkCore
import UIKit
import SwiftUI

enum TagScenarioSnapshotTests: String, CaseIterable {
case test1
case test2
case test3
case test4
case test5

// MARK: - Type Alias

typealias Constants = ComponentSnapshotTestConstants

// MARK: - Configurations

func configuration(isSwiftUIComponent: Bool) -> [TagConfigurationSnapshotTests] {
switch self {
case .test1:
return self.test1(isSwiftUIComponent: isSwiftUIComponent)
case .test2:
return self.test2(isSwiftUIComponent: isSwiftUIComponent)
case .test3:
return self.test3(isSwiftUIComponent: isSwiftUIComponent)
case .test4:
return self.test4(isSwiftUIComponent: isSwiftUIComponent)
case .test5:
return self.test5(isSwiftUIComponent: isSwiftUIComponent)
}
}

// MARK: - Scenarios

/// Test 1
///
/// Description: To test all intents
///
/// Content:
/// - intents: all
/// - variant: tinted
/// - content: icon + text
/// - mode: all
/// - size: default
private func test1(isSwiftUIComponent: Bool) -> [TagConfigurationSnapshotTests] {
let intents = TagIntent.allCases

return intents.map {
.init(
scenario: self,
intent: $0,
variant: .tinted,
iconImage: .mock(isSwiftUIComponent: isSwiftUIComponent),
text: "Text",
isLongText: false,
modes: Constants.Modes.all,
sizes: Constants.Sizes.default
)
}
}

/// Test 2
///
/// Description: To test all variants
///
/// Content:
/// - intent: main
/// - variant: all
/// - content: text only
/// - mode: all
/// - size: default
private func test2(isSwiftUIComponent: Bool) -> [TagConfigurationSnapshotTests] {
let variants = TagVariant.allCases

return variants.map {
.init(
scenario: self,
intent: .main,
variant: $0,
iconImage: nil,
text: "Text",
isLongText: false,
modes: Constants.Modes.all,
sizes: Constants.Sizes.default
)
}
}

/// Test 3
///
/// Description: To test all color for filled variant
///
/// Content:
/// - intents: all
/// - variant: filled
/// - content: icon + text
/// - mode: default
/// - size: default
private func test3(isSwiftUIComponent: Bool) -> [TagConfigurationSnapshotTests] {
let intents = TagIntent.allCases

return intents.map {
.init(
scenario: self,
intent: $0,
variant: .filled,
iconImage: .mock(isSwiftUIComponent: isSwiftUIComponent),
text: "Text",
isLongText: false,
modes: Constants.Modes.default,
sizes: Constants.Sizes.default
)
}
}

/// Test 4
///
/// Description: To test content resilience
///
/// Content:
/// - intent: neutral
/// - variant: tinted
/// - content: all (icon only / long text/ long text + icon)
/// - mode: default
/// - size: default
private func test4(isSwiftUIComponent: Bool) -> [TagConfigurationSnapshotTests] {
let contents: [(isIconImage: Bool, isLongText: Bool)] = [
(isIconImage: true, isLongText: false), // Only Icon
(isIconImage: false, isLongText: true), // Only long text
(isIconImage: true, isLongText: true) // Icon + Long text
]

return contents.map {
.init(
scenario: self,
intent: .neutral,
variant: .tinted,
iconImage: $0.isIconImage ? .mock(isSwiftUIComponent: isSwiftUIComponent) : nil,
text: $0.isLongText ? "Very very long long text" : nil,
isLongText: $0.isLongText,
modes: Constants.Modes.default,
sizes: Constants.Sizes.default
)
}
}

/// Test 6
///
/// Description: To test a11y sizes
///
/// Content:
/// - intent: main
/// - variant: tinted
/// - content: icon + text
/// - mode: default
/// - size: all
private func test5(isSwiftUIComponent: Bool) -> [TagConfigurationSnapshotTests] {
return [
.init(
scenario: self,
intent: .main,
variant: .tinted,
iconImage: .mock(isSwiftUIComponent: isSwiftUIComponent),
text: "Text",
isLongText: false,
modes: Constants.Modes.default,
sizes: Constants.Sizes.all
)
]
}
}

// MARK: - Private Extensions

private extension ImageEither {

static func mock(isSwiftUIComponent: Bool) -> Self {
return isSwiftUIComponent ? .right(Image.mock) : .left(UIImage.mock)
}
}

private extension Image {
static let mock = Image(systemName: "person.2.circle.fill")
}

private extension UIImage {
static var mock = UIImage(systemName: "person.2.circle.fill") ?? UIImage()
}
45 changes: 20 additions & 25 deletions core/Sources/Components/Tag/View/Common/TagSutSnapshotTests.swift
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
//
// TagSutSnapshotTests.swift
// TagConfigurationSnapshotTests.swift
// SparkCoreTests
//
// Created by robin.lemaire on 05/05/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

import UIKit
@testable import SparkCore

struct TagSutSnapshotTests {
struct TagConfigurationSnapshotTests {

// MARK: - Properties

let scenario: TagScenarioSnapshotTests

let intent: TagIntent
let variant: TagVariant

// MARK: - Getter

func testName(on function: String = #function) -> String {
return "\(function)-\(self.intent)-\(self.variant)"
let iconImage: ImageEither?
let text: String?
let isLongText: Bool
var width: CGFloat? {
return self.isLongText ? 100 : nil
}
let modes: [ComponentSnapshotTestMode]
let sizes: [UIContentSizeCategory]

// MARK: - Cases

static var allCases: [Self] {
return Self.allVariantCases(for: .alert) +
Self.allVariantCases(for: .danger) +
Self.allVariantCases(for: .info) +
Self.allVariantCases(for: .neutral) +
Self.allVariantCases(for: .main) +
Self.allVariantCases(for: .support) +
Self.allVariantCases(for: .success) +
Self.allVariantCases(for: .accent) +
Self.allVariantCases(for: .basic)
}
// MARK: - Getter

private static func allVariantCases(for intent: TagIntent) -> [Self] {
func testName() -> String {
return [
.init(intent: intent, variant: .filled),
.init(intent: intent, variant: .outlined),
.init(intent: intent, variant: .tinted)
]
"\(self.scenario.rawValue)",
"\(self.intent)",
"\(self.variant)",
self.iconImage != nil ? "withImage" : "withoutImage",
self.text != nil ? (self.isLongText ? "longText" : "normalText") : "withoutText"
].joined(separator: "-")
}
}
69 changes: 21 additions & 48 deletions core/Sources/Components/Tag/View/SwiftUI/TagViewSnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,30 @@ final class TagViewSnapshotTests: SwiftUIComponentSnapshotTestCase {
// MARK: - Properties

private let theme: Theme = SparkTheme.shared
private var iconImage = Image(systemName: "person.2.circle.fill")
private let text = "Text"

// MARK: - Tests

func test_swiftUI_tag_with_only_image_for_all_intent_and_variant() throws {
let suts = TagSutSnapshotTests.allCases
for sut in suts {
let view = TagView(theme: self.theme)
.intent(sut.intent)
.variant(sut.variant)
.iconImage(self.iconImage)
.fixedSize()

self.assertSnapshotInDarkAndLight(
matching: view,
testName: sut.testName()
)
}
}

func test_swiftUI_tag_with_only_text_for_all_intent_and_variant() {
let suts = TagSutSnapshotTests.allCases
for sut in suts {
let view = TagView(theme: self.theme)
.intent(sut.intent)
.variant(sut.variant)
.text(self.text)
.fixedSize()

self.assertSnapshotInDarkAndLight(
matching: view,
testName: sut.testName()
)
}
}

func test_swiftUI_tag_with_image_and_text_for_all_intent_and_variant() throws {
let suts = TagSutSnapshotTests.allCases
for sut in suts {
let view = TagView(theme: self.theme)
.intent(sut.intent)
.variant(sut.variant)
.iconImage(self.iconImage)
.text(self.text)
.fixedSize()

self.assertSnapshotInDarkAndLight(
matching: view,
testName: sut.testName()
)
func test() {
let scenarios = TagScenarioSnapshotTests.allCases

for scenario in scenarios {
let configurations = scenario.configuration(isSwiftUIComponent: true)
for configuration in configurations {
let view = TagView(theme: self.theme)
.intent(configuration.intent)
.variant(configuration.variant)
.iconImage(configuration.iconImage?.rightValue)
.text(configuration.text)
.frame(width: configuration.width)
.fixedSize()

self.assertSnapshot(
matching: view,
modes: configuration.modes,
sizes: configuration.sizes,
testName: configuration.testName()
)
}
}
}
}
Loading

0 comments on commit 0bc9a80

Please sign in to comment.