mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Improve tests' reliability (#763)
* Create publisher extension into the unit test target * Add ViewModelContext test extension * Refactor BugReportViewModelTests * Fix failing UTs * Idea PublishedClosure * Refactor RoomDetailsViewModelTests * Replace more Task.yield/Task.sleep * Move leaveRoom/ignore/unignore under the @MainActor * Revert "Idea PublishedClosure" This reverts commit 4ab25291041f0dbd99083baf9d95bc6647f1fd97. * Make process(viewAction:) sync * Refactor BugReportViewModel callback to a publisher * Fix UTs * Refactor ReportContentViewModel * Fix ui test build error * Try make sonar happy * Empty commit * Revert "Try make sonar happy" This reverts commit 97804b19373a8f55f12174ccbf27f1fd8db583b7. * Rename ui test identifier * Cleanup * Callback -> actions refactor * Update template * Add publisher in TemplateCoordinator * Add env variable in IntegrationTests.xctestplan * Add async sequence extension * Amend integration test plan * Remove env variable from target.yml * Cleanup * Fix failing UI tests
This commit is contained in:
@@ -46,7 +46,6 @@ class BugReportViewModelTests: XCTestCase {
|
||||
let context = viewModel.context
|
||||
|
||||
context.send(viewAction: .removeScreenshot)
|
||||
try await Task.sleep(nanoseconds: 100_000_000)
|
||||
XCTAssertNil(context.viewState.screenshot)
|
||||
}
|
||||
|
||||
@@ -58,7 +57,6 @@ class BugReportViewModelTests: XCTestCase {
|
||||
let context = viewModel.context
|
||||
XCTAssertNil(context.viewState.screenshot)
|
||||
context.send(viewAction: .attachScreenshot(UIImage.actions))
|
||||
try await Task.sleep(nanoseconds: 100_000_000)
|
||||
XCTAssert(context.viewState.screenshot == UIImage.actions)
|
||||
}
|
||||
|
||||
@@ -70,19 +68,20 @@ class BugReportViewModelTests: XCTestCase {
|
||||
deviceID: nil,
|
||||
screenshot: nil, isModallyPresented: false)
|
||||
let context = viewModel.context
|
||||
var isSuccess = false
|
||||
viewModel.callback = { result in
|
||||
switch result {
|
||||
case .submitFinished:
|
||||
isSuccess = true
|
||||
default: break
|
||||
}
|
||||
}
|
||||
context.send(viewAction: .submit)
|
||||
try await Task.sleep(for: .milliseconds(100))
|
||||
|
||||
_ = await viewModel
|
||||
.actions
|
||||
.values
|
||||
.first {
|
||||
guard case .submitFinished = $0 else {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
XCTAssert(mockService.submitBugReportProgressListenerCallsCount == 1)
|
||||
XCTAssert(mockService.submitBugReportProgressListenerReceivedArguments?.bugReport == BugReport(userID: "@mock.client.com", deviceID: nil, text: "", includeLogs: true, includeCrashLog: true, githubLabels: [], files: []))
|
||||
XCTAssertTrue(isSuccess)
|
||||
}
|
||||
|
||||
func testSendReportWithError() async throws {
|
||||
@@ -95,20 +94,19 @@ class BugReportViewModelTests: XCTestCase {
|
||||
deviceID: nil,
|
||||
screenshot: nil, isModallyPresented: false)
|
||||
let context = viewModel.context
|
||||
var isFailure = false
|
||||
|
||||
viewModel.callback = { result in
|
||||
switch result {
|
||||
case .submitFailed:
|
||||
isFailure = true
|
||||
default: break
|
||||
}
|
||||
}
|
||||
|
||||
context.send(viewAction: .submit)
|
||||
try await Task.sleep(for: .milliseconds(100))
|
||||
|
||||
_ = await viewModel
|
||||
.actions
|
||||
.values
|
||||
.first {
|
||||
guard case .submitFailed = $0 else {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
XCTAssert(mockService.submitBugReportProgressListenerCallsCount == 1)
|
||||
XCTAssert(mockService.submitBugReportProgressListenerReceivedArguments?.bugReport == BugReport(userID: "@mock.client.com", deviceID: nil, text: "", includeLogs: true, includeCrashLog: true, githubLabels: [], files: []))
|
||||
XCTAssertTrue(isFailure)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Copyright 2023 New Vector Ltd
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
extension AsyncSequence {
|
||||
func first() async rethrows -> Self.Element? {
|
||||
try await first { _ in true }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Copyright 2023 New Vector Ltd
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
import Combine
|
||||
|
||||
extension Published.Publisher {
|
||||
/// Returns the next output from the publisher skipping the current value stored into it (which is readable from the @Published property itself).
|
||||
/// - Returns: the next output from the publisher
|
||||
var nextValue: Output? {
|
||||
get async {
|
||||
var iterator = values.makeAsyncIterator()
|
||||
|
||||
// skips the publisher's current value
|
||||
_ = await iterator.next()
|
||||
return await iterator.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Copyright 2023 New Vector Ltd
|
||||
//
|
||||
// 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 ElementX
|
||||
|
||||
extension ViewModelContext {
|
||||
@discardableResult
|
||||
func nextViewState() async -> ViewState? {
|
||||
await $viewState.nextValue
|
||||
}
|
||||
}
|
||||
@@ -23,12 +23,12 @@ class FilePreviewScreenViewModelTests: XCTestCase {
|
||||
var viewModel: FilePreviewViewModelProtocol!
|
||||
var context: FilePreviewViewModelType.Context!
|
||||
|
||||
@MainActor override func setUpWithError() throws {
|
||||
override func setUpWithError() throws {
|
||||
viewModel = FilePreviewViewModel(mediaFile: .unmanaged(url: URL(staticString: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf")))
|
||||
context = viewModel.context
|
||||
}
|
||||
|
||||
@MainActor func testCancel() async throws {
|
||||
func testCancel() async throws {
|
||||
var correctResult = false
|
||||
viewModel.callback = { result in
|
||||
switch result {
|
||||
|
||||
@@ -18,18 +18,19 @@ import XCTest
|
||||
|
||||
@testable import ElementX
|
||||
|
||||
@MainActor
|
||||
class HomeScreenViewModelTests: XCTestCase {
|
||||
var viewModel: HomeScreenViewModelProtocol!
|
||||
var context: HomeScreenViewModelType.Context!
|
||||
|
||||
@MainActor override func setUpWithError() throws {
|
||||
override func setUpWithError() throws {
|
||||
viewModel = HomeScreenViewModel(userSession: MockUserSession(clientProxy: MockClientProxy(userID: "@mock:client.com"),
|
||||
mediaProvider: MockMediaProvider()),
|
||||
attributedStringBuilder: AttributedStringBuilder())
|
||||
context = viewModel.context
|
||||
}
|
||||
|
||||
@MainActor func testSelectRoom() async throws {
|
||||
func testSelectRoom() async throws {
|
||||
let mockRoomId = "mock_room_id"
|
||||
var correctResult = false
|
||||
var selectedRoomId = ""
|
||||
@@ -49,7 +50,7 @@ class HomeScreenViewModelTests: XCTestCase {
|
||||
XCTAssertEqual(mockRoomId, selectedRoomId)
|
||||
}
|
||||
|
||||
@MainActor func testTapUserAvatar() async throws {
|
||||
func testTapUserAvatar() async throws {
|
||||
var correctResult = false
|
||||
viewModel.callback = { result in
|
||||
switch result {
|
||||
|
||||
@@ -36,7 +36,8 @@ class ReportContentScreenViewModelTests: XCTestCase {
|
||||
viewModel.state.bindings.reasonText = reportReason
|
||||
viewModel.state.bindings.ignoreUser = false
|
||||
viewModel.context.send(viewAction: .submit)
|
||||
await Task.yield()
|
||||
|
||||
_ = await viewModel.actions.values.first()
|
||||
|
||||
// Then the content should be reported, but the user should not be included.
|
||||
XCTAssertEqual(roomProxy.reportContentReasonCallsCount, 1, "The content should always be reported.")
|
||||
@@ -59,7 +60,8 @@ class ReportContentScreenViewModelTests: XCTestCase {
|
||||
viewModel.state.bindings.reasonText = reportReason
|
||||
viewModel.state.bindings.ignoreUser = true
|
||||
viewModel.context.send(viewAction: .submit)
|
||||
await Task.yield()
|
||||
|
||||
_ = await viewModel.actions.values.first()
|
||||
|
||||
// Then the content should be reported, and the user should be ignored.
|
||||
XCTAssertEqual(roomProxy.reportContentReasonCallsCount, 1, "The content should always be reported.")
|
||||
|
||||
@@ -34,24 +34,21 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
|
||||
roomProxyMock = RoomProxyMock(with: .init(displayName: "Test", isPublic: true, members: mockedMembers))
|
||||
viewModel = RoomDetailsViewModel(roomProxy: roomProxyMock, mediaProvider: MockMediaProvider())
|
||||
context.send(viewAction: .processTapLeave)
|
||||
await Task.yield()
|
||||
XCTAssertEqual(context.leaveRoomAlertItem?.state, .public)
|
||||
XCTAssertEqual(context.leaveRoomAlertItem?.subtitle, L10n.leaveRoomAlertSubtitle)
|
||||
}
|
||||
|
||||
func testLeavRoomTappedWhenRoomNotPublic() async {
|
||||
func testLeaveRoomTappedWhenRoomNotPublic() async {
|
||||
let mockedMembers: [RoomMemberProxyMock] = [.mockBob, .mockAlice]
|
||||
roomProxyMock = RoomProxyMock(with: .init(displayName: "Test", isPublic: false, members: mockedMembers))
|
||||
viewModel = RoomDetailsViewModel(roomProxy: roomProxyMock, mediaProvider: MockMediaProvider())
|
||||
context.send(viewAction: .processTapLeave)
|
||||
await Task.yield()
|
||||
XCTAssertEqual(context.leaveRoomAlertItem?.state, .private)
|
||||
XCTAssertEqual(context.leaveRoomAlertItem?.subtitle, L10n.leaveRoomAlertPrivateSubtitle)
|
||||
}
|
||||
|
||||
func testLeaveRoomTappedWithLessThanTwoMembers() async {
|
||||
context.send(viewAction: .processTapLeave)
|
||||
await Task.yield()
|
||||
XCTAssertEqual(context.leaveRoomAlertItem?.state, .empty)
|
||||
XCTAssertEqual(context.leaveRoomAlertItem?.subtitle, L10n.leaveRoomAlertEmptySubtitle)
|
||||
}
|
||||
@@ -78,7 +75,7 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
|
||||
.failure(.failedLeavingRoom)
|
||||
}
|
||||
context.send(viewAction: .confirmLeave)
|
||||
await Task.yield()
|
||||
await context.nextViewState()
|
||||
XCTAssertEqual(roomProxyMock.leaveRoomCallsCount, 1)
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
}
|
||||
@@ -103,10 +100,10 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
|
||||
XCTAssertEqual(context.viewState.dmRecipient, RoomMemberDetails(withProxy: recipient))
|
||||
|
||||
context.send(viewAction: .ignoreConfirmed)
|
||||
await Task.yield()
|
||||
await context.nextViewState()
|
||||
|
||||
XCTAssertTrue(context.viewState.isProcessingIgnoreRequest)
|
||||
try await Task.sleep(for: .milliseconds(10))
|
||||
await context.nextViewState()
|
||||
XCTAssertFalse(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssert(context.viewState.dmRecipient?.isIgnored == true)
|
||||
}
|
||||
@@ -123,10 +120,10 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
|
||||
XCTAssertEqual(context.viewState.dmRecipient, RoomMemberDetails(withProxy: recipient))
|
||||
|
||||
context.send(viewAction: .ignoreConfirmed)
|
||||
await Task.yield()
|
||||
await context.nextViewState()
|
||||
|
||||
XCTAssertTrue(context.viewState.isProcessingIgnoreRequest)
|
||||
try await Task.sleep(for: .milliseconds(10))
|
||||
await context.nextViewState()
|
||||
XCTAssertFalse(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssert(context.viewState.dmRecipient?.isIgnored == false)
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
@@ -144,10 +141,10 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
|
||||
XCTAssertEqual(context.viewState.dmRecipient, RoomMemberDetails(withProxy: recipient))
|
||||
|
||||
context.send(viewAction: .unignoreConfirmed)
|
||||
await Task.yield()
|
||||
await context.nextViewState()
|
||||
|
||||
XCTAssertTrue(context.viewState.isProcessingIgnoreRequest)
|
||||
try await Task.sleep(for: .milliseconds(10))
|
||||
await context.nextViewState()
|
||||
XCTAssertFalse(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssert(context.viewState.dmRecipient?.isIgnored == false)
|
||||
}
|
||||
@@ -164,10 +161,10 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
|
||||
XCTAssertEqual(context.viewState.dmRecipient, RoomMemberDetails(withProxy: recipient))
|
||||
|
||||
context.send(viewAction: .unignoreConfirmed)
|
||||
await Task.yield()
|
||||
await context.nextViewState()
|
||||
XCTAssertTrue(context.viewState.isProcessingIgnoreRequest)
|
||||
|
||||
try await Task.sleep(for: .milliseconds(10))
|
||||
await context.nextViewState()
|
||||
XCTAssertFalse(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssert(context.viewState.dmRecipient?.isIgnored == true)
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
|
||||
@@ -42,14 +42,13 @@ class RoomMemberDetailsViewModelTests: XCTestCase {
|
||||
viewModel = RoomMemberDetailsViewModel(roomMemberProxy: roomMemberProxyMock, mediaProvider: MockMediaProvider())
|
||||
|
||||
context.send(viewAction: .showIgnoreAlert)
|
||||
await Task.yield()
|
||||
XCTAssertEqual(context.ignoreUserAlert, .init(action: .ignore))
|
||||
|
||||
context.send(viewAction: .ignoreConfirmed)
|
||||
await Task.yield()
|
||||
await context.nextViewState()
|
||||
XCTAssertTrue(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssertFalse(context.viewState.details.isIgnored)
|
||||
try await Task.sleep(for: .milliseconds(10))
|
||||
await context.nextViewState()
|
||||
XCTAssertFalse(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssertTrue(context.viewState.details.isIgnored)
|
||||
}
|
||||
@@ -61,16 +60,14 @@ class RoomMemberDetailsViewModelTests: XCTestCase {
|
||||
return .failure(.ignoreUserFailed)
|
||||
}
|
||||
viewModel = RoomMemberDetailsViewModel(roomMemberProxy: roomMemberProxyMock, mediaProvider: MockMediaProvider())
|
||||
|
||||
context.send(viewAction: .showIgnoreAlert)
|
||||
await Task.yield()
|
||||
XCTAssertEqual(context.ignoreUserAlert, .init(action: .ignore))
|
||||
|
||||
context.send(viewAction: .ignoreConfirmed)
|
||||
await Task.yield()
|
||||
await context.nextViewState()
|
||||
XCTAssertTrue(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssertFalse(context.viewState.details.isIgnored)
|
||||
try await Task.sleep(for: .milliseconds(10))
|
||||
await context.nextViewState()
|
||||
XCTAssertFalse(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssertNotNil(context.errorAlert)
|
||||
XCTAssertFalse(context.viewState.details.isIgnored)
|
||||
@@ -85,14 +82,13 @@ class RoomMemberDetailsViewModelTests: XCTestCase {
|
||||
viewModel = RoomMemberDetailsViewModel(roomMemberProxy: roomMemberProxyMock, mediaProvider: MockMediaProvider())
|
||||
|
||||
context.send(viewAction: .showUnignoreAlert)
|
||||
await Task.yield()
|
||||
XCTAssertEqual(context.ignoreUserAlert, .init(action: .unignore))
|
||||
|
||||
context.send(viewAction: .unignoreConfirmed)
|
||||
await Task.yield()
|
||||
await context.nextViewState()
|
||||
XCTAssertTrue(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssertTrue(context.viewState.details.isIgnored)
|
||||
try await Task.sleep(for: .milliseconds(10))
|
||||
await context.nextViewState()
|
||||
XCTAssertFalse(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssertFalse(context.viewState.details.isIgnored)
|
||||
}
|
||||
@@ -106,14 +102,13 @@ class RoomMemberDetailsViewModelTests: XCTestCase {
|
||||
viewModel = RoomMemberDetailsViewModel(roomMemberProxy: roomMemberProxyMock, mediaProvider: MockMediaProvider())
|
||||
|
||||
context.send(viewAction: .showUnignoreAlert)
|
||||
await Task.yield()
|
||||
XCTAssertEqual(context.ignoreUserAlert, .init(action: .unignore))
|
||||
|
||||
context.send(viewAction: .unignoreConfirmed)
|
||||
await Task.yield()
|
||||
await context.nextViewState()
|
||||
XCTAssertTrue(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssertTrue(context.viewState.details.isIgnored)
|
||||
try await Task.sleep(for: .milliseconds(10))
|
||||
await context.nextViewState()
|
||||
XCTAssertFalse(context.viewState.isProcessingIgnoreRequest)
|
||||
XCTAssertTrue(context.viewState.details.isIgnored)
|
||||
XCTAssertNotNil(context.errorAlert)
|
||||
|
||||
@@ -25,7 +25,6 @@ class SessionVerificationViewModelTests: XCTestCase {
|
||||
var context: SessionVerificationViewModelType.Context!
|
||||
var sessionVerificationController: SessionVerificationControllerProxyMock!
|
||||
|
||||
@MainActor
|
||||
override func setUpWithError() throws {
|
||||
sessionVerificationController = SessionVerificationControllerProxyMock.configureMock()
|
||||
viewModel = SessionVerificationViewModel(sessionVerificationControllerProxy: sessionVerificationController)
|
||||
@@ -49,18 +48,14 @@ class SessionVerificationViewModelTests: XCTestCase {
|
||||
|
||||
context.send(viewAction: .close)
|
||||
|
||||
await Task.yield()
|
||||
|
||||
XCTAssertEqual(context.viewState.verificationState, .cancelling)
|
||||
|
||||
try await Task.sleep(for: .milliseconds(100))
|
||||
await context.nextViewState()
|
||||
|
||||
XCTAssertEqual(context.viewState.verificationState, .cancelled)
|
||||
|
||||
context.send(viewAction: .restart)
|
||||
|
||||
await Task.yield()
|
||||
|
||||
XCTAssertEqual(context.viewState.verificationState, .initial)
|
||||
|
||||
XCTAssert(sessionVerificationController.requestVerificationCallsCount == 1)
|
||||
|
||||
Reference in New Issue
Block a user