Add tests to onboarding personalisation.

This commit is contained in:
Doug
2022-03-17 16:19:13 +00:00
parent 34dcaca74c
commit d72b815dc1
13 changed files with 167 additions and 85 deletions
@@ -31,23 +31,45 @@ class OnboardingDisplayNameUITests: MockScreenTest {
func verifyOnboardingDisplayNameScreen() throws {
guard let screenState = screenState as? MockOnboardingDisplayNameScreenState else { fatalError("no screen") }
switch screenState {
case .presence(let presence):
verifyOnboardingDisplayNamePresence(presence: presence)
case .longDisplayName(let name):
verifyOnboardingDisplayNameLongName(name: name)
case .emptyTextField:
verifyEmptyTextField()
case .filledTextField(let displayName):
verifyDisplayName(displayName: displayName)
case .longDisplayName(displayName: let displayName):
verifyLongDisplayName(displayName: displayName)
}
}
func verifyOnboardingDisplayNamePresence(presence: OnboardingDisplayNamePresence) {
let presenceText = app.staticTexts["presenceText"]
XCTAssert(presenceText.exists)
XCTAssertEqual(presenceText.label, presence.title)
func verifyEmptyTextField() {
let textField = app.textFields.element
XCTAssertTrue(textField.exists, "The textfield should always be shown.")
XCTAssertEqual(textField.value as? String, VectorL10n.onboardingDisplayNamePlaceholder, "When the textfield is empty, the value should match the placeholder.")
XCTAssertEqual(textField.placeholderValue, VectorL10n.onboardingDisplayNamePlaceholder, "The textfield's placeholder should be set.")
let footer = app.staticTexts["textFieldFooter"]
XCTAssertTrue(footer.exists, "The textfield's footer should always be shown.")
XCTAssertEqual(footer.label, VectorL10n.onboardingDisplayNameHint, "The footer should display a hint when no text is set.")
}
func verifyOnboardingDisplayNameLongName(name: String) {
let displayNameText = app.staticTexts["displayNameText"]
XCTAssert(displayNameText.exists)
XCTAssertEqual(displayNameText.label, name)
func verifyDisplayName(displayName: String) {
let textField = app.textFields.element
XCTAssertTrue(textField.exists, "The textfield should always be shown.")
XCTAssertEqual(textField.value as? String, displayName, "When a name has been set, it should show in the textfield.")
XCTAssertEqual(textField.placeholderValue, VectorL10n.onboardingDisplayNamePlaceholder, "The textfield's placeholder should be set.")
let footer = app.staticTexts["textFieldFooter"]
XCTAssertTrue(footer.exists, "The textfield's footer should always be shown.")
XCTAssertEqual(footer.label, VectorL10n.onboardingDisplayNameHint, "The footer should display a hint when an acceptable name is entered.")
}
func verifyLongDisplayName(displayName: String) {
let textField = app.textFields.element
XCTAssertTrue(textField.exists, "The textfield should always be shown.")
XCTAssertEqual(textField.value as? String, displayName, "When a name has been set, it should show in the textfield.")
XCTAssertEqual(textField.placeholderValue, VectorL10n.onboardingDisplayNamePlaceholder, "The textfield's placeholder should be set.")
let footer = app.staticTexts["textFieldFooter"]
XCTAssertTrue(footer.exists, "The textfield's footer should always be shown.")
XCTAssertEqual(footer.label, VectorL10n.onboardingDisplayNameMaxLength, "The footer should display an error when the display name is too long.")
}
}
@@ -22,36 +22,47 @@ import Combine
@available(iOS 14.0, *)
class OnboardingDisplayNameViewModelTests: XCTestCase {
private enum Constants {
static let presenceInitialValue: OnboardingDisplayNamePresence = .offline
static let displayName = "Alice"
}
var service: MockOnboardingDisplayNameService!
var viewModel: OnboardingDisplayNameViewModelProtocol!
var viewModel: OnboardingDisplayNameViewModel!
var context: OnboardingDisplayNameViewModelType.Context!
var cancellables = Set<AnyCancellable>()
override func setUpWithError() throws {
service = MockOnboardingDisplayNameService(displayName: Constants.displayName, presence: Constants.presenceInitialValue)
viewModel = OnboardingDisplayNameViewModel.makeOnboardingDisplayNameViewModel(onboardingDisplayNameService: service)
viewModel = nil
context = nil
}
func setUp(with displayName: String) {
viewModel = OnboardingDisplayNameViewModel(displayName: displayName)
context = viewModel.context
}
func testInitialState() {
XCTAssertEqual(context.viewState.displayName, Constants.displayName)
XCTAssertEqual(context.viewState.presence, Constants.presenceInitialValue)
func testValidDisplayName() {
// Given a short display name
let displayName = "Alice"
setUp(with: displayName)
// When validating the display name
viewModel.process(viewAction: .validateDisplayName)
// Then no error message should be set
XCTAssertEqual(context.viewState.bindings.displayName, displayName, "The display name should match the value used at init.")
XCTAssertNil(context.viewState.validationErrorMessage, "There should not be an error message in the view state.")
}
func testFirstPresenceReceived() throws {
let presencePublisher = context.$viewState.map(\.presence).removeDuplicates().collect(1).first()
XCTAssertEqual(try xcAwait(presencePublisher), [Constants.presenceInitialValue])
}
func testPresenceUpdatesReceived() throws {
let presencePublisher = context.$viewState.map(\.presence).removeDuplicates().collect(3).first()
let awaitDeferred = xcAwaitDeferred(presencePublisher)
let newPresenceValue1: OnboardingDisplayNamePresence = .online
let newPresenceValue2: OnboardingDisplayNamePresence = .idle
service.simulateUpdate(presence: newPresenceValue1)
service.simulateUpdate(presence: newPresenceValue2)
XCTAssertEqual(try awaitDeferred(), [Constants.presenceInitialValue, newPresenceValue1, newPresenceValue2])
func testInvalidDisplayName() {
// Given a short display name
let displayName = """
Bacon ipsum dolor amet filet mignon chicken kevin andouille. Doner shoulder beef, brisket bresaola turkey jowl venison. Ham hock cow turducken, chislic venison doner short loin strip steak tri-tip jowl. Sirloin pork belly hamburger ribeye. Tail capicola alcatra short ribs turkey doner.
"""
setUp(with: displayName)
// When validating the display name
viewModel.process(viewAction: .validateDisplayName)
// Then no error message should be set
XCTAssertEqual(context.viewState.bindings.displayName, displayName, "The display name should match the value used at init.")
XCTAssertNotNil(context.viewState.validationErrorMessage, "There should be an error message in the view state.")
}
}