Add utility to run UI tests for Screen states, add screen states for template and finish unit test.

This commit is contained in:
David Langley
2021-09-10 16:43:31 +01:00
parent 8c9a00b688
commit c69bd99b5f
15 changed files with 332 additions and 148 deletions
@@ -1,45 +0,0 @@
//
// Copyright 2021 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 Foundation
import SwiftUI
/**
Using an enum for the screen allows you define the different state cases with
the relevant associated data for each case.
*/
@available(iOS 14.0, *)
enum MockTemplateProfileUserScreenStates: MockScreen {
case mockPresenceStates(TemplateUserProfilePresence)
case mockLongDisplayName(String)
static var screenStates: [MockTemplateProfileUserScreenStates] = TemplateUserProfilePresence.allCases.map(MockTemplateProfileUserScreenStates.mockPresenceStates)
+ [.mockLongDisplayName("Somebody with a super long name we would like to test")]
static func screen(for state: MockTemplateProfileUserScreenStates) -> some View {
let service: MockTemplateUserProfileService
switch state {
case .mockPresenceStates(let presence):
service = MockTemplateUserProfileService(presence: presence)
case .mockLongDisplayName(let displayName):
service = MockTemplateUserProfileService(displayName: displayName)
}
let viewModel = TemplateUserProfileViewModel(userService: service)
return TemplateUserProfile(viewModel: viewModel)
.addDependency(MockAvatarService.example)
}
}
@@ -0,0 +1,60 @@
//
// Copyright 2021 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 Foundation
import SwiftUI
/// Using an enum for the screen allows you define the different state cases with
/// the relevant associated data for each case.
@available(iOS 14.0, *)
enum MockTemplateProfileUserScreenState: MockScreenState, CaseIterable {
// A case for each state you want to represent
// with specific, minimal associated data that will allow you
// mock that screen.
case presence(TemplateUserProfilePresence)
case longDisplayName(String)
/// The associated screen
var screenType: Any.Type {
TemplateUserProfile.self
}
/// A list of screen state definitions
static var allCases: [MockTemplateProfileUserScreenState] {
// Each of the presence statuses
TemplateUserProfilePresence.allCases.map(MockTemplateProfileUserScreenState.presence)
// A long display name
+ [.longDisplayName("Somebody with a super long name we would like to test")]
}
/// Generate the view struct for the screen state.
var screenView: AnyView {
let service: MockTemplateUserProfileService
switch self {
case .presence(let presence):
service = MockTemplateUserProfileService(presence: presence)
case .longDisplayName(let displayName):
service = MockTemplateUserProfileService(displayName: displayName)
}
let viewModel = TemplateUserProfileViewModel(userService: service)
// can simulate service and viewModel actions here if needs be.
return AnyView(TemplateUserProfile(viewModel: viewModel)
.addDependency(MockAvatarService.example))
}
}
@@ -15,33 +15,35 @@
//
import XCTest
@testable import RiotSwiftUI
import RiotSwiftUI
@available(iOS 14.0, *)
class TestUserProfileUITests: XCTestCase {
let app = XCUIApplication()
class TestUserProfileUITests: MockScreenTest {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
app.launch()
// In UI tests its important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
override class var screenType: MockScreenState.Type {
return MockTemplateProfileUserScreenState.self
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
func testTemplateUserProfileScreen() throws {
guard let screenState = screenState as? MockTemplateProfileUserScreenState else { fatalError("no screen") }
switch screenState {
case .presence(let presence):
testTemplateUserProfilePresence(presence: presence)
case .longDisplayName(let name):
testTemplateUserProfileLongName(name: name)
}
}
func testUserContentTextDisplayed() throws {
let userContentText = app.staticTexts["More great user content!"]
XCTAssert(userContentText.exists)
func testTemplateUserProfilePresence(presence: TemplateUserProfilePresence) {
let presenceText = app.staticTexts["presenceText"]
XCTAssert(presenceText.exists)
XCTAssert(presenceText.label == presence.title)
}
func testTemplateUserProfileLongName(name: String) {
let displayNameText = app.staticTexts["displayNameText"]
XCTAssert(displayNameText.exists)
XCTAssert(displayNameText.label == name)
}
}
@@ -38,12 +38,12 @@ class TemplateUserProfileViewModelTests: XCTestCase {
XCTAssertEqual(viewModel.viewState.presence, Constants.presenceInitialValue)
}
func testFirstPresenceRecieved() throws {
func testFirstPresenceReceived() throws {
let presencePublisher = viewModel.$viewState.map(\.presence).removeDuplicates().collect(1).first()
XCTAssertEqual(try xcAwait(presencePublisher), [Constants.presenceInitialValue])
}
func testPresenceUpdatesRecieved() throws {
func testPresenceUpdatesReceived() throws {
let presencePublisher = viewModel.$viewState.map(\.presence).removeDuplicates().collect(3).first()
let newPresenceValue1: TemplateUserProfilePresence = .online
let newPresenceValue2: TemplateUserProfilePresence = .idle
@@ -67,6 +67,6 @@ struct TemplateUserProfile: View {
@available(iOS 14.0, *)
struct TemplateUserProfile_Previews: PreviewProvider {
static var previews: some View {
MockTemplateProfileUserScreenStates.screenGroup()
MockTemplateProfileUserScreenState.screenGroup()
}
}
@@ -38,6 +38,9 @@ struct TemplateUserProfileHeader: View {
VStack(spacing: 8){
Text(displayName ?? "")
.font(theme.fonts.title3)
.accessibility(identifier: "displayNameText")
.padding(.horizontal)
.lineLimit(1)
TemplateUserProfilePresenceView(presence: presence)
}
}
@@ -31,6 +31,7 @@ struct TemplateUserProfilePresenceView: View {
.frame(width: 8, height: 8)
Text(presence.title)
.font(.subheadline)
.accessibilityIdentifier("presenceText")
}
.foregroundColor(foregroundColor)
.padding(0)