Skip to content

Commit 81cdf3e

Browse files
added tests for the command handlers
1 parent 968cd33 commit 81cdf3e

File tree

12 files changed

+569
-20
lines changed

12 files changed

+569
-20
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/CommandsHandler.swift

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,58 @@ extension CommandHandler {
9090
/// Model for the composer's commands.
9191
public struct ComposerCommand {
9292
/// Identifier of the command.
93-
let id: String
93+
public let id: String
9494
/// Typing suggestion that invokes the command.
95-
var typingSuggestion: TypingSuggestion
95+
public var typingSuggestion: TypingSuggestion
9696
/// Display info for the command.
97-
let displayInfo: CommandDisplayInfo?
97+
public let displayInfo: CommandDisplayInfo?
9898
/// Whether execution of the command replaces sending of a message.
99-
var replacesMessageSent: Bool = false
99+
public var replacesMessageSent: Bool = false
100+
101+
public init(
102+
id: String,
103+
typingSuggestion: TypingSuggestion,
104+
displayInfo: CommandDisplayInfo?,
105+
replacesMessageSent: Bool = false
106+
) {
107+
self.id = id
108+
self.typingSuggestion = typingSuggestion
109+
self.displayInfo = displayInfo
110+
self.replacesMessageSent = replacesMessageSent
111+
}
100112
}
101113

102114
/// Provides information about the suggestion.
103115
public struct SuggestionInfo {
104116
/// Identifies the suggestion.
105-
let key: String
117+
public let key: String
106118
/// Any value that can be passed to the suggestion.
107-
let value: Any
119+
public let value: Any
120+
121+
public init(key: String, value: Any) {
122+
self.key = key
123+
self.value = value
124+
}
108125
}
109126

110127
/// Display information about a command.
111128
public struct CommandDisplayInfo {
112-
let displayName: String
113-
let icon: UIImage
114-
let format: String
115-
let isInstant: Bool
129+
public let displayName: String
130+
public let icon: UIImage
131+
public let format: String
132+
public let isInstant: Bool
133+
134+
public init(
135+
displayName: String,
136+
icon: UIImage,
137+
format: String,
138+
isInstant: Bool
139+
) {
140+
self.displayName = displayName
141+
self.icon = icon
142+
self.format = format
143+
self.isInstant = isInstant
144+
}
116145
}
117146

118147
/// Main commands handler - decides which commands to invoke.
@@ -123,7 +152,7 @@ public class CommandsHandler: CommandHandler {
123152
public let id: String = "main"
124153
public var displayInfo: CommandDisplayInfo?
125154

126-
init(commands: [CommandHandler]) {
155+
public init(commands: [CommandHandler]) {
127156
self.commands = commands
128157
}
129158

@@ -157,7 +186,7 @@ public class CommandsHandler: CommandHandler {
157186
return handler.showSuggestions(for: command)
158187
}
159188

160-
return StreamChatError.wrongConfig.asFailedPromise()
189+
return StreamChatError.noSuggestionsAvailable.asFailedPromise()
161190
}
162191

163192
public func handleCommand(

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/InstantCommands/GiphyCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public struct GiphyCommandHandler: CommandHandler {
1717

1818
private let typingSuggester: TypingSuggester
1919

20-
init(
20+
public init(
2121
commandSymbol: String,
2222
id: String = "/giphy"
2323
) {

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/InstantCommands/MuteCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class MuteCommandHandler: TwoStepMentionCommand {
1212
@Injected(\.images) private var images
1313
@Injected(\.chatClient) private var chatClient
1414

15-
init(
15+
public init(
1616
channelController: ChatChannelController,
1717
commandSymbol: String,
1818
id: String = "/mute"

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/InstantCommands/TwoStepMentionCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ open class TwoStepMentionCommand: CommandHandler {
2222
public var displayInfo: CommandDisplayInfo?
2323
public let replacesMessageSending: Bool = true
2424

25-
init(
25+
public init(
2626
channelController: ChatChannelController,
2727
commandSymbol: String,
2828
id: String,

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/InstantCommands/UnmuteCommandHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class UnmuteCommandHandler: TwoStepMentionCommand {
1212
@Injected(\.images) private var images
1313
@Injected(\.chatClient) private var chatClient
1414

15-
init(
15+
public init(
1616
channelController: ChatChannelController,
1717
commandSymbol: String,
1818
id: String = "/unmute"

Sources/StreamChatSwiftUI/ChatChannel/Composer/Suggestions/Mentions/MentionsCommandHandler.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public struct MentionsCommandHandler: CommandHandler {
1818
private let channelController: ChatChannelController
1919
private let userSearchController: ChatUserSearchController
2020

21-
init(
21+
public init(
2222
channelController: ChatChannelController,
23+
userSearchController: ChatUserSearchController? = nil,
2324
commandSymbol: String,
2425
mentionAllAppUsers: Bool,
2526
id: String = "mentions"
@@ -28,7 +29,11 @@ public struct MentionsCommandHandler: CommandHandler {
2829
self.channelController = channelController
2930
self.mentionAllAppUsers = mentionAllAppUsers
3031
typingSuggester = TypingSuggester(options: .init(symbol: commandSymbol))
31-
userSearchController = channelController.client.userSearchController()
32+
if let userSearchController = userSearchController {
33+
self.userSearchController = userSearchController
34+
} else {
35+
self.userSearchController = channelController.client.userSearchController()
36+
}
3237
}
3338

3439
public func canHandleCommand(in text: String, caretLocation: Int) -> ComposerCommand? {

Sources/StreamChatSwiftUI/Utils/Errors.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public struct StreamChatError: Error {
3333
description: nil,
3434
additionalInfo: nil
3535
)
36+
37+
static let noSuggestionsAvailable = StreamChatError(
38+
errorCode: StreamChatErrorCode.noSuggestions,
39+
description: nil,
40+
additionalInfo: nil
41+
)
3642
}
3743

3844
extension StreamChatError: Equatable {
@@ -56,4 +62,5 @@ public enum StreamChatErrorCode: Int {
5662
case unknown = 101_000
5763
case missingData = 101_001
5864
case wrongConfig = 101_002
65+
case noSuggestions = 101_003
5966
}

StreamChatSwiftUI.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
8423C33D277C94F30092DCF1 /* TwoStepMentionCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8423C33C277C94F30092DCF1 /* TwoStepMentionCommand.swift */; };
2121
8423C33F277C9A5F0092DCF1 /* UnmuteCommandHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8423C33E277C9A5F0092DCF1 /* UnmuteCommandHandler.swift */; };
2222
8423C342277CBA280092DCF1 /* TypingSuggester_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8423C341277CBA280092DCF1 /* TypingSuggester_Tests.swift */; };
23+
8423C344277CC5020092DCF1 /* CommandsHandler_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8423C343277CC5020092DCF1 /* CommandsHandler_Tests.swift */; };
24+
8423C346277D9BFF0092DCF1 /* TestCommandsConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8423C345277D9BFF0092DCF1 /* TestCommandsConfig.swift */; };
25+
8423C348277DBBDA0092DCF1 /* InstantCommandsHandler_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8423C347277DBBDA0092DCF1 /* InstantCommandsHandler_Tests.swift */; };
2326
842F0BB8276B3518002C400C /* QuotedMessageView_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842F0BB7276B3518002C400C /* QuotedMessageView_Tests.swift */; };
2427
84335014274BAB15007A1B81 /* ViewFactoryExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84335013274BAB15007A1B81 /* ViewFactoryExamples.swift */; };
2528
84335016274BABF3007A1B81 /* NewChatView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84335015274BABF3007A1B81 /* NewChatView.swift */; };
@@ -315,6 +318,9 @@
315318
8423C33C277C94F30092DCF1 /* TwoStepMentionCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwoStepMentionCommand.swift; sourceTree = "<group>"; };
316319
8423C33E277C9A5F0092DCF1 /* UnmuteCommandHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnmuteCommandHandler.swift; sourceTree = "<group>"; };
317320
8423C341277CBA280092DCF1 /* TypingSuggester_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypingSuggester_Tests.swift; sourceTree = "<group>"; };
321+
8423C343277CC5020092DCF1 /* CommandsHandler_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandsHandler_Tests.swift; sourceTree = "<group>"; };
322+
8423C345277D9BFF0092DCF1 /* TestCommandsConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestCommandsConfig.swift; sourceTree = "<group>"; };
323+
8423C347277DBBDA0092DCF1 /* InstantCommandsHandler_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstantCommandsHandler_Tests.swift; sourceTree = "<group>"; };
318324
842E979C275E0AD000A52E7B /* StreamChatSwiftUI.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = StreamChatSwiftUI.xctestplan; sourceTree = "<group>"; };
319325
842F0BB7276B3518002C400C /* QuotedMessageView_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuotedMessageView_Tests.swift; sourceTree = "<group>"; };
320326
84335013274BAB15007A1B81 /* ViewFactoryExamples.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewFactoryExamples.swift; sourceTree = "<group>"; };
@@ -614,6 +620,9 @@
614620
isa = PBXGroup;
615621
children = (
616622
8423C341277CBA280092DCF1 /* TypingSuggester_Tests.swift */,
623+
8423C343277CC5020092DCF1 /* CommandsHandler_Tests.swift */,
624+
8423C347277DBBDA0092DCF1 /* InstantCommandsHandler_Tests.swift */,
625+
8423C345277D9BFF0092DCF1 /* TestCommandsConfig.swift */,
617626
);
618627
path = Suggestions;
619628
sourceTree = "<group>";
@@ -1538,6 +1547,7 @@
15381547
84C94CDD27578B92007FE2B9 /* MemberPayload.swift in Sources */,
15391548
84C94D1127578BF2007FE2B9 /* ChannelId.swift in Sources */,
15401549
84AB7B21277203EF00631A10 /* GalleryView_Tests.swift in Sources */,
1550+
8423C348277DBBDA0092DCF1 /* InstantCommandsHandler_Tests.swift in Sources */,
15411551
84C94CD927578B92007FE2B9 /* ChatChannelMember_Mock.swift in Sources */,
15421552
842383E02767394200888CFC /* ChatChannelDataSource_Tests.swift in Sources */,
15431553
84DEC8DF2760A1D100172876 /* MessageView_Tests.swift in Sources */,
@@ -1548,6 +1558,7 @@
15481558
84C94CE227578B92007FE2B9 /* UserPayload.swift in Sources */,
15491559
84C94D3D27579BB0007FE2B9 /* TestDataModel2.xcdatamodeld in Sources */,
15501560
84C94CD327578B92007FE2B9 /* ChatMessageFileAttachment_Mock.swift in Sources */,
1561+
8423C346277D9BFF0092DCF1 /* TestCommandsConfig.swift in Sources */,
15511562
84C94CE827578B92007FE2B9 /* ChatMessageController_Mock.swift in Sources */,
15521563
84C94CD127578B92007FE2B9 /* CurrentChatUser_Mock.swift in Sources */,
15531564
84C94D0E27578BF2007FE2B9 /* AssertAsync.swift in Sources */,
@@ -1556,6 +1567,7 @@
15561567
84C94CDF27578B92007FE2B9 /* MessagePayload.swift in Sources */,
15571568
84C94CD527578B92007FE2B9 /* ChatMessageImageAttachment_Mock.swift in Sources */,
15581569
84C94C8027567D3F007FE2B9 /* ChatChannelListViewModel_Tests.swift in Sources */,
1570+
8423C344277CC5020092DCF1 /* CommandsHandler_Tests.swift in Sources */,
15591571
84C94CE727578B92007FE2B9 /* ChatChannelListController_Mock.swift in Sources */,
15601572
84C94D1027578BF2007FE2B9 /* TestDataModel2.xcdatamodeld in Sources */,
15611573
84C94CE627578B92007FE2B9 /* CurrentChatUserController_Mock.swift in Sources */,

StreamChatSwiftUITests/Tests/ChatChannel/ChatChannelTestHelpers.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ class ChatChannelTestHelpers {
1010

1111
static func makeChannelController(
1212
chatClient: ChatClient,
13-
messages: [ChatMessage] = []
13+
messages: [ChatMessage] = [],
14+
lastActiveWatchers: [ChatUser] = []
1415
) -> ChatChannelController_Mock {
15-
let channel = ChatChannel.mockDMChannel()
16+
let channel = ChatChannel.mockDMChannel(lastActiveWatchers: lastActiveWatchers)
1617
let channelQuery = ChannelQuery(cid: channel.cid)
1718
let channelListQuery = ChannelListQuery(filter: .containMembers(userIds: [chatClient.currentUserId!]))
1819
let channelController = ChatChannelController_Mock(

0 commit comments

Comments
 (0)