Fix Naming

Change userService name to templateUserProfileService for templating.
Remove test subclass from MockScreenTest
This commit is contained in:
David Langley
2021-09-11 14:13:43 +01:00
parent 9e8a90ec2a
commit a73d8a4122
9 changed files with 41 additions and 31 deletions
@@ -39,7 +39,7 @@ final class TemplateUserProfileCoordinator: Coordinator {
@available(iOS 14.0, *)
init(parameters: TemplateUserProfileCoordinatorParameters) {
self.parameters = parameters
let viewModel = TemplateUserProfileViewModel(userService: TemplateUserProfileService(session: parameters.session))
let viewModel = TemplateUserProfileViewModel(templateUserProfileService: TemplateUserProfileService(session: parameters.session))
let view = TemplateUserProfile(viewModel: viewModel)
.addDependency(AvatarService.instantiate(mediaManager: parameters.session.mediaManager))
templateUserProfileViewModel = viewModel
@@ -21,7 +21,7 @@ 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 {
enum MockTemplateUserProfileScreenState: MockScreenState, CaseIterable {
// A case for each state you want to represent
// with specific, minimal associated data that will allow you
// mock that screen.
@@ -34,9 +34,9 @@ enum MockTemplateProfileUserScreenState: MockScreenState, CaseIterable {
}
/// A list of screen state definitions
static var allCases: [MockTemplateProfileUserScreenState] {
static var allCases: [MockTemplateUserProfileScreenState] {
// Each of the presence statuses
TemplateUserProfilePresence.allCases.map(MockTemplateProfileUserScreenState.presence)
TemplateUserProfilePresence.allCases.map(MockTemplateUserProfileScreenState.presence)
// A long display name
+ [.longDisplayName("Somebody with a super long name we would like to test")]
}
@@ -50,7 +50,7 @@ enum MockTemplateProfileUserScreenState: MockScreenState, CaseIterable {
case .longDisplayName(let displayName):
service = MockTemplateUserProfileService(displayName: displayName)
}
let viewModel = TemplateUserProfileViewModel(userService: service)
let viewModel = TemplateUserProfileViewModel(templateUserProfileService: service)
// can simulate service and viewModel actions here if needs be.
@@ -18,29 +18,33 @@ import XCTest
import RiotSwiftUI
@available(iOS 14.0, *)
class TestUserProfileUITests: MockScreenTest {
class TemplateUserProfileUITests: MockScreenTest {
override class var screenType: MockScreenState.Type {
return MockTemplateProfileUserScreenState.self
return MockTemplateUserProfileScreenState.self
}
override class func createTest() -> MockScreenTest {
return TemplateUserProfileUITests(selector: #selector(verifyTemplateUserProfileScreen))
}
func testTemplateUserProfileScreen() throws {
guard let screenState = screenState as? MockTemplateProfileUserScreenState else { fatalError("no screen") }
func verifyTemplateUserProfileScreen() throws {
guard let screenState = screenState as? MockTemplateUserProfileScreenState else { fatalError("no screen") }
switch screenState {
case .presence(let presence):
testTemplateUserProfilePresence(presence: presence)
verifyTemplateUserProfilePresence(presence: presence)
case .longDisplayName(let name):
testTemplateUserProfileLongName(name: name)
verifyTemplateUserProfileLongName(name: name)
}
}
func testTemplateUserProfilePresence(presence: TemplateUserProfilePresence) {
func verifyTemplateUserProfilePresence(presence: TemplateUserProfilePresence) {
let presenceText = app.staticTexts["presenceText"]
XCTAssert(presenceText.exists)
XCTAssert(presenceText.label == presence.title)
}
func testTemplateUserProfileLongName(name: String) {
func verifyTemplateUserProfileLongName(name: String) {
let displayNameText = app.staticTexts["displayNameText"]
XCTAssert(displayNameText.exists)
XCTAssert(displayNameText.label == name)
@@ -30,7 +30,7 @@ class TemplateUserProfileViewModelTests: XCTestCase {
var cancellables = Set<AnyCancellable>()
override func setUpWithError() throws {
service = MockTemplateUserProfileService(displayName: Constants.displayName, presence: Constants.presenceInitialValue)
viewModel = TemplateUserProfileViewModel(userService: service)
viewModel = TemplateUserProfileViewModel(templateUserProfileService: service)
}
func testInitialState() {
@@ -67,6 +67,6 @@ struct TemplateUserProfile: View {
@available(iOS 14.0, *)
struct TemplateUserProfile_Previews: PreviewProvider {
static var previews: some View {
MockTemplateProfileUserScreenState.screenGroup()
MockTemplateUserProfileScreenState.screenGroup()
}
}
@@ -23,7 +23,7 @@ class TemplateUserProfileViewModel: ObservableObject, TemplateUserProfileViewMod
// MARK: - Properties
// MARK: Private
private let userService: TemplateUserProfileServiceProtocol
private let templateUserProfileService: TemplateUserProfileServiceProtocol
private var cancellables = Set<AnyCancellable>()
// MARK: Public
@@ -32,11 +32,11 @@ class TemplateUserProfileViewModel: ObservableObject, TemplateUserProfileViewMod
var completion: ((TemplateUserProfileViewModelResult) -> Void)?
// MARK: - Setup
init(userService: TemplateUserProfileServiceProtocol, initialState: TemplateUserProfileViewState? = nil) {
self.userService = userService
self.viewState = initialState ?? Self.defaultState(userService: userService)
init(templateUserProfileService: TemplateUserProfileServiceProtocol, initialState: TemplateUserProfileViewState? = nil) {
self.templateUserProfileService = templateUserProfileService
self.viewState = initialState ?? Self.defaultState(templateUserProfileService: templateUserProfileService)
userService.presenceSubject
templateUserProfileService.presenceSubject
.map(TemplateUserProfileStateAction.updatePresence)
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] action in
@@ -45,8 +45,12 @@ class TemplateUserProfileViewModel: ObservableObject, TemplateUserProfileViewMod
.store(in: &cancellables)
}
private static func defaultState(userService: TemplateUserProfileServiceProtocol) -> TemplateUserProfileViewState {
return TemplateUserProfileViewState(avatar: userService.avatarData, displayName: userService.displayName, presence: userService.presenceSubject.value)
private static func defaultState(templateUserProfileService: TemplateUserProfileServiceProtocol) -> TemplateUserProfileViewState {
return TemplateUserProfileViewState(
avatar: templateUserProfileService.avatarData,
displayName: templateUserProfileService.displayName,
presence: templateUserProfileService.presenceSubject.value
)
}
// MARK: - Public