|
| 1 | +// |
| 2 | +// Copyright © 2021 Stream.io Inc. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +@testable import StreamChat |
| 6 | +@testable import StreamChatSwiftUI |
| 7 | +import XCTest |
| 8 | + |
| 9 | +class MuteCommandHandler_Tests: XCTestCase { |
| 10 | + |
| 11 | + private var chatClient: ChatClient = { |
| 12 | + let client = ChatClient.mock() |
| 13 | + client.currentUserId = .unique |
| 14 | + return client |
| 15 | + }() |
| 16 | + |
| 17 | + private var streamChat: StreamChat? |
| 18 | + |
| 19 | + override func setUp() { |
| 20 | + super.setUp() |
| 21 | + streamChat = StreamChat(chatClient: chatClient) |
| 22 | + } |
| 23 | + |
| 24 | + func test_muteCommandHandler_selectingUserToMute() { |
| 25 | + // Given |
| 26 | + let symbol = "/mute" |
| 27 | + let muteCommandHandler = makeMuteCommandHandler() |
| 28 | + let command = ComposerCommand( |
| 29 | + id: symbol, |
| 30 | + typingSuggestion: TypingSuggestion( |
| 31 | + text: "@", |
| 32 | + locationRange: NSRange(location: 1, length: 0) |
| 33 | + ), displayInfo: nil |
| 34 | + ) |
| 35 | + |
| 36 | + // When |
| 37 | + muteCommandHandler.handleCommand( |
| 38 | + for: .constant(symbol), |
| 39 | + selectedRangeLocation: .constant(0), |
| 40 | + command: .constant(command), |
| 41 | + extraData: [ |
| 42 | + "chatUser": TestCommandsConfig.mockUsers[0] |
| 43 | + ] |
| 44 | + ) |
| 45 | + let canBeExecuted = muteCommandHandler.canBeExecuted(composerCommand: command) |
| 46 | + |
| 47 | + // Then |
| 48 | + let user = muteCommandHandler.selectedUser |
| 49 | + XCTAssert(user == TestCommandsConfig.mockUsers[0]) |
| 50 | + XCTAssert(canBeExecuted == true) |
| 51 | + } |
| 52 | + |
| 53 | + func test_muteCommandHandler_showSuggestions() { |
| 54 | + // Given |
| 55 | + let muteCommandHandler = makeMuteCommandHandler() |
| 56 | + let command = ComposerCommand( |
| 57 | + id: "/mute", |
| 58 | + typingSuggestion: TypingSuggestion( |
| 59 | + text: "@", |
| 60 | + locationRange: NSRange(location: 1, length: 0) |
| 61 | + ), displayInfo: nil |
| 62 | + ) |
| 63 | + let expectation = expectation(description: "suggestions") |
| 64 | + |
| 65 | + // When |
| 66 | + _ = muteCommandHandler.showSuggestions(for: command).sink { _ in |
| 67 | + log.debug("finished showing suggestions") |
| 68 | + } receiveValue: { suggestionInfo in |
| 69 | + // Then |
| 70 | + let users = suggestionInfo.value as! [ChatUser] |
| 71 | + XCTAssert(users.count == 3) |
| 72 | + expectation.fulfill() |
| 73 | + } |
| 74 | + |
| 75 | + waitForExpectations(timeout: 5, handler: nil) |
| 76 | + } |
| 77 | + |
| 78 | + func test_unmuteCommandHandler_selectingUserToUnmute() { |
| 79 | + // Given |
| 80 | + let symbol = "/unmute" |
| 81 | + let channelController = ChatChannelTestHelpers.makeChannelController(chatClient: chatClient) |
| 82 | + let unmuteCommandHandler = UnmuteCommandHandler( |
| 83 | + channelController: channelController, |
| 84 | + commandSymbol: symbol |
| 85 | + ) |
| 86 | + let command = ComposerCommand( |
| 87 | + id: symbol, |
| 88 | + typingSuggestion: TypingSuggestion( |
| 89 | + text: "@", |
| 90 | + locationRange: NSRange(location: 1, length: 0) |
| 91 | + ), displayInfo: nil |
| 92 | + ) |
| 93 | + |
| 94 | + // When |
| 95 | + unmuteCommandHandler.handleCommand( |
| 96 | + for: .constant(symbol), |
| 97 | + selectedRangeLocation: .constant(0), |
| 98 | + command: .constant(command), |
| 99 | + extraData: [ |
| 100 | + "chatUser": TestCommandsConfig.mockUsers[0] |
| 101 | + ] |
| 102 | + ) |
| 103 | + let canBeExecuted = unmuteCommandHandler.canBeExecuted(composerCommand: command) |
| 104 | + |
| 105 | + // Then |
| 106 | + let user = unmuteCommandHandler.selectedUser |
| 107 | + XCTAssert(user == TestCommandsConfig.mockUsers[0]) |
| 108 | + XCTAssert(canBeExecuted == true) |
| 109 | + } |
| 110 | + |
| 111 | + // MARK: - private |
| 112 | + |
| 113 | + private func makeMuteCommandHandler(symbol: String = "/mute") -> MuteCommandHandler { |
| 114 | + let channelController = ChatChannelTestHelpers.makeChannelController( |
| 115 | + chatClient: chatClient, |
| 116 | + lastActiveWatchers: TestCommandsConfig.mockUsers |
| 117 | + ) |
| 118 | + let muteCommandHandler = MuteCommandHandler( |
| 119 | + channelController: channelController, |
| 120 | + commandSymbol: symbol |
| 121 | + ) |
| 122 | + return muteCommandHandler |
| 123 | + } |
| 124 | +} |
0 commit comments