Skip to content

Commit

Permalink
Merge pull request #583 from adevinta/snapshots/component/chip-576
Browse files Browse the repository at this point in the history
[Chip#576] Updated snapshots for chip.
  • Loading branch information
michael-zimmermann authored Oct 27, 2023
2 parents da1fe9d + 0ef87bb commit 1b5e557
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/Sources/Common/Enum/Either/Type/ViewEither.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// ViewEither.swift
// SparkCoreSnapshotTests
//
// Created by michael.zimmermann on 26.10.23.
// Copyright © 2023 Adevinta. All rights reserved.
//

import UIKit
import SwiftUI

typealias ViewEither = Either<UIView, AnyView>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// ChipConfigurationSnapshotTests.swift
// SparkCoreSnapshotTests
//
// Created by michael.zimmermann on 26.10.23.
// Copyright © 2023 Adevinta. All rights reserved.
//

import UIKit
@testable import SparkCore

struct ChipConfigurationSnapshotTests {

// MARK: - Properties

let scenario: ChipScenarioSnapshotTests

let intent: ChipIntent
let variant: ChipVariant
let icon: ImageEither?
let text: String?
let badge: ViewEither?
let state: ChipState

let modes: [ComponentSnapshotTestMode]
let sizes: [UIContentSizeCategory]

// MARK: - Getter

func testName() -> String {
return [
"\(self.scenario.rawValue)",
"\(self.intent)",
"\(self.variant)",
self.icon != nil ? "withImage" : "withoutImage",
self.text != nil ? "withText" : "withoutText",
self.badge != nil ? "withBadge" : "withoutBadge",
self.state.isDisabled ? "disabled" : "enabled",
self.state.isSelected ? "selected" : "notSelected"
].joined(separator: "-")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
//
// ChipScenarioSnapshotTests.swift
// Spark
//
// Created by michael.zimmermann on 26.10.23.
// Copyright © 2023 Adevinta. All rights reserved.
//

@testable import SparkCore
import UIKit
import SwiftUI

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

// MARK: - Type Alias

typealias Constants = ComponentSnapshotTestConstants

// MARK: - Configurations

func configuration(isSwiftUIComponent: Bool) -> [ChipConfigurationSnapshotTests] {
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: outlined
/// - content: icon + text
/// - state: default
/// - mode: all
/// - size: default
private func test1(isSwiftUIComponent: Bool) -> [ChipConfigurationSnapshotTests] {
let intents = ChipIntent.allCases

return intents.map {
.init(
scenario: self,
intent: $0,
variant: .outlined,
icon: .mock(isSwiftUIComponent: isSwiftUIComponent),
text: "Label",
badge: nil,
state: .default,
modes: Constants.Modes.all,
sizes: Constants.Sizes.default
)
}
}

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

return variants.map {
.init(
scenario: self,
intent: .basic,
variant: $0,
icon: nil,
text: "Label",
badge: nil,
state: .default,
modes: Constants.Modes.all,
sizes: Constants.Sizes.default
)
}
}

/// Test 3
///
/// Description: To test all states
///
/// Content:
/// - intents: all
/// - variant: all
/// - content: icon + text
/// - state: all
/// - mode: default
/// - size: default
private func test3(isSwiftUIComponent: Bool) -> [ChipConfigurationSnapshotTests] {
let variants = ChipVariant.allCases
let states = ChipState.all

return all(variants, states).map { variant, state in
.init(
scenario: self,
intent: .main,
variant: variant,
icon: .mock(isSwiftUIComponent: isSwiftUIComponent),
text: "Label",
badge: nil,
state: state,
modes: Constants.Modes.default,
sizes: Constants.Sizes.default
)
}
}

/// Test 4
///
/// Description: To test content resilience
///
/// Content:
/// - intent: neutral
/// - variant: tinted
/// - content: text + icon + in different combinations
/// - mode: default
/// - size: default
private func test4(isSwiftUIComponent: Bool) -> [ChipConfigurationSnapshotTests] {
let contents: [(hasIcon: Bool, hasText: Bool, hasBadge: Bool)] =
[
(true, false, false),
(true, false, true),
(false, true, true),
(true, true, true)
]

return contents.map { content in
.init(
scenario: self,
intent: .neutral,
variant: .tinted,
icon: content.hasIcon ? .mock(isSwiftUIComponent: isSwiftUIComponent) : nil,
text: content.hasText ? "A Very Long Label" : nil,
badge: content.hasBadge ? .mock(isSwiftUIComponent) : nil,
state: .default,
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) -> [ChipConfigurationSnapshotTests] {
return [
.init(
scenario: self,
intent: .accent,
variant: .tinted,
icon: .mock(isSwiftUIComponent: isSwiftUIComponent),
text: "Label",
badge: nil,
state: .default,
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 ViewEither {
static func mock(_ isSwiftUIComponent: Bool) -> Self {
if isSwiftUIComponent {
let view = BadgeView(
theme: SparkTheme.shared,
intent: .danger,
value: 99
).borderVisible(false)

return .right(AnyView(view))
} else {
let view = BadgeUIView(
theme: SparkTheme.shared,
intent: .danger,
value: 99,
isBorderVisible: false
)
return .left(view)
}
}
}

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()
}

private func all<U, V>(_ lhs: [U], _ rhs: [V]) -> [(U, V)] {
lhs.flatMap { left in
rhs.map { right in
(left, right)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ChipStateSnapshotTests.swift
// SparkCoreSnapshotTests
//
// Created by michael.zimmermann on 26.10.23.
// Copyright © 2023 Adevinta. All rights reserved.
//

import Foundation

@testable import SparkCore

extension ChipState {
static let all: [ChipState] =
[
.init(isEnabled: true, isPressed: false, isSelected: true),
.init(isEnabled: false, isPressed: false, isSelected: true),
.init(isEnabled: false, isPressed: false, isSelected: false),
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// ChipViewSnapshotTests.swift
// SparkCoreSnapshotTests
//
// Created by michael.zimmermann on 26.10.23.
// Copyright © 2023 Adevinta. All rights reserved.
//

import XCTest
import SwiftUI
import SnapshotTesting

@testable import SparkCore

final class ChipViewSnapshotTests: SwiftUIComponentSnapshotTestCase {

// MARK: - Properties

private let theme: Theme = SparkTheme.shared

// MARK: - Tests

func test() {
let scenarios = ChipScenarioSnapshotTests.allCases

for scenario in scenarios {
let configurations = scenario.configuration(isSwiftUIComponent: true)
for configuration in configurations {
let view = ChipView(
theme: self.theme,
intent: configuration.intent,
variant: configuration.variant,
icon: configuration.icon?.rightValue,
title: configuration.text)
.component(configuration.badge?.rightValue)
.fixedSize()

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

0 comments on commit 1b5e557

Please sign in to comment.