Skip to content

Commit 48a58db

Browse files
added tests for injecting utils
1 parent 3d8ac3f commit 48a58db

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@
215215
84C94D54275A1380007FE2B9 /* DateUtils_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D53275A1380007FE2B9 /* DateUtils_Tests.swift */; };
216216
84C94D56275A1AE1007FE2B9 /* StringExtensions_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D55275A1AE1007FE2B9 /* StringExtensions_Tests.swift */; };
217217
84C94D58275A1B89007FE2B9 /* MessageTypeResolver_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D57275A1B89007FE2B9 /* MessageTypeResolver_Tests.swift */; };
218+
84C94D5A275A2E43007FE2B9 /* StreamChat_Utils_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D59275A2E43007FE2B9 /* StreamChat_Utils_Tests.swift */; };
219+
84C94D5C275A2E9F007FE2B9 /* StreamChatUtilsMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C94D5B275A2E9F007FE2B9 /* StreamChatUtilsMocks.swift */; };
218220
84EDBC37274FE5CD0057218D /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 84EDBC36274FE5CD0057218D /* Localizable.strings */; };
219221
/* End PBXBuildFile section */
220222

@@ -460,6 +462,8 @@
460462
84C94D53275A1380007FE2B9 /* DateUtils_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateUtils_Tests.swift; sourceTree = "<group>"; };
461463
84C94D55275A1AE1007FE2B9 /* StringExtensions_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringExtensions_Tests.swift; sourceTree = "<group>"; };
462464
84C94D57275A1B89007FE2B9 /* MessageTypeResolver_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageTypeResolver_Tests.swift; sourceTree = "<group>"; };
465+
84C94D59275A2E43007FE2B9 /* StreamChat_Utils_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamChat_Utils_Tests.swift; sourceTree = "<group>"; };
466+
84C94D5B275A2E9F007FE2B9 /* StreamChatUtilsMocks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StreamChatUtilsMocks.swift; sourceTree = "<group>"; };
463467
84EDBC36274FE5CD0057218D /* Localizable.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; path = Localizable.strings; sourceTree = "<group>"; };
464468
/* End PBXFileReference section */
465469

@@ -927,6 +931,7 @@
927931
84C94D29275796D0007FE2B9 /* MockNetworkURLProtocol.swift */,
928932
84C94D2B275796F7007FE2B9 /* RequestRecorderURLProtocol.swift */,
929933
84C94D452757D1CA007FE2B9 /* ImageLoader_Mock.swift */,
934+
84C94D5B275A2E9F007FE2B9 /* StreamChatUtilsMocks.swift */,
930935
);
931936
path = Mocks;
932937
sourceTree = "<group>";
@@ -978,6 +983,7 @@
978983
children = (
979984
84C94D53275A1380007FE2B9 /* DateUtils_Tests.swift */,
980985
84C94D55275A1AE1007FE2B9 /* StringExtensions_Tests.swift */,
986+
84C94D59275A2E43007FE2B9 /* StreamChat_Utils_Tests.swift */,
981987
);
982988
path = Utils;
983989
sourceTree = "<group>";
@@ -1312,6 +1318,7 @@
13121318
84C94D3C27579BB0007FE2B9 /* AssertAsync.swift in Sources */,
13131319
84C94D0727578BF2007FE2B9 /* RandomDispatchQueue.swift in Sources */,
13141320
84C94D2227579359007FE2B9 /* AnyEndpoint.swift in Sources */,
1321+
84C94D5C275A2E9F007FE2B9 /* StreamChatUtilsMocks.swift in Sources */,
13151322
84C94D1227578BF2007FE2B9 /* JSONEncoder+Extensions.swift in Sources */,
13161323
84C94D24275794D3007FE2B9 /* InternetConnectionMock.swift in Sources */,
13171324
84C94CDD27578B92007FE2B9 /* MemberPayload.swift in Sources */,
@@ -1347,6 +1354,7 @@
13471354
84C94D0427578BF2007FE2B9 /* TestError.swift in Sources */,
13481355
84C94D0A27578BF2007FE2B9 /* TestDataModel.xcdatamodeld in Sources */,
13491356
84C94D422757C16D007FE2B9 /* ChatChannelListTestHelpers.swift in Sources */,
1357+
84C94D5A275A2E43007FE2B9 /* StreamChat_Utils_Tests.swift in Sources */,
13501358
);
13511359
runOnlyForDeploymentPostprocessing = 0;
13521360
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright © 2021 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import StreamChatSwiftUI
6+
import UIKit
7+
8+
/// Mock implementation of `VideoPreviewLoader`.
9+
class VideoPreviewLoader_Mock: VideoPreviewLoader {
10+
11+
var loadPreviewVideoCalled = false
12+
13+
func loadPreviewForVideo(at url: URL, completion: @escaping (Result<UIImage, Error>) -> Void) {
14+
loadPreviewVideoCalled = true
15+
}
16+
17+
}
18+
19+
/// Mock implementation of `ImageLoading`.
20+
class ImageLoaderUtils_Mock: ImageLoading {
21+
22+
var loadImageCalled = false
23+
24+
func loadImage(
25+
url: URL?,
26+
imageCDN: ImageCDN,
27+
resize: Bool,
28+
preferredSize: CGSize?,
29+
completion: @escaping ((Result<UIImage, Error>) -> Void)
30+
) {
31+
loadImageCalled = true
32+
}
33+
34+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// Copyright © 2021 Stream.io Inc. All rights reserved.
3+
//
4+
5+
import Foundation
6+
@testable import StreamChat
7+
@testable import StreamChatSwiftUI
8+
import XCTest
9+
10+
class StreamChat_Utils_Tests: XCTestCase {
11+
12+
private let testURL = URL(string: "https://example.com")!
13+
14+
var streamChat: StreamChat!
15+
16+
@Injected(\.utils) var utils
17+
18+
private var chatClient: ChatClient = {
19+
let client = ChatClient.mock()
20+
client.currentUserId = .unique
21+
return client
22+
}()
23+
24+
override func setUp() {
25+
let utils = Utils(
26+
videoPreviewLoader: VideoPreviewLoader_Mock(),
27+
imageLoader: ImageLoaderUtils_Mock()
28+
)
29+
streamChat = StreamChat(chatClient: chatClient, utils: utils)
30+
}
31+
32+
func test_streamChatUtils_injectVideoPreviewLoader() {
33+
// Given
34+
let videoPreviewLoader = utils.videoPreviewLoader as! VideoPreviewLoader_Mock
35+
36+
// When
37+
videoPreviewLoader.loadPreviewForVideo(at: testURL, completion: { _ in })
38+
39+
// Then
40+
XCTAssert(videoPreviewLoader.loadPreviewVideoCalled == true)
41+
}
42+
43+
func test_streamChatUtils_injectImageLoader() {
44+
// Given
45+
let imageLoader = utils.imageLoader as! ImageLoaderUtils_Mock
46+
47+
// When
48+
imageLoader.loadImage(
49+
url: testURL,
50+
imageCDN: utils.imageCDN,
51+
resize: true,
52+
preferredSize: nil,
53+
completion: {_ in }
54+
)
55+
56+
// Then
57+
XCTAssert(imageLoader.loadImageCalled == true)
58+
}
59+
60+
}

0 commit comments

Comments
 (0)