Skip to content

Commit 9151f07

Browse files
Added unit tests
1 parent 854128b commit 9151f07

File tree

7 files changed

+131
-50
lines changed

7 files changed

+131
-50
lines changed

DemoAppSwiftUI/AppDelegate.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
// Copyright © 2022 Stream.io Inc. All rights reserved.
33
//
44

5-
import FPSCounter
65
import StreamChat
76
import StreamChatSwiftUI
87
import SwiftUI
98
import UIKit
109

11-
class AppDelegate: NSObject, UIApplicationDelegate, FPSCounterDelegate {
10+
class AppDelegate: NSObject, UIApplicationDelegate {
1211

1312
var streamChat: StreamChat?
14-
15-
var fpsCounter = FPSCounter()
16-
13+
1714
var chatClient: ChatClient = {
1815
var config = ChatClientConfig(apiKey: .init(apiKeyString))
1916
// config.isLocalStorageEnabled = true
@@ -66,8 +63,6 @@ class AppDelegate: NSObject, UIApplicationDelegate, FPSCounterDelegate {
6663
}
6764
}
6865

69-
fpsCounter.delegate = self
70-
fpsCounter.startTracking()
7166
UNUserNotificationCenter.current().delegate = NotificationsHandler.shared
7267

7368
return true
@@ -100,10 +95,6 @@ class AppDelegate: NSObject, UIApplicationDelegate, FPSCounterDelegate {
10095
UserDefaults(suiteName: applicationGroupIdentifier)?.set(currentUserId, forKey: currentUserIdRegisteredForPush)
10196
}
10297
}
103-
104-
func fpsCounter(_ counter: FPSCounter, didUpdateFramesPerSecond fps: Int) {
105-
print("======== current fps = \(fps) =========")
106-
}
10798
}
10899

109100
extension UIColor {

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,13 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
308308

309309
private func handleDateChange() {
310310
guard showScrollToLatestButton == true, let currentDate = currentDate else {
311-
DispatchQueue.main.async { [weak self] in
312-
self?.currentDateString = nil
313-
}
311+
currentDateString = nil
314312
return
315313
}
316314

317-
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
318-
let dateString = messageListDateOverlay.string(from: currentDate)
319-
DispatchQueue.main.async { [unowned self] in
320-
if currentDateString != dateString {
321-
currentDateString = dateString
322-
}
323-
}
315+
let dateString = messageListDateOverlay.string(from: currentDate)
316+
if currentDateString != dateString {
317+
currentDateString = dateString
324318
}
325319
}
326320

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@
164164
846D6564279FF0800094B36E /* ReactionUserView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 846D6563279FF0800094B36E /* ReactionUserView.swift */; };
165165
847CEFEC27BFAB6400606257 /* StreamChat in Frameworks */ = {isa = PBXBuildFile; productRef = 847CEFEB27BFAB6400606257 /* StreamChat */; };
166166
847CEFEE27C38ABE00606257 /* MessageCachingUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 847CEFED27C38ABE00606257 /* MessageCachingUtils.swift */; };
167-
847CEFF527C3E88B00606257 /* FPSCounter in Frameworks */ = {isa = PBXBuildFile; productRef = 847CEFF427C3E88B00606257 /* FPSCounter */; };
168167
848399EA275FB3E9003075E4 /* SnapshotTesting in Frameworks */ = {isa = PBXBuildFile; productRef = 848399E9275FB3E9003075E4 /* SnapshotTesting */; };
169168
848399EC275FB41B003075E4 /* ChatChannelListView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848399EB275FB41B003075E4 /* ChatChannelListView_Tests.swift */; };
170169
848399F227601231003075E4 /* ReactionsOverlayView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848399F127601231003075E4 /* ReactionsOverlayView_Tests.swift */; };
@@ -185,6 +184,7 @@
185184
84B288D1274CEDD000DD090B /* GroupNameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B288D0274CEDD000DD090B /* GroupNameView.swift */; };
186185
84B288D3274D23AF00DD090B /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B288D2274D23AF00DD090B /* LoginView.swift */; };
187186
84B288D5274D286500DD090B /* LoginViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B288D4274D286500DD090B /* LoginViewModel.swift */; };
187+
84B439E127C6B2F100C04C99 /* MessageCachingUtils_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B439E027C6B2F100C04C99 /* MessageCachingUtils_Tests.swift */; };
188188
84B55F6A2798154C00B99B01 /* MessageListConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B55F692798154C00B99B01 /* MessageListConfig.swift */; };
189189
84B9B20E27998E9200BFAEAE /* ColorExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B9B20D27998E9200BFAEAE /* ColorExtensions.swift */; };
190190
84C2042327917B6A0024D616 /* MessageListView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C2042227917B6A0024D616 /* MessageListView_Tests.swift */; };
@@ -507,6 +507,7 @@
507507
84B288D0274CEDD000DD090B /* GroupNameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupNameView.swift; sourceTree = "<group>"; };
508508
84B288D2274D23AF00DD090B /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
509509
84B288D4274D286500DD090B /* LoginViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewModel.swift; sourceTree = "<group>"; };
510+
84B439E027C6B2F100C04C99 /* MessageCachingUtils_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageCachingUtils_Tests.swift; sourceTree = "<group>"; };
510511
84B55F692798154C00B99B01 /* MessageListConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageListConfig.swift; sourceTree = "<group>"; };
511512
84B9B20D27998E9200BFAEAE /* ColorExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorExtensions.swift; sourceTree = "<group>"; };
512513
84C2042227917B6A0024D616 /* MessageListView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageListView_Tests.swift; sourceTree = "<group>"; };
@@ -617,7 +618,6 @@
617618
isa = PBXFrameworksBuildPhase;
618619
buildActionMask = 2147483647;
619620
files = (
620-
847CEFF527C3E88B00606257 /* FPSCounter in Frameworks */,
621621
847CEFEC27BFAB6400606257 /* StreamChat in Frameworks */,
622622
8465FCB727468B0600AF091E /* NukeUI in Frameworks */,
623623
);
@@ -1215,6 +1215,7 @@
12151215
841B2EF5278F108700ED619E /* MessageReadIndicatorView_Tests.swift */,
12161216
84C94D4E2758FE59007FE2B9 /* ChatChannelTestHelpers.swift */,
12171217
8492975327B1725B00A8EEB0 /* MessageComposerView_Tests.swift */,
1218+
84B439E027C6B2F100C04C99 /* MessageCachingUtils_Tests.swift */,
12181219
);
12191220
path = ChatChannel;
12201221
sourceTree = "<group>";
@@ -1277,7 +1278,6 @@
12771278
packageProductDependencies = (
12781279
8465FCB627468B0600AF091E /* NukeUI */,
12791280
847CEFEB27BFAB6400606257 /* StreamChat */,
1280-
847CEFF427C3E88B00606257 /* FPSCounter */,
12811281
);
12821282
productName = StreamChatSwiftUI;
12831283
productReference = 8465FBB52746873A00AF091E /* StreamChatSwiftUI.framework */;
@@ -1360,7 +1360,6 @@
13601360
8465FCB527468B0600AF091E /* XCRemoteSwiftPackageReference "NukeUI" */,
13611361
848399E6275FB3BE003075E4 /* XCRemoteSwiftPackageReference "swift-snapshot-testing" */,
13621362
847CEFEA27BFAB6400606257 /* XCRemoteSwiftPackageReference "stream-chat-swift" */,
1363-
847CEFF327C3E88B00606257 /* XCRemoteSwiftPackageReference "fps-counter" */,
13641363
);
13651364
productRefGroup = 8465FBB62746873A00AF091E /* Products */;
13661365
projectDirPath = "";
@@ -1650,6 +1649,7 @@
16501649
84C94D3D27579BB0007FE2B9 /* TestDataModel2.xcdatamodeld in Sources */,
16511650
8421BCEC27A400E8000F977D /* ReactionsUsersView_Tests.swift in Sources */,
16521651
84C94CD327578B92007FE2B9 /* ChatMessageFileAttachment_Mock.swift in Sources */,
1652+
84B439E127C6B2F100C04C99 /* MessageCachingUtils_Tests.swift in Sources */,
16531653
8423C346277D9BFF0092DCF1 /* TestCommandsConfig.swift in Sources */,
16541654
84C94CE827578B92007FE2B9 /* ChatMessageController_Mock.swift in Sources */,
16551655
84C94CD127578B92007FE2B9 /* CurrentChatUser_Mock.swift in Sources */,
@@ -2127,14 +2127,6 @@
21272127
minimumVersion = 4.10.1;
21282128
};
21292129
};
2130-
847CEFF327C3E88B00606257 /* XCRemoteSwiftPackageReference "fps-counter" */ = {
2131-
isa = XCRemoteSwiftPackageReference;
2132-
repositoryURL = "https://github.com/konoma/fps-counter.git";
2133-
requirement = {
2134-
branch = master;
2135-
kind = branch;
2136-
};
2137-
};
21382130
848399E6275FB3BE003075E4 /* XCRemoteSwiftPackageReference "swift-snapshot-testing" */ = {
21392131
isa = XCRemoteSwiftPackageReference;
21402132
repositoryURL = "https://github.com/pointfreeco/swift-snapshot-testing.git";
@@ -2161,11 +2153,6 @@
21612153
package = 847CEFEA27BFAB6400606257 /* XCRemoteSwiftPackageReference "stream-chat-swift" */;
21622154
productName = StreamChat;
21632155
};
2164-
847CEFF427C3E88B00606257 /* FPSCounter */ = {
2165-
isa = XCSwiftPackageProductDependency;
2166-
package = 847CEFF327C3E88B00606257 /* XCRemoteSwiftPackageReference "fps-counter" */;
2167-
productName = FPSCounter;
2168-
};
21692156
848399E9275FB3E9003075E4 /* SnapshotTesting */ = {
21702157
isa = XCSwiftPackageProductDependency;
21712158
package = 848399E6275FB3BE003075E4 /* XCRemoteSwiftPackageReference "swift-snapshot-testing" */;

StreamChatSwiftUI.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

StreamChatSwiftUITests/Tests/ChatChannel/ChatChannelDataSource_Tests.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ class ChatChannelDataSource_Tests: StreamChatTestCase {
142142
var updateMessagesCalled = false
143143
var updateChannelCalled = false
144144

145-
func dataSource(channelDataSource: ChannelDataSource, didUpdateMessages messages: LazyCachedMapCollection<ChatMessage>) {
145+
func dataSource(
146+
channelDataSource: ChannelDataSource,
147+
didUpdateMessages messages: LazyCachedMapCollection<ChatMessage>,
148+
changes: [ListChange<ChatMessage>]
149+
) {
146150
updateMessagesCalled = true
147151
}
148152

StreamChatSwiftUITests/Tests/ChatChannel/ChatMessageIDs_Tests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class ChatMessageIDs_Tests: XCTestCase {
5959
cid: .unique,
6060
text: "test",
6161
author: .mock(id: .unique),
62-
attachments: ChatChannelTestHelpers.imageAttachments
62+
attachments: ChatChannelTestHelpers.imageAttachments,
63+
localState: .pendingSend
6364
)
6465

6566
// When
@@ -84,7 +85,8 @@ class ChatMessageIDs_Tests: XCTestCase {
8485
reactionScores: [
8586
MessageReactionType(rawValue: reaction): 3
8687
],
87-
attachments: ChatChannelTestHelpers.imageAttachments
88+
attachments: ChatChannelTestHelpers.imageAttachments,
89+
localState: .pendingSend
8890
)
8991

9092
// When
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//
2+
// Copyright © 2022 Stream.io Inc. All rights reserved.
3+
//
4+
5+
@testable import StreamChat
6+
@testable import StreamChatSwiftUI
7+
import XCTest
8+
9+
class MessageCachingUtils_Tests: XCTestCase {
10+
11+
let author = ChatUser.mock(
12+
id: "test",
13+
name: "Test",
14+
imageURL: URL(string: "https://test.com")!
15+
)
16+
lazy var message = ChatMessage.mock(
17+
id: .unique,
18+
cid: .unique,
19+
text: "Test",
20+
author: author,
21+
quotedMessage: .mock(
22+
id: .unique,
23+
cid: .unique,
24+
text: "Quoted",
25+
author: author
26+
)
27+
)
28+
29+
func test_messageCachingUtils_authorId() {
30+
// Given
31+
let utils = MessageCachingUtils()
32+
33+
// When
34+
let authorIdInitial = utils.authorId(for: message)
35+
let authorIdCached = utils.authorId(for: message)
36+
37+
// Then
38+
XCTAssert(authorIdInitial == "test")
39+
XCTAssert(authorIdInitial == authorIdCached)
40+
}
41+
42+
func test_messageCachingUtils_authorName() {
43+
// Given
44+
let utils = MessageCachingUtils()
45+
46+
// When
47+
let authorNameInitial = utils.authorName(for: message)
48+
let authorNameCached = utils.authorName(for: message)
49+
50+
// Then
51+
XCTAssert(authorNameInitial == "Test")
52+
XCTAssert(authorNameInitial == authorNameCached)
53+
}
54+
55+
func test_messageCachingUtils_imageURL() {
56+
// Given
57+
let utils = MessageCachingUtils()
58+
59+
// When
60+
let authorURLInitial = utils.authorImageURL(for: message)
61+
let authorURLCached = utils.authorImageURL(for: message)
62+
63+
// Then
64+
XCTAssert(authorURLInitial?.absoluteString == "https://test.com")
65+
XCTAssert(authorURLInitial == authorURLCached)
66+
}
67+
68+
func test_messageCachingUtils_quotedMessageAvailable() {
69+
// Given
70+
let utils = MessageCachingUtils()
71+
72+
// When
73+
let quotedMessageInitial = utils.quotedMessage(for: message)
74+
let quotedMessageCached = utils.quotedMessage(for: message)
75+
76+
// Then
77+
XCTAssert(quotedMessageInitial == quotedMessageCached)
78+
}
79+
80+
func test_messageCachingUtils_quotedMessageNotAvailable() {
81+
// Given
82+
let message = ChatMessage.mock(
83+
id: .unique,
84+
cid: .unique,
85+
text: .unique,
86+
author: .mock(id: .unique)
87+
)
88+
let utils = MessageCachingUtils()
89+
90+
// When
91+
let quotedMessageInitial = utils.quotedMessage(for: message)
92+
let quotedMessageCached = utils.quotedMessage(for: message)
93+
94+
// Then
95+
XCTAssert(quotedMessageInitial == nil)
96+
XCTAssert(quotedMessageCached == nil)
97+
}
98+
99+
func test_messageCachingUtils_recreatingCache() {
100+
// Given
101+
let utils = MessageCachingUtils()
102+
103+
// When
104+
let authorIdInitial = utils.authorId(for: message)
105+
utils.clearCache()
106+
let authorIdAfterClear = utils.authorId(for: message)
107+
108+
// Then
109+
XCTAssert(authorIdInitial == "test")
110+
XCTAssert(authorIdInitial == authorIdAfterClear)
111+
}
112+
}

0 commit comments

Comments
 (0)