Add xcDeferFulfillment to help with tests that have publishers. (#1278)

* Add xcAwaitDeferred to aid with tests that have publishers.

* Improve naming

* Address Comments

- Fix naming
- Add optional expectation message
- Use trailing closure syntax
This commit is contained in:
David Langley
2023-07-07 15:08:32 +01:00
committed by GitHub
parent 448db8a8e9
commit 83e27564d5
8 changed files with 92 additions and 24 deletions
@@ -14,9 +14,8 @@
// limitations under the License.
//
import XCTest
@testable import ElementX
import XCTest
@MainActor
class ReportContentScreenViewModelTests: XCTestCase {
@@ -24,7 +23,7 @@ class ReportContentScreenViewModelTests: XCTestCase {
let senderID = "@meany:server.com"
let reportReason = "I don't like it."
func testReportContent() async {
func testReportContent() async throws {
// Given the report content view for some content.
let roomProxy = RoomProxyMock(with: .init(displayName: "test"))
roomProxy.reportContentReasonReturnValue = .success(())
@@ -32,13 +31,16 @@ class ReportContentScreenViewModelTests: XCTestCase {
senderID: senderID,
roomProxy: roomProxy)
let deferred = deferFulfillment(viewModel.actions.collect(2).first(), message: "2 actions should be published.")
// When reporting the content without ignoring the user.
viewModel.state.bindings.reasonText = reportReason
viewModel.state.bindings.ignoreUser = false
viewModel.context.send(viewAction: .submit)
_ = await viewModel.actions.values.first()
let actions = try await deferred.fulfill()
XCTAssertEqual(actions, [.submitStarted, .submitFinished])
// Then the content should be reported, but the user should not be included.
XCTAssertEqual(roomProxy.reportContentReasonCallsCount, 1, "The content should always be reported.")
XCTAssertEqual(roomProxy.reportContentReasonReceivedArguments?.eventID, itemID, "The event ID should match the content being reported.")