|
| 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 TypingSuggester_Tests: XCTestCase { |
| 10 | + |
| 11 | + func test_typingSuggester_mentionWholeText() { |
| 12 | + // Given |
| 13 | + let options = TypingSuggestionOptions(symbol: "@") |
| 14 | + let typingSuggester = TypingSuggester(options: options) |
| 15 | + let string = "@Martin" |
| 16 | + let caretLocation = 7 |
| 17 | + |
| 18 | + // When |
| 19 | + let suggestion = typingSuggester.typingSuggestion( |
| 20 | + in: string, |
| 21 | + caretLocation: caretLocation |
| 22 | + ) |
| 23 | + |
| 24 | + // Then |
| 25 | + XCTAssert(suggestion?.text == "Martin") |
| 26 | + XCTAssert(suggestion?.locationRange == NSRange(location: 1, length: 6)) |
| 27 | + } |
| 28 | + |
| 29 | + func test_typingSuggester_mentionMiddleOfText() { |
| 30 | + // Given |
| 31 | + let options = TypingSuggestionOptions(symbol: "@") |
| 32 | + let typingSuggester = TypingSuggester(options: options) |
| 33 | + let string = "@Martin" |
| 34 | + let caretLocation = 5 |
| 35 | + |
| 36 | + // When |
| 37 | + let suggestion = typingSuggester.typingSuggestion( |
| 38 | + in: string, |
| 39 | + caretLocation: caretLocation |
| 40 | + ) |
| 41 | + |
| 42 | + // Then |
| 43 | + XCTAssert(suggestion?.text == "Mart") |
| 44 | + XCTAssert(suggestion?.locationRange == NSRange(location: 1, length: 4)) |
| 45 | + } |
| 46 | + |
| 47 | + func test_typingSuggester_mentionDifferentSymbol() { |
| 48 | + // Given |
| 49 | + let options = TypingSuggestionOptions(symbol: "$") |
| 50 | + let typingSuggester = TypingSuggester(options: options) |
| 51 | + let string = "$cash" |
| 52 | + let caretLocation = 3 |
| 53 | + |
| 54 | + // When |
| 55 | + let suggestion = typingSuggester.typingSuggestion( |
| 56 | + in: string, |
| 57 | + caretLocation: caretLocation |
| 58 | + ) |
| 59 | + |
| 60 | + // Then |
| 61 | + XCTAssert(suggestion?.text == "ca") |
| 62 | + XCTAssert(suggestion?.locationRange == NSRange(location: 1, length: 2)) |
| 63 | + } |
| 64 | + |
| 65 | + func test_typingSuggester_notFoundEmptySpace() { |
| 66 | + // Given |
| 67 | + let options = TypingSuggestionOptions(symbol: "@") |
| 68 | + let typingSuggester = TypingSuggester(options: options) |
| 69 | + let string = "@M art" |
| 70 | + let caretLocation = 3 |
| 71 | + |
| 72 | + // When |
| 73 | + let suggestion = typingSuggester.typingSuggestion( |
| 74 | + in: string, |
| 75 | + caretLocation: caretLocation |
| 76 | + ) |
| 77 | + |
| 78 | + // Then |
| 79 | + XCTAssert(suggestion == nil) |
| 80 | + } |
| 81 | + |
| 82 | + func test_typingSuggester_notFoundNotOnStart() { |
| 83 | + // Given |
| 84 | + let options = TypingSuggestionOptions(symbol: "@") |
| 85 | + let typingSuggester = TypingSuggester(options: options) |
| 86 | + let string = "Hello@User" |
| 87 | + let caretLocation = 7 |
| 88 | + |
| 89 | + // When |
| 90 | + let suggestion = typingSuggester.typingSuggestion( |
| 91 | + in: string, |
| 92 | + caretLocation: caretLocation |
| 93 | + ) |
| 94 | + |
| 95 | + // Then |
| 96 | + XCTAssert(suggestion == nil) |
| 97 | + } |
| 98 | + |
| 99 | + func test_typingSuggester_minimumNumberOfCharacters() { |
| 100 | + // Given |
| 101 | + let options = TypingSuggestionOptions( |
| 102 | + symbol: "@", |
| 103 | + minimumRequiredCharacters: 5 |
| 104 | + ) |
| 105 | + let typingSuggester = TypingSuggester(options: options) |
| 106 | + let string = "@Mar" |
| 107 | + let caretLocation = 4 |
| 108 | + |
| 109 | + // When |
| 110 | + let suggestion = typingSuggester.typingSuggestion( |
| 111 | + in: string, |
| 112 | + caretLocation: caretLocation |
| 113 | + ) |
| 114 | + |
| 115 | + // Then |
| 116 | + XCTAssert(suggestion == nil) |
| 117 | + } |
| 118 | + |
| 119 | + func test_typingSuggester_notOnlyOnStartAllowed() { |
| 120 | + // Given |
| 121 | + let options = TypingSuggestionOptions(symbol: "@") |
| 122 | + let typingSuggester = TypingSuggester(options: options) |
| 123 | + let string = "Hey @Mar" |
| 124 | + let caretLocation = 8 |
| 125 | + |
| 126 | + // When |
| 127 | + let suggestion = typingSuggester.typingSuggestion( |
| 128 | + in: string, |
| 129 | + caretLocation: caretLocation |
| 130 | + ) |
| 131 | + |
| 132 | + // Then |
| 133 | + XCTAssert(suggestion?.text == "Mar") |
| 134 | + XCTAssert(suggestion?.locationRange == NSRange(location: 5, length: 3)) |
| 135 | + } |
| 136 | + |
| 137 | + func test_typingSuggester_onlyOnStartAllowed() { |
| 138 | + // Given |
| 139 | + let options = TypingSuggestionOptions( |
| 140 | + symbol: "@", |
| 141 | + shouldTriggerOnlyAtStart: true |
| 142 | + ) |
| 143 | + let typingSuggester = TypingSuggester(options: options) |
| 144 | + let string = "Hey @Mar" |
| 145 | + let caretLocation = 8 |
| 146 | + |
| 147 | + // When |
| 148 | + let suggestion = typingSuggester.typingSuggestion( |
| 149 | + in: string, |
| 150 | + caretLocation: caretLocation |
| 151 | + ) |
| 152 | + |
| 153 | + // Then |
| 154 | + XCTAssert(suggestion == nil) |
| 155 | + } |
| 156 | +} |
0 commit comments