Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
[iOS] expose the set at room mention function (#843)
Browse files Browse the repository at this point in the history
* expose the set at room mention function

* code improvement

* Unit Tests

* more tests
  • Loading branch information
Velin92 committed Oct 12, 2023
1 parent 450db53 commit cb43c9e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ final class ComposerModelWrapper: ComposerModelWrapperProtocol {
func insertMentionAtSuggestion(url: String, text: String, suggestion: SuggestionPattern, attributes: [Attribute]) -> ComposerUpdate {
execute { try $0.insertMentionAtSuggestion(url: url, text: text, suggestion: suggestion, attributes: attributes) }
}

func insertAtRoomMention() -> ComposerUpdate {
execute { try $0.insertAtRoomMention() }
}

func insertAtRoomMentionAtSuggestion(_ suggestion: SuggestionPattern) -> ComposerUpdate {
execute { try $0.insertAtRoomMentionAtSuggestion(suggestion: suggestion) }
}

func removeLinks() -> ComposerUpdate {
execute { try $0.removeLinks() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ public extension WysiwygComposerViewModel {
applyUpdate(update)
hasPendingFormats = true
}

/// Sets the @room mention at the suggestion position
func setAtRoomMention() {
let update: ComposerUpdate
if let suggestionPattern, suggestionPattern.key == .at {
update = model.insertAtRoomMentionAtSuggestion(suggestionPattern)
} else {
update = model.insertAtRoomMention()
}
applyUpdate(update)
hasPendingFormats = true
}

/// Set a command with `Slash` pattern.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ extension WysiwygComposerViewModelTests {
"""
)
}

func testAtRoomSuggestionCanBeUsed() {
_ = viewModel.replaceText(range: .zero, replacementText: "@ro")
viewModel.setAtRoomMention()
XCTAssertEqual(
viewModel.content.html,
"""
@room\u{00A0}
"""
)
}

func testAtMentionWithNoSuggestion() {
_ = viewModel.replaceText(range: .zero, replacementText: "Text")
Expand All @@ -72,6 +83,21 @@ extension WysiwygComposerViewModelTests {
"""
)
}

func testAtRoomMentionWithNoSuggestion() {
_ = viewModel.replaceText(range: .zero, replacementText: "Text")
viewModel.select(range: .init(location: 0, length: 4))
viewModel.setAtRoomMention()
// Text is not removed, and the
// mention is added after the text
XCTAssertEqual(
viewModel.content.html,
"""
Text@room\u{00A0}
"""
)
}


func testAtMentionWithNoSuggestionAtLeading() {
_ = viewModel.replaceText(range: .zero, replacementText: "Text")
Expand All @@ -85,6 +111,19 @@ extension WysiwygComposerViewModelTests {
"""
)
}

func testAtRoomMentionWithNoSuggestionAtLeading() {
_ = viewModel.replaceText(range: .zero, replacementText: "Text")
viewModel.select(range: .init(location: 0, length: 0))
viewModel.setAtRoomMention()
// Text is not removed, and the mention is added before the text
XCTAssertEqual(
viewModel.content.html,
"""
@roomText
"""
)
}

func testHashSuggestionCanBeUsed() {
_ = viewModel.replaceText(range: .zero, replacementText: "#roo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,40 @@ extension WysiwygComposerTests {
)
.assertSelection(start: 8, end: 8)
}

func testSuggestionForAtRoomPattern() {
let model = ComposerModelWrapper()
let update = model.replaceText(newText: "@roo")

guard case .suggestion(suggestionPattern: let suggestionPattern) = update.menuAction() else {
XCTFail("No user suggestion found")
return
}

model
.action {
$0.insertAtRoomMentionAtSuggestion(suggestionPattern)
}
.assertHtml("<a data-mention-type=\"at-room\" href=\"#\" contenteditable=\"false\">@room</a> ")
.assertSelection(start: 2, end: 2)
}

func testForNonLeadingSuggestionForAtRoomPattern() {
let model = ComposerModelWrapper()
let update = model.replaceText(newText: "Hello @roo")

guard case .suggestion(suggestionPattern: let suggestionPattern) = update.menuAction() else {
XCTFail("No user suggestion found")
return
}

model
.action {
$0.insertAtRoomMentionAtSuggestion(suggestionPattern)
}
.assertHtml("Hello <a data-mention-type=\"at-room\" href=\"#\" contenteditable=\"false\">@room</a> ")
.assertSelection(start: 8, end: 8)
}

func testSuggestionForHashPattern() {
let model = ComposerModelWrapper()
Expand Down

0 comments on commit cb43c9e

Please sign in to comment.