-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathLearnerDashboardViewModelTests.swift
More file actions
268 lines (225 loc) · 9.57 KB
/
LearnerDashboardViewModelTests.swift
File metadata and controls
268 lines (225 loc) · 9.57 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//
// This file is part of Canvas.
// Copyright (C) 2024-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
import Combine
import CombineSchedulers
@testable import Core
@testable import Student
import SwiftUI
import TestsFoundation
import XCTest
final class LearnerDashboardViewModelTests: StudentTestCase {
private var testee: LearnerDashboardViewModel!
private var interactor: LearnerDashboardInteractorMock!
private var colorInteractor: LearnerDashboardColorInteractorLive!
private var courseSyncInteractor: CourseSyncInteractorMock!
private var scheduler: TestSchedulerOf<DispatchQueue>!
private var testDefaults: SessionDefaults!
override func setUp() {
super.setUp()
scheduler = DispatchQueue.test
interactor = LearnerDashboardInteractorMock()
courseSyncInteractor = CourseSyncInteractorMock()
testDefaults = SessionDefaults(sessionID: "test-session")
testDefaults.reset()
colorInteractor = LearnerDashboardColorInteractorLive(defaults: testDefaults)
}
override func tearDown() {
testee = nil
interactor = nil
colorInteractor = nil
courseSyncInteractor = nil
scheduler = nil
testDefaults.reset()
testDefaults = nil
super.tearDown()
}
// MARK: - Initialization
func test_init_shouldLoadWidgets() {
let widget1 = MockWidgetViewModel(id: SystemWidgetIdentifier.courseInvitations.rawValue)
let widget2 = MockWidgetViewModel(id: EditableWidgetIdentifier.helloWidget.rawValue)
testee = LearnerDashboardViewModel(
interactor: interactor,
colorInteractor: colorInteractor,
snackBarViewModel: SnackBarViewModel(scheduler: scheduler.eraseToAnyScheduler()),
mainScheduler: scheduler.eraseToAnyScheduler(),
courseSyncInteractor: courseSyncInteractor,
environment: env
)
interactor.loadWidgetsPublisher.send([widget1, widget2])
scheduler.advance()
XCTAssertEqual(testee.widgets.count, 2)
XCTAssertEqual(testee.widgets[0].id, SystemWidgetIdentifier.courseInvitations.rawValue)
XCTAssertEqual(testee.widgets[1].id, EditableWidgetIdentifier.helloWidget.rawValue)
}
// MARK: - Screen config
func test_screenConfig_shouldBeConfiguredCorrectly() {
testee = LearnerDashboardViewModel(
interactor: interactor,
colorInteractor: colorInteractor,
snackBarViewModel: SnackBarViewModel(scheduler: scheduler.eraseToAnyScheduler()),
mainScheduler: scheduler.eraseToAnyScheduler(),
courseSyncInteractor: courseSyncInteractor,
environment: env
)
XCTAssertEqual(testee.screenConfig.refreshable, true)
XCTAssertEqual(testee.screenConfig.showsScrollIndicators, false)
}
// MARK: - State management
func test_init_withNoWidgets_shouldSetDataState() {
testee = LearnerDashboardViewModel(
interactor: interactor,
colorInteractor: colorInteractor,
snackBarViewModel: SnackBarViewModel(scheduler: scheduler.eraseToAnyScheduler()),
mainScheduler: scheduler.eraseToAnyScheduler(),
courseSyncInteractor: courseSyncInteractor,
environment: env
)
interactor.loadWidgetsPublisher.send([])
scheduler.advance()
XCTAssertEqual(testee.state, .data)
}
func test_init_withWidgets_shouldSetDataState() {
let widget = MockWidgetViewModel(id: EditableWidgetIdentifier.helloWidget.rawValue)
testee = LearnerDashboardViewModel(
interactor: interactor,
colorInteractor: colorInteractor,
snackBarViewModel: SnackBarViewModel(scheduler: scheduler.eraseToAnyScheduler()),
mainScheduler: scheduler.eraseToAnyScheduler(),
courseSyncInteractor: courseSyncInteractor,
environment: env
)
interactor.loadWidgetsPublisher.send([widget])
scheduler.advance()
XCTAssertEqual(testee.state, .data)
}
// MARK: - Refresh
func test_refresh_shouldCallRefreshOnAllWidgets() {
let widget1 = MockWidgetViewModel(id: EditableWidgetIdentifier.helloWidget.rawValue)
let widget2 = MockWidgetViewModel(id: EditableWidgetIdentifier.coursesAndGroups.rawValue)
let widget3 = MockWidgetViewModel(id: SystemWidgetIdentifier.courseInvitations.rawValue)
testee = LearnerDashboardViewModel(
interactor: interactor,
colorInteractor: colorInteractor,
snackBarViewModel: SnackBarViewModel(scheduler: scheduler.eraseToAnyScheduler()),
mainScheduler: scheduler.eraseToAnyScheduler(),
courseSyncInteractor: courseSyncInteractor,
environment: env
)
interactor.loadWidgetsPublisher.send([widget3, widget1, widget2])
scheduler.advance()
testee.refresh(ignoreCache: true)
scheduler.advance()
XCTAssertEqual(widget1.refreshCalled, true)
XCTAssertEqual(widget1.refreshIgnoreCache, true)
XCTAssertEqual(widget2.refreshCalled, true)
XCTAssertEqual(widget2.refreshIgnoreCache, true)
XCTAssertEqual(widget3.refreshCalled, true)
XCTAssertEqual(widget3.refreshIgnoreCache, true)
}
func test_refresh_shouldCallCompletionWhenAllWidgetsFinish() {
let widget = MockWidgetViewModel(id: EditableWidgetIdentifier.helloWidget.rawValue)
testee = LearnerDashboardViewModel(
interactor: interactor,
colorInteractor: colorInteractor,
snackBarViewModel: SnackBarViewModel(scheduler: scheduler.eraseToAnyScheduler()),
mainScheduler: scheduler.eraseToAnyScheduler(),
courseSyncInteractor: courseSyncInteractor,
environment: env
)
interactor.loadWidgetsPublisher.send([widget])
scheduler.advance()
let expectation = expectation(description: "refresh completion")
testee.refresh(ignoreCache: false) {
expectation.fulfill()
}
scheduler.advance()
wait(for: [expectation], timeout: 5)
XCTAssertEqual(widget.refreshCalled, true)
XCTAssertEqual(widget.refreshIgnoreCache, false)
}
// MARK: - Offline Sync Handlers
func test_offlineSyncTriggered_shouldStartDownload() {
testee = LearnerDashboardViewModel(
interactor: interactor,
colorInteractor: colorInteractor,
snackBarViewModel: SnackBarViewModel(scheduler: scheduler.eraseToAnyScheduler()),
mainScheduler: scheduler.eraseToAnyScheduler(),
courseSyncInteractor: courseSyncInteractor,
environment: env
)
let entries = [CourseSyncEntry.make()]
NotificationCenter.default.post(
name: .OfflineSyncTriggered,
object: entries
)
XCTAssertEqual(courseSyncInteractor.downloadContentCalled, true)
XCTAssertEqual(courseSyncInteractor.downloadContentEntries?.count, 1)
}
func test_offlineSyncCleanTriggered_shouldCleanContent() {
testee = LearnerDashboardViewModel(
interactor: interactor,
colorInteractor: colorInteractor,
snackBarViewModel: SnackBarViewModel(scheduler: scheduler.eraseToAnyScheduler()),
mainScheduler: scheduler.eraseToAnyScheduler(),
courseSyncInteractor: courseSyncInteractor,
environment: env
)
let ids = [CourseSyncID(value: "1")]
NotificationCenter.default.post(
name: .OfflineSyncCleanTriggered,
object: ids
)
XCTAssertEqual(courseSyncInteractor.cleanContentCalled, true)
XCTAssertEqual(courseSyncInteractor.cleanContentIds?.count, 1)
}
}
private final class MockWidgetViewModel: DashboardWidgetViewModel {
let id: String
let isHiddenInEmptyState = false
let state: InstUI.ScreenState = .data
var refreshCalled = false
var refreshIgnoreCache: Bool?
init(id: String) {
self.id = id
}
func makeView() -> AnyView {
AnyView(EmptyView())
}
func refresh(ignoreCache: Bool) -> AnyPublisher<Void, Never> {
refreshCalled = true
refreshIgnoreCache = ignoreCache
return Just(()).eraseToAnyPublisher()
}
}
private final class CourseSyncInteractorMock: CourseSyncInteractor {
var downloadContentCalled = false
var downloadContentEntries: [CourseSyncEntry]?
var cleanContentCalled = false
var cleanContentIds: [CourseSyncID]?
func downloadContent(for entries: [CourseSyncEntry]) -> AnyPublisher<[CourseSyncEntry], Never> {
downloadContentCalled = true
downloadContentEntries = entries
return Just(entries).eraseToAnyPublisher()
}
func cleanContent(for ids: [CourseSyncID]) -> AnyPublisher<Void, Never> {
cleanContentCalled = true
cleanContentIds = ids
return Just(()).eraseToAnyPublisher()
}
func cancel() {}
}