Skip to content

Commit dd8c699

Browse files
added tests for MoreChannelActionsViewModel
1 parent 49e5214 commit dd8c699

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@
206206
84C94D3D27579BB0007FE2B9 /* TestDataModel2.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D3527579BB0007FE2B9 /* TestDataModel2.xcdatamodeld */; };
207207
84C94D3E27579BB0007FE2B9 /* StreamChatModel.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D3727579BB0007FE2B9 /* StreamChatModel.xcdatamodeld */; };
208208
84C94D422757C16D007FE2B9 /* ChatChannelListTestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D412757C16D007FE2B9 /* ChatChannelListTestHelpers.swift */; };
209+
84C94D442757C704007FE2B9 /* MoreChannelActionsViewModel_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D432757C704007FE2B9 /* MoreChannelActionsViewModel_Tests.swift */; };
210+
84C94D462757D1CA007FE2B9 /* ImageLoader_Mock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D452757D1CA007FE2B9 /* ImageLoader_Mock.swift */; };
209211
84EDBC37274FE5CD0057218D /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 84EDBC36274FE5CD0057218D /* Localizable.strings */; };
210212
/* End PBXBuildFile section */
211213

@@ -442,6 +444,8 @@
442444
84C94D3627579BB0007FE2B9 /* TestDataModel2.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = TestDataModel2.xcdatamodel; sourceTree = "<group>"; };
443445
84C94D3827579BB0007FE2B9 /* StreamChatModel.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = StreamChatModel.xcdatamodel; sourceTree = "<group>"; };
444446
84C94D412757C16D007FE2B9 /* ChatChannelListTestHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatChannelListTestHelpers.swift; sourceTree = "<group>"; };
447+
84C94D432757C704007FE2B9 /* MoreChannelActionsViewModel_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoreChannelActionsViewModel_Tests.swift; sourceTree = "<group>"; };
448+
84C94D452757D1CA007FE2B9 /* ImageLoader_Mock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageLoader_Mock.swift; sourceTree = "<group>"; };
445449
84EDBC36274FE5CD0057218D /* Localizable.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; path = Localizable.strings; sourceTree = "<group>"; };
446450
/* End PBXFileReference section */
447451

@@ -782,6 +786,7 @@
782786
isa = PBXGroup;
783787
children = (
784788
84C94C7F27567D3F007FE2B9 /* ChatChannelListViewModel_Tests.swift */,
789+
84C94D432757C704007FE2B9 /* MoreChannelActionsViewModel_Tests.swift */,
785790
84C94D412757C16D007FE2B9 /* ChatChannelListTestHelpers.swift */,
786791
);
787792
path = ChatChannelList;
@@ -907,6 +912,7 @@
907912
84C94D272757954C007FE2B9 /* VirtualTimer.swift */,
908913
84C94D29275796D0007FE2B9 /* MockNetworkURLProtocol.swift */,
909914
84C94D2B275796F7007FE2B9 /* RequestRecorderURLProtocol.swift */,
915+
84C94D452757D1CA007FE2B9 /* ImageLoader_Mock.swift */,
910916
);
911917
path = Mocks;
912918
sourceTree = "<group>";
@@ -1237,6 +1243,7 @@
12371243
84C94D0B27578BF2007FE2B9 /* QueueAwareDelegate.swift in Sources */,
12381244
84C94CE427578B92007FE2B9 /* ChatChannelController_Mock.swift in Sources */,
12391245
84C94D282757954C007FE2B9 /* VirtualTimer.swift in Sources */,
1246+
84C94D442757C704007FE2B9 /* MoreChannelActionsViewModel_Tests.swift in Sources */,
12401247
84C94CD727578B92007FE2B9 /* ChatMessageLinkAttachment_Mock.swift in Sources */,
12411248
84C94D0527578BF2007FE2B9 /* TestRunnerEnvironment.swift in Sources */,
12421249
84C94D1A27579273007FE2B9 /* TestRequest.swift in Sources */,
@@ -1254,6 +1261,7 @@
12541261
84C94D1E2757929C007FE2B9 /* APIClient_Mock.swift in Sources */,
12551262
84C94CE927578B93007FE2B9 /* ChatClient_Mock.swift in Sources */,
12561263
84C94D2627579511007FE2B9 /* CDNClient_Mock.swift in Sources */,
1264+
84C94D462757D1CA007FE2B9 /* ImageLoader_Mock.swift in Sources */,
12571265
84C94D2A275796D0007FE2B9 /* MockNetworkURLProtocol.swift in Sources */,
12581266
84C94CCF27578B92007FE2B9 /* ChatMessageReaction_Mock.swift in Sources */,
12591267
84C94D0327578BF2007FE2B9 /* WaitFor.swift in Sources */,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Copyright © 2021 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import Foundation
6+
import UIKit
7+
@testable import StreamChat
8+
import StreamChatSwiftUI
9+
10+
class ImageLoader_Mock: ImageLoading {
11+
12+
static let defaultLoadedImage = UIImage(systemName: "checkmark")!
13+
14+
func loadImage(
15+
url: URL?,
16+
imageCDN: ImageCDN,
17+
resize: Bool,
18+
preferredSize: CGSize?,
19+
completion: @escaping ((Result<UIImage, Error>) -> Void)) {
20+
completion(.success(Self.defaultLoadedImage))
21+
}
22+
23+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// Copyright © 2021 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import XCTest
6+
@testable import StreamChatSwiftUI
7+
@testable import StreamChat
8+
9+
class MoreChannelActionsViewModel_Tests: XCTestCase {
10+
11+
@Injected(\.images) var images
12+
13+
private var chatClient: ChatClient = {
14+
let client = ChatClient.mock()
15+
client.currentUserId = .unique
16+
return client
17+
}()
18+
19+
private var streamChat: StreamChat?
20+
21+
override func setUp() {
22+
super.setUp()
23+
let imageLoader = ImageLoader_Mock()
24+
let utils = Utils(imageLoader: imageLoader)
25+
streamChat = StreamChat(chatClient: chatClient, utils: utils)
26+
}
27+
28+
func test_moreActionsVM_membersLoaded() {
29+
// Given
30+
let memberId: String = .unique
31+
let viewModel = makeMoreActionsViewModel(
32+
members: [.mock(id: memberId, isOnline: true)]
33+
)
34+
35+
// When
36+
let members = viewModel.members
37+
38+
// Then
39+
XCTAssert(members.count == 1)
40+
XCTAssert(members[0].id == memberId)
41+
}
42+
43+
func test_moreActionsVM_chatHeaderInfo() {
44+
// Given
45+
let viewModel = makeMoreActionsViewModel()
46+
47+
// When
48+
let title = viewModel.chatName
49+
let subtitle = viewModel.subtitleText
50+
51+
// Then
52+
XCTAssert(title == "test")
53+
XCTAssert(subtitle == "Online")
54+
}
55+
56+
func test_moreActionsVM_imageLoading() {
57+
// Given
58+
let memberId: String = .unique
59+
let member: ChatChannelMember = .mock(id: memberId, isOnline: true)
60+
let viewModel = makeMoreActionsViewModel(
61+
members: [member]
62+
)
63+
64+
// When
65+
let firstImage = viewModel.image(for: member)
66+
let secondImage = viewModel.image(for: member)
67+
let cachedImage = viewModel.memberAvatars[memberId]
68+
69+
// Then
70+
XCTAssert(firstImage == images.userAvatarPlaceholder2)
71+
XCTAssert(secondImage == ImageLoader_Mock.defaultLoadedImage)
72+
XCTAssert(cachedImage != nil)
73+
XCTAssert(cachedImage == secondImage)
74+
}
75+
76+
//MARK: - private
77+
78+
private func makeMoreActionsViewModel(
79+
members: [ChatChannelMember] = []
80+
) -> MoreChannelActionsViewModel {
81+
var channelMembers = [ChatChannelMember]()
82+
if !members.isEmpty {
83+
channelMembers = members
84+
} else {
85+
channelMembers = [.mock(id: .unique, isOnline: true)]
86+
}
87+
let channel = ChatChannel.mockDMChannel(
88+
name: "test",
89+
lastActiveMembers: channelMembers
90+
)
91+
92+
let channelActions = ChannelAction.defaultActions(
93+
for: channel,
94+
chatClient: chatClient,
95+
onDismiss: {},
96+
onError: { _ in }
97+
)
98+
99+
let moreActionsVM = MoreChannelActionsViewModel(
100+
channel: channel,
101+
channelActions: channelActions
102+
)
103+
104+
return moreActionsVM
105+
}
106+
107+
}

0 commit comments

Comments
 (0)