Skip to content

Commit f7a7127

Browse files
added tests for ChatChannelNamer
1 parent 29bc24b commit f7a7127

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
84C94D5C275A2E9F007FE2B9 /* StreamChatUtilsMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D5B275A2E9F007FE2B9 /* StreamChatUtilsMocks.swift */; };
220220
84C94D5E275A3AA9007FE2B9 /* ImageCDN_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D5D275A3AA9007FE2B9 /* ImageCDN_Tests.swift */; };
221221
84C94D60275A45D2007FE2B9 /* ViewFactory_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D5F275A45D2007FE2B9 /* ViewFactory_Tests.swift */; };
222+
84C94D62275A5BB7007FE2B9 /* ChatChannelNamer_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D61275A5BB7007FE2B9 /* ChatChannelNamer_Tests.swift */; };
222223
84EDBC37274FE5CD0057218D /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 84EDBC36274FE5CD0057218D /* Localizable.strings */; };
223224
/* End PBXBuildFile section */
224225

@@ -468,6 +469,7 @@
468469
84C94D5B275A2E9F007FE2B9 /* StreamChatUtilsMocks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamChatUtilsMocks.swift; sourceTree = "<group>"; };
469470
84C94D5D275A3AA9007FE2B9 /* ImageCDN_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageCDN_Tests.swift; sourceTree = "<group>"; };
470471
84C94D5F275A45D2007FE2B9 /* ViewFactory_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewFactory_Tests.swift; sourceTree = "<group>"; };
472+
84C94D61275A5BB7007FE2B9 /* ChatChannelNamer_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatChannelNamer_Tests.swift; sourceTree = "<group>"; };
471473
84EDBC36274FE5CD0057218D /* Localizable.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; path = Localizable.strings; sourceTree = "<group>"; };
472474
/* End PBXFileReference section */
473475

@@ -990,6 +992,7 @@
990992
84C94D59275A2E43007FE2B9 /* StreamChat_Utils_Tests.swift */,
991993
84C94D5D275A3AA9007FE2B9 /* ImageCDN_Tests.swift */,
992994
84C94D5F275A45D2007FE2B9 /* ViewFactory_Tests.swift */,
995+
84C94D61275A5BB7007FE2B9 /* ChatChannelNamer_Tests.swift */,
993996
);
994997
path = Utils;
995998
sourceTree = "<group>";
@@ -1308,6 +1311,7 @@
13081311
84C94D4D2758FD5C007FE2B9 /* MessageComposerViewModel_Tests.swift in Sources */,
13091312
84C94CDB27578B92007FE2B9 /* NSManagedObject+ContextChange.swift in Sources */,
13101313
84C94D0C27578BF2007FE2B9 /* AssertJSONEqual.swift in Sources */,
1314+
84C94D62275A5BB7007FE2B9 /* ChatChannelNamer_Tests.swift in Sources */,
13111315
84C94D492758BE1C007FE2B9 /* ChatChannelViewModel_Tests.swift in Sources */,
13121316
84C94CE027578B92007FE2B9 /* CurrentUserPayload.swift in Sources */,
13131317
84C94CCD27578B92007FE2B9 /* ChannelUnreadCount_Mock.swift in Sources */,
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
//
2+
// Copyright © 2021 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import StreamChat
6+
@testable import StreamChatSwiftUI
7+
import XCTest
8+
9+
class ChatChannelNamer_Tests: XCTestCase {
10+
11+
var defaultMembers: [ChatChannelMember]!
12+
13+
override func setUp() {
14+
super.setUp()
15+
16+
defaultMembers = [
17+
.mock(
18+
id: .unique,
19+
name: "Darth Vader",
20+
imageURL: nil,
21+
isOnline: true
22+
),
23+
.mock(
24+
id: .unique,
25+
name: "Darth Maul",
26+
imageURL: nil,
27+
isOnline: true
28+
),
29+
.mock(
30+
id: .unique,
31+
name: "Kylo Ren",
32+
imageURL: nil,
33+
isOnline: true
34+
)
35+
]
36+
}
37+
38+
func test_defaultChannelNamer_whenChannelHasName_showsChannelName() {
39+
// Given
40+
let channel = ChatChannel.mock(
41+
cid: .unique,
42+
name: "Darth Channel",
43+
imageURL: URL(string: "https://example.com")!,
44+
lastActiveMembers: defaultMembers
45+
)
46+
let currentUserId: String = .unique
47+
let namer: ChatChannelNamer = DefaultChatChannelNamer()
48+
49+
// When
50+
let nameForChannel = namer(channel, currentUserId)
51+
52+
// Then
53+
XCTAssertEqual(nameForChannel, "Darth Channel")
54+
}
55+
56+
func test_defaultChannelNamer_directChannel_whenChannelHasNoName_andExactly2Members_showsCurrentMembers() {
57+
// Given
58+
defaultMembers = [
59+
.mock(
60+
id: .unique,
61+
name: "Darth Vader",
62+
imageURL: nil,
63+
isOnline: true
64+
),
65+
.mock(
66+
id: .unique,
67+
name: "Darth Maul",
68+
imageURL: nil,
69+
isOnline: true
70+
)
71+
]
72+
let channel = ChatChannel.mockDMChannel(
73+
name: nil,
74+
lastActiveMembers: defaultMembers
75+
)
76+
let currentUserId: String = .unique
77+
let namer: ChatChannelNamer = DefaultChatChannelNamer()
78+
79+
// When
80+
let nameForChannel = namer(channel, currentUserId)
81+
82+
// Then
83+
XCTAssertEqual(nameForChannel, "Darth Maul and Darth Vader")
84+
}
85+
86+
func test_defaultChannelNamer_directChannel_whenChannelHasNoName_whenChannelHasNoMembers_showsCurrentUserId() {
87+
// Given
88+
let channel = ChatChannel.mockDMChannel(
89+
name: nil
90+
)
91+
let currentUserId: String = .unique
92+
let namer: ChatChannelNamer = DefaultChatChannelNamer()
93+
94+
// When
95+
let nameForChannel = namer(channel, currentUserId)
96+
97+
// Then
98+
XCTAssertEqual(nameForChannel, nil)
99+
}
100+
101+
func test_defaultChannelNamer_directChannel_whenChannelHasNoName_whenChannelHasOnlyCurrentMember_showsCurrentMemberName() {
102+
// Given
103+
let currentUser: ChatChannelMember = .mock(id: .unique, name: "Luke Skywalker")
104+
let channel = ChatChannel.mockDMChannel(
105+
name: nil,
106+
lastActiveMembers: [currentUser]
107+
)
108+
let currentUserId: String = currentUser.id
109+
let namer: ChatChannelNamer = DefaultChatChannelNamer()
110+
111+
// When
112+
let nameForChannel = namer(channel, currentUserId)
113+
114+
// Then
115+
XCTAssertEqual(nameForChannel, currentUser.name)
116+
}
117+
118+
func test_defaultChannelNamer_directChannel_whenChannelHasNoName_andMoreThan2Members_showsMembersAndNMore() {
119+
// Given
120+
let channel = ChatChannel.mockDMChannel(
121+
name: nil,
122+
lastActiveMembers: defaultMembers
123+
)
124+
let currentUserId: String = .unique
125+
let namer: ChatChannelNamer = DefaultChatChannelNamer()
126+
127+
// When
128+
let nameForChannel = namer(channel, currentUserId)
129+
130+
// Then
131+
XCTAssertEqual(nameForChannel, "Darth Maul, Darth Vader and 1 more")
132+
}
133+
134+
func test_defaultChannelNamer_whenChannelHasNoName_AndNotDM_returnsNil() {
135+
// Given
136+
let channelID: String = .unique
137+
let channel = ChatChannel.mock(
138+
cid: ChannelId(type: .gaming, id: channelID),
139+
name: nil
140+
)
141+
let currentUserId: String = .unique
142+
let namer: ChatChannelNamer = DefaultChatChannelNamer()
143+
144+
// When
145+
let nameForChannel = namer(channel, currentUserId)
146+
147+
// Then
148+
XCTAssertEqual(nameForChannel, nil)
149+
}
150+
151+
func test_defaultChannelNamer_withModifiedParameters_customSeparator() {
152+
// Given
153+
let channel = ChatChannel.mockDMChannel(
154+
name: nil,
155+
lastActiveMembers: defaultMembers
156+
)
157+
let currentUserId: String = .unique
158+
let namer: ChatChannelNamer = DefaultChatChannelNamer(separator: " |")
159+
160+
// When
161+
let nameForChannel = namer(channel, currentUserId)
162+
163+
// Then
164+
XCTAssertEqual(nameForChannel, "Darth Maul | Darth Vader and 1 more")
165+
}
166+
167+
func test_defaultChannelNamer_withModifiedParameters_numberOfMaximumMembers() {
168+
// Given
169+
defaultMembers = [
170+
.mock(
171+
id: .unique,
172+
name: "Darth Vader",
173+
imageURL: nil,
174+
isOnline: true
175+
),
176+
.mock(
177+
id: .unique,
178+
name: "Darth Maul",
179+
imageURL: nil,
180+
isOnline: true
181+
),
182+
.mock(
183+
id: .unique,
184+
name: "Kylo Ren",
185+
imageURL: nil,
186+
isOnline: true
187+
),
188+
.mock(
189+
id: .unique,
190+
name: "Darth Bane",
191+
imageURL: nil,
192+
isOnline: true
193+
)
194+
]
195+
let channel = ChatChannel.mockDMChannel(
196+
name: nil,
197+
lastActiveMembers: defaultMembers
198+
)
199+
let currentUserId: String = .unique
200+
let namer: ChatChannelNamer = DefaultChatChannelNamer(maxMemberNames: 4, separator: " |")
201+
202+
// When
203+
let nameForChannel = namer(channel, currentUserId)
204+
205+
// Then
206+
XCTAssertEqual(nameForChannel, "Darth Bane | Darth Maul | Darth Vader | Kylo Ren")
207+
}
208+
209+
}

0 commit comments

Comments
 (0)