2023-03-02 19:24:10 +01:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2022-2024 New Vector Ltd.
|
2023-03-02 19:24:10 +01:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2023-03-02 19:24:10 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
@testable import ElementX
|
2023-07-07 15:08:32 +01:00
|
|
|
import XCTest
|
2023-03-02 19:24:10 +01:00
|
|
|
|
|
|
|
@MainActor
|
|
|
|
class ReportContentScreenViewModelTests: XCTestCase {
|
2023-07-12 12:28:41 +02:00
|
|
|
let eventID = "test-id"
|
2023-04-03 10:21:24 +01:00
|
|
|
let senderID = "@meany:server.com"
|
|
|
|
let reportReason = "I don't like it."
|
|
|
|
|
2023-07-07 15:08:32 +01:00
|
|
|
func testReportContent() async throws {
|
2023-04-03 10:21:24 +01:00
|
|
|
// Given the report content view for some content.
|
2024-08-20 16:13:27 +03:00
|
|
|
let roomProxy = JoinedRoomProxyMock(.init(name: "test"))
|
2023-04-03 10:21:24 +01:00
|
|
|
roomProxy.reportContentReasonReturnValue = .success(())
|
2024-03-05 14:08:34 +00:00
|
|
|
let clientProxy = ClientProxyMock(.init())
|
2023-07-12 12:28:41 +02:00
|
|
|
let viewModel = ReportContentScreenViewModel(eventID: eventID,
|
2023-04-24 18:48:07 +03:00
|
|
|
senderID: senderID,
|
2024-03-05 14:08:34 +00:00
|
|
|
roomProxy: roomProxy,
|
|
|
|
clientProxy: clientProxy)
|
2023-04-03 10:21:24 +01:00
|
|
|
|
|
|
|
// When reporting the content without ignoring the user.
|
|
|
|
viewModel.state.bindings.reasonText = reportReason
|
|
|
|
viewModel.state.bindings.ignoreUser = false
|
|
|
|
viewModel.context.send(viewAction: .submit)
|
2023-04-05 17:07:12 +02:00
|
|
|
|
2023-09-26 13:28:29 +03:00
|
|
|
let deferred = deferFulfillment(viewModel.actions) { action in
|
|
|
|
action == .submitFinished
|
|
|
|
}
|
|
|
|
|
|
|
|
try await deferred.fulfill()
|
2023-07-07 15:08:32 +01:00
|
|
|
|
2023-04-03 10:21:24 +01:00
|
|
|
// Then the content should be reported, but the user should not be included.
|
|
|
|
XCTAssertEqual(roomProxy.reportContentReasonCallsCount, 1, "The content should always be reported.")
|
2023-07-12 12:28:41 +02:00
|
|
|
XCTAssertEqual(roomProxy.reportContentReasonReceivedArguments?.eventID, eventID, "The event ID should match the content being reported.")
|
2023-04-03 10:21:24 +01:00
|
|
|
XCTAssertEqual(roomProxy.reportContentReasonReceivedArguments?.reason, reportReason, "The reason should match the user input.")
|
2024-03-05 14:08:34 +00:00
|
|
|
XCTAssertEqual(clientProxy.ignoreUserCallsCount, 0, "A call to ignore a user should not have been made.")
|
|
|
|
XCTAssertNil(clientProxy.ignoreUserReceivedUserID, "The sender shouldn't have been ignored.")
|
2023-04-03 10:21:24 +01:00
|
|
|
}
|
|
|
|
|
2023-07-11 10:08:27 +02:00
|
|
|
func testReportIgnoringSender() async throws {
|
2023-04-03 10:21:24 +01:00
|
|
|
// Given the report content view for some content.
|
2024-08-20 16:13:27 +03:00
|
|
|
let roomProxy = JoinedRoomProxyMock(.init(name: "test"))
|
2023-04-03 10:21:24 +01:00
|
|
|
roomProxy.reportContentReasonReturnValue = .success(())
|
2024-03-05 14:08:34 +00:00
|
|
|
let clientProxy = ClientProxyMock(.init())
|
2023-07-12 12:28:41 +02:00
|
|
|
let viewModel = ReportContentScreenViewModel(eventID: eventID,
|
2023-04-24 18:48:07 +03:00
|
|
|
senderID: senderID,
|
2024-03-05 14:08:34 +00:00
|
|
|
roomProxy: roomProxy,
|
|
|
|
clientProxy: clientProxy)
|
2023-04-03 10:21:24 +01:00
|
|
|
|
|
|
|
// When reporting the content and also ignoring the user.
|
|
|
|
viewModel.state.bindings.reasonText = reportReason
|
|
|
|
viewModel.state.bindings.ignoreUser = true
|
2023-07-11 10:08:27 +02:00
|
|
|
|
2023-04-03 10:21:24 +01:00
|
|
|
viewModel.context.send(viewAction: .submit)
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
let deferred = deferFulfillment(viewModel.actions) { action in
|
|
|
|
action == .submitFinished
|
|
|
|
}
|
|
|
|
|
|
|
|
try await deferred.fulfill()
|
2023-04-03 10:21:24 +01:00
|
|
|
|
|
|
|
// Then the content should be reported, and the user should be ignored.
|
|
|
|
XCTAssertEqual(roomProxy.reportContentReasonCallsCount, 1, "The content should always be reported.")
|
2023-07-12 12:28:41 +02:00
|
|
|
XCTAssertEqual(roomProxy.reportContentReasonReceivedArguments?.eventID, eventID, "The event ID should match the content being reported.")
|
2023-04-03 10:21:24 +01:00
|
|
|
XCTAssertEqual(roomProxy.reportContentReasonReceivedArguments?.reason, reportReason, "The reason should match the user input.")
|
2024-03-05 14:08:34 +00:00
|
|
|
XCTAssertEqual(clientProxy.ignoreUserCallsCount, 1, "A call should have been made to ignore the sender.")
|
|
|
|
XCTAssertEqual(clientProxy.ignoreUserReceivedUserID, senderID, "The ignored user ID should match the sender.")
|
2023-03-02 19:24:10 +01:00
|
|
|
}
|
|
|
|
}
|