-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleSnapshotTests.swift
More file actions
125 lines (108 loc) · 3.31 KB
/
ExampleSnapshotTests.swift
File metadata and controls
125 lines (108 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// ExampleSnapshotTests.swift
// Copyright © 2022 Cameron Cooke. All rights reserved.
//
import SnapshotTesting
import SwiftUI
import XCTest
@testable import ParameterizedTesting
final class ExampleSnapshotTests: ParameterizedTestCase3<Weather, Int, Theme, Void> {
// MARK: - Internal -
override class func values() -> ([Weather], [Int], [Theme]) {
(
[.raining, .sunny, .cloudy, .snowing],
[12, 34, 3, 22, 0],
[.light, .dark]
)
}
override func testAllCombinations(
_ weather: Weather,
_ temperature: Int,
_ theme: Theme,
_ expectedResult: Void?
) {
let view = WeatherView(
viewModel: WeatherView.ViewModel(
weather: weather,
temperature: temperature,
theme: theme
)
)
#if os(iOS) || os(tvOS)
// Recorded on iPhone 14 (iOS 16.x)
assertSnapshot(
matching: view,
as: .image(
precision: 0.9,
perceptualPrecision: 0.98,
layout: .fixed(width: 400, height: 45)
),
testName: "\(weather)_\(temperature)_\(theme)_iOS"
)
#else
let viewController = NSHostingController(rootView: view)
XCTExpectFailure("Might fail as reference images were created on different OS environment")
assertSnapshot(
matching: viewController,
as: .image(
precision: 0.9,
perceptualPrecision: 0.98,
size: CGSize(width: 400, height: 45)
),
testName: "\(weather)_\(temperature)_\(theme)_macOS"
)
#endif
}
}
// MARK: Fakes
typealias Weather = WeatherView.ViewModel.Weather
typealias Theme = WeatherView.ViewModel.Theme
struct WeatherView: View {
struct ViewModel {
enum Weather: String, Hashable, CaseIterable {
case raining
case sunny
case cloudy
case snowing
}
enum Theme: Hashable, CaseIterable {
case light
case dark
}
let weather: Weather
let temperature: Int
let theme: Theme
var summary: String {
"It's \(weather.rawValue) and a \(adjective) \(temperature) degrees celsius"
}
private var adjective: String {
switch temperature {
case ...2: return "freezing"
case 3 ... 10: return "cold"
case 11 ... 19: return "mild"
case 20 ... 25: return "comfortable"
case 25...: return "hot"
default: return ""
}
}
var weatherEmoji: String {
switch weather {
case .raining: return "🌧️"
case .sunny: return "☀️"
case .cloudy: return "☁️"
case .snowing: return "🌨️"
}
}
}
let viewModel: ViewModel
var body: some View {
VStack {
Text(viewModel.weatherEmoji)
.font(.headline)
Spacer(minLength: 4)
Text(viewModel.summary)
.foregroundColor(viewModel.theme == Theme.light ? Color.black : Color.white)
.background(viewModel.theme == Theme.light ? Color.gray : Color.black)
}
}
}