Skip to content

Commit 5359a96

Browse files
committed
fix
1 parent 142e5d4 commit 5359a96

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
//
2+
// ShopListViewModelTest.swift
3+
// NativeAppTemplate
4+
//
5+
// Created by Claude on 2025/06/22.
6+
//
7+
8+
import Testing
9+
import Foundation
10+
@testable import NativeAppTemplate
11+
12+
@MainActor
13+
@Suite
14+
struct ShopListViewModelTest {
15+
var shops: [Shop] {
16+
[
17+
mockShop(id: "1", name: "Shop 1"),
18+
mockShop(id: "2", name: "Shop 2"),
19+
mockShop(id: "3", name: "Shop 3"),
20+
mockShop(id: "4", name: "Shop 4"),
21+
mockShop(id: "5", name: "Shop 5")
22+
]
23+
}
24+
25+
let sessionController = TestSessionController()
26+
let shopRepository = TestShopRepository(
27+
shopsService: ShopsService()
28+
)
29+
let itemTagRepository = TestItemTagRepository(
30+
itemTagsService: ItemTagsService()
31+
)
32+
let tabViewModel = TabViewModel()
33+
let mainTab = MainTab.shops
34+
35+
@Test
36+
func leftInShopSlots() {
37+
shopRepository.limitCount = 5
38+
shopRepository.createdShopsCount = 2
39+
40+
let viewModel = ShopListViewModel(
41+
sessionController: sessionController,
42+
shopRepository: shopRepository,
43+
itemTagRepository: itemTagRepository,
44+
tabViewModel: tabViewModel,
45+
mainTab: mainTab
46+
)
47+
48+
#expect(viewModel.leftInShopSlots == 3)
49+
}
50+
51+
@Test("Should pop to root view", arguments: [false, true])
52+
func shouldPopToRootView(shouldPopToRootView: Bool) {
53+
sessionController.shouldPopToRootView = shouldPopToRootView
54+
55+
shopRepository.setShops(shops: shops)
56+
57+
let viewModel = ShopListViewModel(
58+
sessionController: sessionController,
59+
shopRepository: shopRepository,
60+
itemTagRepository: itemTagRepository,
61+
tabViewModel: tabViewModel,
62+
mainTab: mainTab
63+
)
64+
65+
viewModel.reload()
66+
67+
#expect(viewModel.shouldPopToRootView == shouldPopToRootView)
68+
}
69+
70+
@Test
71+
func reload() {
72+
shopRepository.setShops(shops: shops)
73+
74+
let viewModel = ShopListViewModel(
75+
sessionController: sessionController,
76+
shopRepository: shopRepository,
77+
itemTagRepository: itemTagRepository,
78+
tabViewModel: tabViewModel,
79+
mainTab: mainTab
80+
)
81+
82+
viewModel.reload()
83+
84+
#expect(viewModel.shops.count == 5)
85+
}
86+
87+
@Test
88+
func reloadFailed() {
89+
shopRepository.setShops(shops: shops)
90+
shopRepository.error = NativeAppTemplateAPIError.requestFailed(nil, 422, nil)
91+
92+
let viewModel = ShopListViewModel(
93+
sessionController: sessionController,
94+
shopRepository: shopRepository,
95+
itemTagRepository: itemTagRepository,
96+
tabViewModel: tabViewModel,
97+
mainTab: mainTab
98+
)
99+
100+
viewModel.reload()
101+
102+
#expect(viewModel.state == .failed)
103+
}
104+
105+
@Test
106+
func showCreateView() {
107+
let viewModel = ShopListViewModel(
108+
sessionController: sessionController,
109+
shopRepository: shopRepository,
110+
itemTagRepository: itemTagRepository,
111+
tabViewModel: tabViewModel,
112+
mainTab: mainTab
113+
)
114+
115+
#expect(viewModel.isShowingCreateSheet == false)
116+
117+
viewModel.showCreateView()
118+
119+
#expect(viewModel.isShowingCreateSheet == true)
120+
121+
viewModel.showCreateView()
122+
123+
#expect(viewModel.isShowingCreateSheet == false)
124+
}
125+
126+
@Test
127+
func setTabViewModelShowingDetailViewToFalse() {
128+
tabViewModel.showingDetailView[mainTab] = true
129+
130+
let viewModel = ShopListViewModel(
131+
sessionController: sessionController,
132+
shopRepository: shopRepository,
133+
itemTagRepository: itemTagRepository,
134+
tabViewModel: tabViewModel,
135+
mainTab: mainTab
136+
)
137+
138+
viewModel.setTabViewModelShowingDetailViewToFalse()
139+
140+
#expect(tabViewModel.showingDetailView[mainTab] == false)
141+
}
142+
143+
@Test
144+
func scrollToTopID() {
145+
let viewModel = ShopListViewModel(
146+
sessionController: sessionController,
147+
shopRepository: shopRepository,
148+
itemTagRepository: itemTagRepository,
149+
tabViewModel: tabViewModel,
150+
mainTab: mainTab
151+
)
152+
153+
let scrollToTopID = viewModel.scrollToTopID()
154+
155+
#expect(scrollToTopID == ScrollToTopID(mainTab: mainTab, detail: false))
156+
}
157+
158+
@Test
159+
func repositoryProperties() {
160+
shopRepository.setShops(shops: shops)
161+
shopRepository.limitCount = 10
162+
shopRepository.createdShopsCount = 5
163+
164+
let viewModel = ShopListViewModel(
165+
sessionController: sessionController,
166+
shopRepository: shopRepository,
167+
itemTagRepository: itemTagRepository,
168+
tabViewModel: tabViewModel,
169+
mainTab: mainTab
170+
)
171+
172+
viewModel.reload()
173+
174+
#expect(viewModel.shops.count == 5)
175+
#expect(viewModel.limitCount == 10)
176+
#expect(viewModel.createdShopsCount == 5)
177+
#expect(viewModel.isEmpty == false)
178+
#expect(viewModel.leftInShopSlots == 5)
179+
}
180+
181+
@Test
182+
func emptyState() {
183+
shopRepository.setShops(shops: [])
184+
185+
let viewModel = ShopListViewModel(
186+
sessionController: sessionController,
187+
shopRepository: shopRepository,
188+
itemTagRepository: itemTagRepository,
189+
tabViewModel: tabViewModel,
190+
mainTab: mainTab
191+
)
192+
193+
viewModel.reload()
194+
195+
#expect(viewModel.shops.isEmpty == true)
196+
#expect(viewModel.isEmpty == true)
197+
}
198+
199+
private func mockShop(id: String = UUID().uuidString, name: String = "Mock Shop") -> Shop {
200+
Shop(
201+
id: id,
202+
name: name,
203+
description: "This is a mock shop for testing",
204+
timeZone: "Tokyo",
205+
itemTagsCount: 10,
206+
scannedItemTagsCount: 5,
207+
completedItemTagsCount: 3,
208+
displayShopServerPath: "https://api.nativeapptemplate.com/display/shops/\(id)?type=server"
209+
)
210+
}
211+
}

0 commit comments

Comments
 (0)