From 28de74ee3724ecef1ed509741c8156c486381474 Mon Sep 17 00:00:00 2001 From: Mauro <34335419+Velin92@users.noreply.github.com> Date: Fri, 20 Oct 2023 11:09:46 +0200 Subject: [PATCH] [iOS] getMentionsState function + tests (#850) * added the getMentionsState function * fixed a test * lint and format * pr suggestion --- .../Components/ComposerModelWrapper.swift | 4 + .../WysiwygComposerViewModel.swift | 5 + ...ComposerViewModelTests+MentionsState.swift | 129 ++++++++++++++++++ ...ygComposerViewModelTests+Suggestions.swift | 1 - 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+MentionsState.swift diff --git a/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/ComposerModelWrapper.swift b/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/ComposerModelWrapper.swift index 039582f17..fb8b4a8ec 100644 --- a/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/ComposerModelWrapper.swift +++ b/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/ComposerModelWrapper.swift @@ -167,6 +167,10 @@ final class ComposerModelWrapper: ComposerModelWrapperProtocol { func getLinkAction() -> LinkAction { model.getLinkAction() } + + func getMentionsState() -> MentionsState { + model.getMentionsState() + } // MARK: Extensions diff --git a/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/WysiwygComposerView/WysiwygComposerViewModel.swift b/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/WysiwygComposerView/WysiwygComposerViewModel.swift index 486ff80cd..b1ba09a20 100644 --- a/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/WysiwygComposerView/WysiwygComposerViewModel.swift +++ b/platforms/ios/lib/WysiwygComposer/Sources/WysiwygComposer/Components/WysiwygComposerView/WysiwygComposerViewModel.swift @@ -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) diff --git a/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+MentionsState.swift b/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+MentionsState.swift new file mode 100644 index 000000000..4eb6571b5 --- /dev/null +++ b/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+MentionsState.swift @@ -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("Alice") + 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("Room") + 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("Room") + 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( + """ +
+ """ + ) + 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) + } +} diff --git a/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+Suggestions.swift b/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+Suggestions.swift index 60266cf31..18cdcff52 100644 --- a/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+Suggestions.swift +++ b/platforms/ios/lib/WysiwygComposer/Tests/WysiwygComposerTests/Components/WysiwygComposerView/WysiwygComposerViewModelTests+Suggestions.swift @@ -98,7 +98,6 @@ extension WysiwygComposerViewModelTests { ) } - func testAtMentionWithNoSuggestionAtLeading() { _ = viewModel.replaceText(range: .zero, replacementText: "Text") viewModel.select(range: .init(location: 0, length: 0))