|
| 1 | +// |
| 2 | +// Copyright Β© 2024 Stream.io Inc. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +import SnapshotTesting |
| 6 | +@testable import StreamChat |
| 7 | +@testable import StreamChatSwiftUI |
| 8 | +@testable import StreamChatTestTools |
| 9 | +import StreamSwiftTestHelpers |
| 10 | +import SwiftUI |
| 11 | +import XCTest |
| 12 | + |
| 13 | +class ChatThreadListView_Tests: StreamChatTestCase { |
| 14 | + |
| 15 | + func test_chatThreadListView_empty() { |
| 16 | + let view = makeView(.empty()) |
| 17 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 18 | + } |
| 19 | + |
| 20 | + func test_chatThreadListView_loading() { |
| 21 | + let view = makeView(.loading()) |
| 22 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 23 | + } |
| 24 | + |
| 25 | + func test_chatThreadListView_withThreads() { |
| 26 | + let view = makeView(.withThreads()) |
| 27 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 28 | + } |
| 29 | + |
| 30 | + func test_chatThreadListView_loadingMoreThreads() { |
| 31 | + let view = makeView(.loadingMoreThreads()) |
| 32 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 33 | + } |
| 34 | + |
| 35 | + func test_chatThreadListView_reloadingThreads() { |
| 36 | + let view = makeView(.reloadingThreads()) |
| 37 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 38 | + } |
| 39 | + |
| 40 | + func test_chatThreadListView_whenNewThreadsAvailable() { |
| 41 | + let view = makeView(.newThreadsAvailable()) |
| 42 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 43 | + } |
| 44 | + |
| 45 | + func test_chatThreadListView_errorLoadingThreads() { |
| 46 | + let view = makeView(.errorLoadingThreads()) |
| 47 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 48 | + } |
| 49 | + |
| 50 | + func test_chatThreadListView_errorLoadingMoreThreads() { |
| 51 | + let view = makeView(.errorLoadingMoreThreads()) |
| 52 | + assertSnapshot(matching: view, as: .image(perceptualPrecision: precision)) |
| 53 | + } |
| 54 | + |
| 55 | + private func makeView(_ viewModel: MockChatThreadListViewModel) -> some View { |
| 56 | + ChatThreadListView( |
| 57 | + viewFactory: DefaultViewFactory.shared, |
| 58 | + viewModel: viewModel |
| 59 | + ) |
| 60 | + .applyDefaultSize() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class CustomFactory: ViewFactory { |
| 65 | + @Injected(\.chatClient) public var chatClient |
| 66 | + |
| 67 | + func makeThreadListLoadingView() -> some View { |
| 68 | + LoadingView() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +private class MockChatThreadListViewModel: ChatThreadListViewModel { |
| 73 | + static func empty() -> MockChatThreadListViewModel { |
| 74 | + return MockChatThreadListViewModel( |
| 75 | + threads: [], |
| 76 | + isLoading: false, |
| 77 | + isReloading: false, |
| 78 | + isEmpty: true, |
| 79 | + failedToLoadThreads: false, |
| 80 | + failedToLoadMoreThreads: false, |
| 81 | + isLoadingMoreThreads: false, |
| 82 | + hasLoadedAllThreads: false, |
| 83 | + newThreadsCount: 0, |
| 84 | + hasNewThreads: false |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + static func loading() -> MockChatThreadListViewModel { |
| 89 | + return MockChatThreadListViewModel( |
| 90 | + threads: [], |
| 91 | + isLoading: true, |
| 92 | + isReloading: false, |
| 93 | + isEmpty: false, |
| 94 | + failedToLoadThreads: false, |
| 95 | + failedToLoadMoreThreads: false, |
| 96 | + isLoadingMoreThreads: false, |
| 97 | + hasLoadedAllThreads: false, |
| 98 | + newThreadsCount: 0, |
| 99 | + hasNewThreads: false |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + static func withThreads() -> MockChatThreadListViewModel { |
| 104 | + return MockChatThreadListViewModel( |
| 105 | + threads: mockThreads, |
| 106 | + isLoading: false, |
| 107 | + isReloading: false, |
| 108 | + isEmpty: false, |
| 109 | + failedToLoadThreads: false, |
| 110 | + failedToLoadMoreThreads: false, |
| 111 | + isLoadingMoreThreads: false, |
| 112 | + hasLoadedAllThreads: false, |
| 113 | + newThreadsCount: 0, |
| 114 | + hasNewThreads: false |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | + static func loadingMoreThreads() -> MockChatThreadListViewModel { |
| 119 | + return MockChatThreadListViewModel( |
| 120 | + threads: mockThreads, |
| 121 | + isLoading: false, |
| 122 | + isReloading: false, |
| 123 | + isEmpty: false, |
| 124 | + failedToLoadThreads: false, |
| 125 | + failedToLoadMoreThreads: false, |
| 126 | + isLoadingMoreThreads: true, |
| 127 | + hasLoadedAllThreads: false, |
| 128 | + newThreadsCount: 0, |
| 129 | + hasNewThreads: false |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + static func reloadingThreads() -> MockChatThreadListViewModel { |
| 134 | + return MockChatThreadListViewModel( |
| 135 | + threads: mockThreads, |
| 136 | + isLoading: false, |
| 137 | + isReloading: true, |
| 138 | + isEmpty: false, |
| 139 | + failedToLoadThreads: false, |
| 140 | + failedToLoadMoreThreads: false, |
| 141 | + isLoadingMoreThreads: false, |
| 142 | + hasLoadedAllThreads: false, |
| 143 | + newThreadsCount: 0, |
| 144 | + hasNewThreads: false |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + static func newThreadsAvailable() -> MockChatThreadListViewModel { |
| 149 | + return MockChatThreadListViewModel( |
| 150 | + threads: mockThreads, |
| 151 | + isLoading: false, |
| 152 | + isReloading: false, |
| 153 | + isEmpty: false, |
| 154 | + failedToLoadThreads: false, |
| 155 | + failedToLoadMoreThreads: false, |
| 156 | + isLoadingMoreThreads: false, |
| 157 | + hasLoadedAllThreads: false, |
| 158 | + newThreadsCount: 2, |
| 159 | + hasNewThreads: true |
| 160 | + ) |
| 161 | + } |
| 162 | + |
| 163 | + static func errorLoadingThreads() -> MockChatThreadListViewModel { |
| 164 | + return MockChatThreadListViewModel( |
| 165 | + threads: [], |
| 166 | + isLoading: false, |
| 167 | + isReloading: false, |
| 168 | + isEmpty: true, |
| 169 | + failedToLoadThreads: true, |
| 170 | + failedToLoadMoreThreads: false, |
| 171 | + isLoadingMoreThreads: false, |
| 172 | + hasLoadedAllThreads: false, |
| 173 | + newThreadsCount: 0, |
| 174 | + hasNewThreads: false |
| 175 | + ) |
| 176 | + } |
| 177 | + |
| 178 | + static func errorLoadingMoreThreads() -> MockChatThreadListViewModel { |
| 179 | + return MockChatThreadListViewModel( |
| 180 | + threads: mockThreads, |
| 181 | + isLoading: false, |
| 182 | + isReloading: false, |
| 183 | + isEmpty: false, |
| 184 | + failedToLoadThreads: false, |
| 185 | + failedToLoadMoreThreads: true, |
| 186 | + isLoadingMoreThreads: false, |
| 187 | + hasLoadedAllThreads: false, |
| 188 | + newThreadsCount: 0, |
| 189 | + hasNewThreads: false |
| 190 | + ) |
| 191 | + } |
| 192 | + |
| 193 | + convenience init( |
| 194 | + threads: [ChatThread], |
| 195 | + isLoading: Bool, |
| 196 | + isReloading: Bool, |
| 197 | + isEmpty: Bool, |
| 198 | + failedToLoadThreads: Bool, |
| 199 | + failedToLoadMoreThreads: Bool, |
| 200 | + isLoadingMoreThreads: Bool, |
| 201 | + hasLoadedAllThreads: Bool, |
| 202 | + newThreadsCount: Int, |
| 203 | + hasNewThreads: Bool |
| 204 | + ) { |
| 205 | + self.init(threadListController: nil, eventsController: nil) |
| 206 | + self.threads = LazyCachedMapCollection(elements: threads) |
| 207 | + self.isLoading = isLoading |
| 208 | + self.isReloading = isReloading |
| 209 | + self.isEmpty = isEmpty |
| 210 | + self.failedToLoadThreads = failedToLoadThreads |
| 211 | + self.failedToLoadMoreThreads = failedToLoadMoreThreads |
| 212 | + self.isLoadingMoreThreads = isLoadingMoreThreads |
| 213 | + self.hasLoadedAllThreads = hasLoadedAllThreads |
| 214 | + self.newThreadsCount = newThreadsCount |
| 215 | + self.hasNewThreads = hasNewThreads |
| 216 | + } |
| 217 | + |
| 218 | + override func viewDidAppear() {} |
| 219 | + override func loadThreads() {} |
| 220 | + override func loadMoreThreads() {} |
| 221 | + override func controller( |
| 222 | + _ controller: ChatThreadListController, |
| 223 | + didChangeThreads changes: [ListChange<ChatThread>] |
| 224 | + ) {} |
| 225 | + |
| 226 | + |
| 227 | + static var mockYoda = ChatUser.mock(id: .unique, name: "Yoda") |
| 228 | + static var mockVader = ChatUser.mock(id: .unique, name: "Vader") |
| 229 | + |
| 230 | + static var mockThreads: [ChatThread] { |
| 231 | + [ |
| 232 | + .mock( |
| 233 | + parentMessage: .mock(text: "Parent Message", author: mockYoda), |
| 234 | + channel: .mock(cid: .unique, name: "Star Wars Channel"), |
| 235 | + createdBy: mockVader, |
| 236 | + replyCount: 3, |
| 237 | + participantCount: 2, |
| 238 | + threadParticipants: [ |
| 239 | + .mock(user: mockYoda), |
| 240 | + .mock(user: mockVader) |
| 241 | + ], |
| 242 | + lastMessageAt: .unique, |
| 243 | + createdAt: .unique, |
| 244 | + updatedAt: .unique, |
| 245 | + title: nil, |
| 246 | + latestReplies: [ |
| 247 | + .mock(text: "First Message", author: mockYoda), |
| 248 | + .mock(text: "Second Message", author: mockVader), |
| 249 | + .mock(text: "Third Message", author: mockYoda) |
| 250 | + ], |
| 251 | + reads: [ |
| 252 | + .mock(user: mockYoda, unreadMessagesCount: 6) |
| 253 | + ], |
| 254 | + extraData: [:] |
| 255 | + ), |
| 256 | + .mock( |
| 257 | + parentMessage: .mock(text: "Parent Message 2", author: mockYoda), |
| 258 | + channel: .mock(cid: .unique, name: "Marvel Channel"), |
| 259 | + createdBy: mockVader, |
| 260 | + replyCount: 3, |
| 261 | + participantCount: 2, |
| 262 | + threadParticipants: [ |
| 263 | + .mock(user: mockYoda), |
| 264 | + .mock(user: mockVader) |
| 265 | + ], |
| 266 | + lastMessageAt: .unique, |
| 267 | + createdAt: .unique, |
| 268 | + updatedAt: .unique, |
| 269 | + title: nil, |
| 270 | + latestReplies: [ |
| 271 | + .mock(text: "First Message", author: mockVader) |
| 272 | + ], |
| 273 | + reads: [], |
| 274 | + extraData: [:] |
| 275 | + ) |
| 276 | + ] |
| 277 | + } |
| 278 | +} |
0 commit comments