Skip to content

Commit

Permalink
test :: SigninViewModelSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
baekteun committed Oct 19, 2022
1 parent 7348c90 commit da7a4c2
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 19 deletions.
1 change: 1 addition & 0 deletions Projects/Features/SigninFeature/Sources/SigninView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ struct SigninView: View {
DMSWideButton(text: "로그인", color: .PrimaryVariant.primary) {
viewModel.signinButtonDidTap()
}
.disabled(!viewModel.isSigninButtonEnabled)
.padding(.top, 24)
.frame(maxWidth: .infinity)
.padding(.bottom, 40)
Expand Down
8 changes: 6 additions & 2 deletions Projects/Features/SigninFeature/Sources/SigninViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ final class SigninViewModel: BaseViewModel {
@Published var password = ""
@Published var isOnAutoSignin = true
@Published var isNavigateSignup = false
@Published var isSuccessSignin = false
var isSigninButtonEnabled: Bool {
!id.isEmpty && !password.isEmpty
}

private let signinUseCase: any SigninUseCase

Expand All @@ -15,9 +19,9 @@ final class SigninViewModel: BaseViewModel {
}

func signinButtonDidTap() {
guard isSigninButtonEnabled else { return }
addCancellable(signinUseCase.execute(req: .init(accountID: id, password: password))) { [weak self] _ in
self?.isErrorOcuured = true
self?.errorMessage = "아이디 또는 비밀번호를 확인해주세요"
self?.isSuccessSignin = true
}
}
}
85 changes: 85 additions & 0 deletions Projects/Features/SigninFeature/Tests/SigninViewModelSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import Quick
import Nimble
import Combine
import DomainModule
import DataModule
@testable import SigninFeature

// swiftlint: disable function_body_length
final class SigninViewModelSpec: QuickSpec {
override func spec() {
var signinUseCase: SigninUseCase!
var sut: SigninViewModel!

beforeEach {
signinUseCase = SigninUseCaseFake()
sut = SigninViewModel(signinUseCase: signinUseCase)
}
describe("SigninViewModel에서") {
context("유저가 아무것도 입력하지 않은 상태라면") {
it("isSigninButtonEnabled은 false이다.") {
expect { sut.id }.to(beEmpty())
expect { sut.password }.to(beEmpty())
expect { sut.isSigninButtonEnabled }.to(beFalse())
}
}
context("유저가 ID만 입력했다면") {
beforeEach {
sut.id = "A"
}
it("isSigninButtonEnabled은 false이다.") {
expect { sut.id }.toNot(beEmpty())
expect { sut.password }.to(beEmpty())
expect { sut.isSigninButtonEnabled }.to(beFalse())
}
}
context("유저가 Password만 입력했다면") {
beforeEach {
sut.password = "A"
}
it("isSigninButtonEnabled은 false이다.") {
expect { sut.id }.to(beEmpty())
expect { sut.password }.toNot(beEmpty())
expect { sut.isSigninButtonEnabled }.to(beFalse())
}
}
context("유저가 ID와 Password를 모두 입력했다면") {
beforeEach {
sut.id = "A"
sut.password = "A"
}
it("isSigninButtonEnabled은 true이다.") {
expect { sut.id }.toNot(beEmpty())
expect { sut.password }.toNot(beEmpty())
expect { sut.isSigninButtonEnabled }.to(beTrue())
}
}
context("유저가 ID와 Password를 알맞게 입력하고 로그인을 시도한다면") {
beforeEach {
sut.id = "baekteun"
sut.password = "baekteun"
sut.signinButtonDidTap()
}
it("isSuccessSignin이 true이다.") {
expect { sut.isSuccessSignin }.toEventually(beTrue())
}
}
context("유저가 ID와 Password중 하나라도 알맞지 않게 입력하고 로그인을 시도한다면") {
beforeEach {
sut.id = "ASDF"
sut.password = "ASDF"
sut.signinButtonDidTap()
}
it("isSuccessSignin은 false이다") {
expect { sut.isSuccessSignin }.toEventually(beFalse())
}
it("isErrorOcuured는 true이다") {
expect { sut.isErrorOcuured }.toEventually(beTrue())
}
it("errorMessage는 '아이디 또는 비밀번호를 확인해주세요.'가 나온다") {
expect { sut.errorMessage }.toEventually(equal("아이디 또는 비밀번호를 확인해주세요."))
}
}
}
}
}
17 changes: 0 additions & 17 deletions Projects/Features/SigninFeature/Tests/TargetTests.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Combine
import DataMappingModule
import DomainModule
import ErrorModule
import Foundation

public struct SigninUseCaseFake: SigninUseCase {
public init () {}

public func execute(req: SigninRequestDTO) -> AnyPublisher<Void, DmsError> {
if req.accountID == "baekteun" && req.password == "baekteun" {
return Just(()).setFailureType(to: DmsError.self)
.delay(for: 1, scheduler: DispatchQueue.main)
.eraseToAnyPublisher()
} else {
return Fail(error: DmsError.passwordMismatch)
.delay(for: 1, scheduler: DispatchQueue.main)
.eraseToAnyPublisher()
}
}
}

0 comments on commit da7a4c2

Please sign in to comment.