Skip to content

Commit

Permalink
[Tests-1544] Tag snapshot refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
robergro committed Oct 12, 2023
1 parent f52d30c commit defe050
Show file tree
Hide file tree
Showing 4 changed files with 336 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// TagSutSnapshotStrategy.swift
// SparkCoreSnapshotTests
//
// Created by robin.lemaire on 10/10/2023.
// Copyright © 2023 Adevinta. All rights reserved.
//

enum TagSutSnapshotStrategy: String, CaseIterable {
case test1
case test2
case test3
case test4
case test5
case test6
}
265 changes: 248 additions & 17 deletions core/Sources/Components/Tag/View/Common/TagSutSnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,271 @@
// Copyright © 2023 Adevinta. All rights reserved.
//

import UIKit
import SwiftUI
@testable import SparkCore

struct TagSutSnapshotTests {

// MARK: - Type Alias

typealias Constants = ComponentSnapshotTestConstants

// MARK: - Properties

let strategy: TagSutSnapshotStrategy

let intent: TagIntent
let variant: TagVariant
let iconImage: ImageEither?
let text: String?
private let isLongText: Bool
var width: CGFloat? {
return self.isLongText ? 100 : nil
}
let modes: [ComponentSnapshotTestMode]
let sizes: [UIContentSizeCategory]

// MARK: - Getter

func testName(on function: String = #function) -> String {
return "\(function)-\(self.intent)-\(self.variant)"
func testName() -> String {
return [
"\(self.strategy.rawValue)",
"\(self.intent)",
"\(self.variant)",
self.iconImage != nil ? "withImage" : "withoutImage",
self.isLongText ? "longText" : "normalText"
].joined(separator: "-")
}

// MARK: - Testing Strategy

static func test(
for strategy: TagSutSnapshotStrategy,
isSwiftUIComponent: Bool
) -> [TagSutSnapshotTests] {
switch strategy {
case .test1:
return self.test1(
for: strategy,
isSwiftUIComponent: isSwiftUIComponent
)
case .test2:
return self.test2(
for: strategy,
isSwiftUIComponent: isSwiftUIComponent
)
case .test3:
return self.test3(
for: strategy,
isSwiftUIComponent: isSwiftUIComponent
)
case .test4:
return self.test4(
for: strategy,
isSwiftUIComponent: isSwiftUIComponent
)
case .test5:
return self.test5(
for: strategy,
isSwiftUIComponent: isSwiftUIComponent
)
case .test6:
return self.test6(
for: strategy,
isSwiftUIComponent: isSwiftUIComponent
)
}
}

// MARK: - Cases
/// Test 1
///
/// Description: To test all intents
///
/// Content:
/// - intents: all
/// - variant : tinted
/// - content : icon + text
/// - theme : light / dark
/// - size : default
static func test1(
for strategy: TagSutSnapshotStrategy,
isSwiftUIComponent: Bool
) -> [TagSutSnapshotTests] {
let intents = TagIntent.allCases

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)
return intents.map {
.init(
strategy: strategy,
intent: $0,
variant: .tinted,
iconImage: .mock(isSwiftUIComponent: isSwiftUIComponent),
text: "Text",
isLongText: false,
modes: Constants.modes,
sizes: [Constants.size]
)
}
}

private static func allVariantCases(for intent: TagIntent) -> [Self] {
/// Test 2
///
/// Description: To test all variants
///
/// Content:
/// - intent: main
/// - variant : all
/// - content : text only
/// - theme : light / dark
/// - size : default
static func test2(
for strategy: TagSutSnapshotStrategy,
isSwiftUIComponent: Bool
) -> [TagSutSnapshotTests] {
let variants = TagVariant.allCases

return variants.map {
.init(
strategy: strategy,
intent: .main,
variant: $0,
iconImage: nil,
text: "Text",
isLongText: false,
modes: Constants.modes,
sizes: [Constants.size]
)
}
}

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

return intents.map {
.init(
strategy: strategy,
intent: $0,
variant: .filled,
iconImage: .mock(isSwiftUIComponent: isSwiftUIComponent),
text: "Text",
isLongText: false,
modes: [Constants.mode],
sizes: [Constants.size]
)
}
}

/// Test 4
///
/// Description: To test content resilience
///
/// Content:
/// - intent: neutral
/// - variant : tinted
/// - content : icon only
/// - theme : default
/// - size : default
static func test4(
for strategy: TagSutSnapshotStrategy,
isSwiftUIComponent: Bool
) -> [TagSutSnapshotTests] {
return [
.init(intent: intent, variant: .filled),
.init(intent: intent, variant: .outlined),
.init(intent: intent, variant: .tinted)
.init(
strategy: strategy,
intent: .neutral,
variant: .tinted,
iconImage: .mock(isSwiftUIComponent: isSwiftUIComponent),
text: nil,
isLongText: false,
modes: [Constants.mode],
sizes: [Constants.size]
)
]
}

/// Test 5
///
/// Description: To test content resilience
///
/// Content:
/// - intent: neutral
/// - variant : tinted
/// - content : long text
/// - theme : default
/// - size : default
static func test5(
for strategy: TagSutSnapshotStrategy,
isSwiftUIComponent: Bool
) -> [TagSutSnapshotTests] {
return [
.init(
strategy: strategy,
intent: .neutral,
variant: .tinted,
iconImage: nil,
text: "Very very very long long text",
isLongText: true,
modes: [Constants.mode],
sizes: [Constants.size]
)
]
}

/// Test 6
///
/// Description: To test a11y sizes
///
/// Content:
/// - intent: main
/// - variant : tinted
/// - content : icon + text
/// - theme : default
/// - size : xs, medium, xxxl
static func test6(
for strategy: TagSutSnapshotStrategy,
isSwiftUIComponent: Bool
) -> [TagSutSnapshotTests] {
return [
.init(
strategy: strategy,
intent: .main,
variant: .tinted,
iconImage: .mock(isSwiftUIComponent: isSwiftUIComponent),
text: "Text",
isLongText: false,
modes: [Constants.mode],
sizes: Constants.sizes
)
]
}
}

// 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()
}
68 changes: 22 additions & 46 deletions core/Sources/Components/Tag/View/SwiftUI/TagViewSnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,33 @@ 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()
func test() {
let strategies = TagSutSnapshotStrategy.allCases

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()
for strategy in strategies {
let suts = TagSutSnapshotTests.test(
for: strategy,
isSwiftUIComponent: true
)
for sut in suts {
let view = TagView(theme: self.theme)
.intent(sut.intent)
.variant(sut.variant)
.iconImage(sut.iconImage?.rightValue)
.text(sut.text)
.frame(width: sut.width)
.fixedSize()

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

0 comments on commit defe050

Please sign in to comment.