Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

[#139] Add a validation to enable/disable the save button of the Edit Profile view #143

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion mentorship ios/Views/Profile/ProfileEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ struct ProfileEditor: View {
@State var editProfileData = ProfileViewModel().getEditProfileData()
@ObservedObject var profileViewModel = ProfileViewModel()

var canSave:Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change name to enableSaveButton/disableSaveButton. More descriptive.

return (self.editProfileData.name ?? "").isEmpty == false
}

// make api call to update profile
func updateProfile() {
self.profileService.updateProfile(updateProfileData: self.editProfileData) { response in
Expand Down Expand Up @@ -79,7 +83,7 @@ struct ProfileEditor: View {
self.profileViewModel.inActivity = true
// make network call to update profile
self.updateProfile()
})
}.disabled(self.canSave == false))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can do something like .disabled(editProfileData.name.isEmpty)

.alert(isPresented: $profileViewModel.showAlert) {
Alert.init(
title: Text(profileViewModel.alertTitle),
Expand Down
29 changes: 25 additions & 4 deletions mentorship iosTests/ProfileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProfileTests: XCTestCase {
var urlSession: URLSession!

// MARK: - Setup and Tear Down

override func setUpWithError() throws {
// Set url session for mock networking
let configuration = URLSessionConfiguration.ephemeral
Expand All @@ -25,7 +25,7 @@ class ProfileTests: XCTestCase {
// api calls require a token, otherwise tests fail
try KeychainManager.setToken(username: "", tokenString: "")
}

override func tearDownWithError() throws {
urlSession = nil
try KeychainManager.deleteToken()
Expand Down Expand Up @@ -86,7 +86,7 @@ class ProfileTests: XCTestCase {

func testSaveAndGetProfile() {
let profileVM = ProfileViewModel()

//save sample data
profileVM.saveProfile(profile: sampleProfileData)

Expand All @@ -100,7 +100,7 @@ class ProfileTests: XCTestCase {

func testEditProfileDataReceived() {
let profileVM = ProfileViewModel()

//prepare sample data
let sampleData = ProfileModel.ProfileData(id: 100, name: nil, username: "username", email: "test@abc.com")

Expand Down Expand Up @@ -148,6 +148,27 @@ class ProfileTests: XCTestCase {
XCTAssertEqual(profileVM.getProfile().name, "newName")
}

func testSaveButtonDisableState() {
// Profile Service
let profileService: ProfileService = ProfileAPI(urlSession: urlSession)

// View Model
var sampleData = ProfileModel.ProfileData(id: 0, name: "", username: "", email: "")
let profileVM = ProfileViewModel()
profileVM.saveProfile(profile: sampleData)

// Profile Editor View
var profileEditor = ProfileEditor(profileService: profileService, profileViewModel: profileVM)

XCTAssertFalse(profileEditor.canSave)

sampleData = ProfileModel.ProfileData(id: 0, name: "name", username: "", email: "")
profileVM.saveProfile(profile: sampleData)

profileEditor = ProfileEditor(profileService: profileService, profileViewModel: profileVM)
XCTAssertTrue(profileEditor.canSave)
}

// MARK: View Tests (Integration Tests)

func testUpdateProfileAction() throws {
Expand Down