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

Commit

Permalink
[iOS] getMentionsState function + tests (#850)
Browse files Browse the repository at this point in the history
* added the getMentionsState function

* fixed a test

* lint and format

* pr suggestion
  • Loading branch information
Velin92 authored Oct 20, 2023
1 parent 9d60d40 commit 28de74e
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ final class ComposerModelWrapper: ComposerModelWrapperProtocol {
func getLinkAction() -> LinkAction {
model.getLinkAction()
}

func getMentionsState() -> MentionsState {
model.getMentionsState()
}

// MARK: Extensions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ public extension WysiwygComposerViewModel {
func getLinkAction() -> LinkAction {
model.getLinkAction()
}

/// Get the current mentions present in the composer
func getMentionsState() -> MentionsState {
model.getMentionsState()
}

func enter() {
applyUpdate(createEnterUpdate(), skipTextViewUpdate: false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
// Copyright 2023 The Matrix.org Foundation C.I.C
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

@testable import WysiwygComposer
import XCTest

extension WysiwygComposerViewModelTests {
func testSetAtRooMentionsState() {
viewModel.setAtRoomMention()
XCTAssertEqual(viewModel.getMentionsState(), MentionsState(userIds: [], roomIds: [], roomAliases: [], hasAtRoomMention: true))
}

func testAtRooMentionsStateBySettingContent() {
viewModel.setHtmlContent("@room")
XCTAssertEqual(viewModel.getMentionsState(), MentionsState(userIds: [], roomIds: [], roomAliases: [], hasAtRoomMention: true))
}

func testMentionsStatBySettingUserMention() {
viewModel.setMention(url: "https://matrix.to/#/@alice:matrix.org", name: "Alice", mentionType: .user)
XCTAssertEqual(viewModel.getMentionsState(),
MentionsState(userIds: ["@alice:matrix.org"], roomIds: [], roomAliases: [], hasAtRoomMention: false))
}

func testMentionsStateBySettingUserMentionFromContent() {
let result = MentionsState(userIds: ["@alice:matrix.org"], roomIds: [], roomAliases: [], hasAtRoomMention: false)
viewModel.setHtmlContent("<a href=\"https://matrix.to/#/@alice:matrix.org\">Alice</a>")
XCTAssertEqual(viewModel.getMentionsState(), result)

viewModel.setMarkdownContent("[Alice](https://matrix.to/#/@alice:matrix.org)")
XCTAssertEqual(viewModel.getMentionsState(), result)
}

func testMentionsStatBySettingRoomAliasMention() {
viewModel.setMention(url: "https://matrix.to/#/#room:matrix.org", name: "Room", mentionType: .room)
XCTAssertEqual(viewModel.getMentionsState(),
MentionsState(userIds: [], roomIds: [], roomAliases: ["#room:matrix.org"], hasAtRoomMention: false))
}

func testMentionsStateBySettingRoomAliasMentionFromContent() {
let result = MentionsState(userIds: [], roomIds: [], roomAliases: ["#room:matrix.org"], hasAtRoomMention: false)
viewModel.setHtmlContent("<a href=\"https://matrix.to/#/#room:matrix.org\">Room</a>")
XCTAssertEqual(viewModel.getMentionsState(), result)

viewModel.setMarkdownContent("[Room](https://matrix.to/#/#room:matrix.org)")
XCTAssertEqual(viewModel.getMentionsState(), result)
}

func testMentionsStatBySettingRoomIDMention() {
viewModel.setMention(url: "https://matrix.to/#/!room:matrix.org", name: "Room", mentionType: .room)
XCTAssertEqual(viewModel.getMentionsState(), MentionsState(userIds: [], roomIds: ["!room:matrix.org"], roomAliases: [], hasAtRoomMention: false))
}

func testMentionsStateBySettingRoomIDMentionFromContent() {
let result = MentionsState(userIds: [], roomIds: ["!room:matrix.org"], roomAliases: [], hasAtRoomMention: false)
viewModel.setHtmlContent("<a href=\"https://matrix.to/#/!room:matrix.org\">Room</a>")
XCTAssertEqual(viewModel.getMentionsState(), result)

viewModel.setMarkdownContent("[Room](https://matrix.to/#/!room:matrix.org)")
XCTAssertEqual(viewModel.getMentionsState(), result)
}

func testMultipleMentionsBySettingThemIndividually() {
viewModel.setMention(url: "https://matrix.to/#/@alice:matrix.org", name: "Alice", mentionType: .user)
viewModel.setMention(url: "https://matrix.to/#/@bob:matrix.org", name: "Bob", mentionType: .user)
viewModel.setAtRoomMention()

let mentionsState = viewModel.getMentionsState()
XCTAssertEqual(mentionsState.userIds.count, 2)
XCTAssertEqual(Set(mentionsState.userIds), ["@alice:matrix.org", "@bob:matrix.org"])
XCTAssertTrue(mentionsState.hasAtRoomMention)
XCTAssertTrue(mentionsState.roomIds.isEmpty)
XCTAssertTrue(mentionsState.roomAliases.isEmpty)
}

func testMultipleDuplicateMentionsBySettingThemIndividually() {
viewModel.setMention(url: "https://matrix.to/#/@alice:matrix.org", name: "Alice", mentionType: .user)
viewModel.setMention(url: "https://matrix.to/#/@alice:matrix.org", name: "Alice", mentionType: .user)

XCTAssertEqual(viewModel.getMentionsState(),
MentionsState(userIds: ["@alice:matrix.org"], roomIds: [], roomAliases: [], hasAtRoomMention: false))
}

func testMultipleMentionsBySettingThemWithHtmlContent() {
viewModel.setHtmlContent(
"""
<p><a href=\"https://matrix.to/#/@alice:matrix.org\">Alice</a>, \
<a href=\"https://matrix.to/#/!room:matrix.org\">Room</a>, \
<a href=\"https://matrix.to/#/@bob:matrix.org\">Bob</a>, \
<a href=\"https://matrix.to/#/#room:matrix.org\">Room</a>, \
@room</p>
"""
)
let mentionState = viewModel.getMentionsState()
XCTAssertEqual(Set(mentionState.userIds), ["@alice:matrix.org", "@bob:matrix.org"])
XCTAssertEqual(mentionState.roomAliases, ["#room:matrix.org"])
XCTAssertEqual(mentionState.roomIds, ["!room:matrix.org"])
XCTAssertTrue(mentionState.hasAtRoomMention)
}

func testMultipleMentionsBySettingThemWithMarkdownContent() {
viewModel.setMarkdownContent(
"""
[Room](https://matrix.to/#/!room:matrix.org), \
[Room](https://matrix.to/#/#room:matrix.org), \
[Alice](https://matrix.to/#/@alice:matrix.org), \
[Bob](https://matrix.to/#/@bob:matrix.org), \
@room
"""
)
let mentionState = viewModel.getMentionsState()
XCTAssertEqual(Set(mentionState.userIds), ["@alice:matrix.org", "@bob:matrix.org"])
XCTAssertEqual(mentionState.roomAliases, ["#room:matrix.org"])
XCTAssertEqual(mentionState.roomIds, ["!room:matrix.org"])
XCTAssertTrue(mentionState.hasAtRoomMention)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ extension WysiwygComposerViewModelTests {
)
}


func testAtMentionWithNoSuggestionAtLeading() {
_ = viewModel.replaceText(range: .zero, replacementText: "Text")
viewModel.select(range: .init(location: 0, length: 0))
Expand Down

0 comments on commit 28de74e

Please sign in to comment.