-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTestShopRepository.swift
More file actions
89 lines (73 loc) · 2.03 KB
/
Copy pathTestShopRepository.swift
File metadata and controls
89 lines (73 loc) · 2.03 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
//
// TestShopRepository.swift
// NativeAppTemplate
//
import Foundation
@testable import NativeAppTemplate
@MainActor
final class TestShopRepository: ShopRepositoryProtocol {
var shops: [Shop] = []
var state: DataState = .initial
var limitCount: Int = 0
var createdShopsCount: Int = 0
var isEmpty: Bool {
shops.isEmpty
}
/// A test-only
var error: NativeAppTemplateAPIError?
required init(shopsService: ShopsService) {
createdShopsCount = shops.count
}
func findBy(id: String) -> Shop {
guard let shop = shops.first(where: { $0.id == id }) else {
fatalError("Test setup error: Shop with id '\(id)' not found. Available IDs: \(shops.map(\.id))")
}
return shop
}
func reload() {
guard error == nil else {
state = .failed
return
}
state = .loading
createdShopsCount = shops.count
state = .hasData
}
func fetchDetail(id: String) async throws -> Shop {
guard error == nil else {
throw error!
}
guard let shop = shops.first(where: { $0.id == id }) else {
throw NativeAppTemplateAPIError.requestFailed(nil, 404, "Shop with id '\(id)' not found")
}
return shop
}
func create(shop: Shop) async throws -> Shop {
guard error == nil else {
throw error!
}
shops.append(shop)
createdShopsCount += 1
return shop
}
func update(id: String, shop: Shop) async throws -> Shop {
guard error == nil else {
throw error!
}
if let index = shops.firstIndex(where: { $0.id == id }) {
shops[index] = shop
}
return shop
}
func destroy(id: String) async throws {
guard error == nil else {
throw error!
}
shops.removeAll { $0.id == id }
}
/// A test-only
func setShops(shops: [Shop]) {
self.shops = shops
createdShopsCount = shops.count
}
}