-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathShopCreateViewModelTest.swift
More file actions
237 lines (191 loc) · 7.5 KB
/
Copy pathShopCreateViewModelTest.swift
File metadata and controls
237 lines (191 loc) · 7.5 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// ShopCreateViewModelTest.swift
// NativeAppTemplate
//
import Foundation
@testable import NativeAppTemplate
import Testing
@MainActor
@Suite
struct ShopCreateViewModelTest {
let sessionController = TestSessionController()
let shopRepository = TestShopRepository(
shopsService: ShopsService()
)
let messageBus = MessageBus()
@Test
func stateIsInitiallyNotLoading() {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
#expect(viewModel.isCreating == false)
}
@Test
func maximumNameLength() {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
#expect(viewModel.maximumNameLength == 100)
}
@Test
func maximumDescriptionLength() {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
#expect(viewModel.maximumDescriptionLength == 1_000)
}
@Test("Name validation", arguments: [
("", true), // blank → invalid
("a", false), // 1 char → valid
("Shop Name 1", false), // normal → valid
(String(repeating: "a", count: 100), false), // exactly 100 → valid
(String(repeating: "a", count: 101), true) // 101 → invalid
])
func nameValidation(name: String, shouldBeInvalid: Bool) {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
viewModel.name = name
#expect(viewModel.hasInvalidDataName == shouldBeInvalid)
}
@Test("Description validation", arguments: [
("", false), // empty → valid
("Short note.", false), // short → valid
(String(repeating: "x", count: 1000), false), // exactly 1000 → valid
(String(repeating: "x", count: 1001), true) // 1001 → invalid
])
func descriptionValidation(description: String, shouldBeInvalid: Bool) {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
viewModel.description = description
#expect(viewModel.hasInvalidDataDescription == shouldBeInvalid)
}
@Test
func validateNameLengthTruncatesCorrectly() {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
viewModel.name = String(repeating: "a", count: 100) + "EXTRA"
viewModel.validateNameLength()
#expect(viewModel.name == String(repeating: "a", count: 100))
}
@Test
func validateDescriptionLengthTruncatesCorrectly() {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
viewModel.description = String(repeating: "x", count: 1500)
viewModel.validateDescriptionLength()
#expect(viewModel.description.count == 1_000)
}
@Test
func createShop() async throws {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
let createdShopsCount = shopRepository.createdShopsCount
let newName = "New Shop Name"
let newTimeZone = "Osaka"
let newDescription = "New Shop Description"
viewModel.name = newName
viewModel.selectedTimeZone = newTimeZone
viewModel.description = newDescription
// https://stackoverflow.com/a/75618551/1160200
let createShopTask = Task {
viewModel.createShop()
}
await createShopTask.value
let latestShop = try #require(shopRepository.shops.last)
let message = Strings.shopCreated
#expect(viewModel.messageBus.currentMessage?.message == message)
#expect(viewModel.isCreating)
#expect(latestShop.name == newName)
#expect(latestShop.timeZone == newTimeZone)
#expect(latestShop.description == newDescription)
#expect(shopRepository.shops.count == createdShopsCount + 1)
#expect(viewModel.shouldDismiss)
}
@Test
func createShopFailed() async {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
let createdShopsCount = shopRepository.createdShopsCount
let newName = "New Shop Name"
let newTimeZone = "Osaka"
let newDescription = "New Shop Description"
viewModel.name = newName
viewModel.selectedTimeZone = newTimeZone
viewModel.description = newDescription
let message = "You can create up to 99 shops across all organizations."
let httpResponseCode = 422
shopRepository.error = NativeAppTemplateAPIError.requestFailed(nil, 422, message)
// https://stackoverflow.com/a/75618551/1160200
let createShopTask = Task {
viewModel.createShop()
}
await createShopTask.value
#expect(viewModel.messageBus.currentMessage?.message == "[NATIVEAPPTEMPLATE-2001] \(message) [Status: \(httpResponseCode)]")
#expect(viewModel.isCreating)
#expect(shopRepository.shops.count == createdShopsCount)
#expect(viewModel.shouldDismiss)
}
@Test
func createShopFailedNot422() async {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
let createdShopsCount = shopRepository.createdShopsCount
let newName = "New Shop Name"
let newTimeZone = "Osaka"
let newDescription = "New Shop Description"
viewModel.name = newName
viewModel.selectedTimeZone = newTimeZone
viewModel.description = newDescription
let message = "Internal server error."
let httpResponseCode = 500
shopRepository.error = NativeAppTemplateAPIError.requestFailed(nil, httpResponseCode, message)
// https://stackoverflow.com/a/75618551/1160200
let createShopTask = Task {
viewModel.createShop()
}
await createShopTask.value
#expect(viewModel.messageBus.currentMessage?.message == "[NATIVEAPPTEMPLATE-2001] \(message) [Status: \(httpResponseCode)]")
#expect(viewModel.isCreating)
#expect(shopRepository.shops.count == createdShopsCount)
#expect(viewModel.shouldDismiss == false)
}
@Test
func initialValues() {
let viewModel = ShopCreateViewModel(
sessionController: sessionController,
shopRepository: shopRepository,
messageBus: messageBus
)
#expect(viewModel.name == "")
#expect(viewModel.description == "")
#expect(viewModel.selectedTimeZone == Utility.currentTimeZone())
#expect(viewModel.shouldDismiss == false)
}
}