From 97e9a7af1a58ccd6577458579ea6b21e34ace7e7 Mon Sep 17 00:00:00 2001 From: MyungHyun Ryu Date: Sun, 10 Dec 2023 23:30:56 +0900 Subject: [PATCH] :white_check_mark: Add test --- .../viewmodel/SignupViewModelTest.kt | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/SignupViewModelTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/SignupViewModelTest.kt index fb6672c3..11b2dbf6 100644 --- a/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/SignupViewModelTest.kt +++ b/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/SignupViewModelTest.kt @@ -2,22 +2,31 @@ package com.example.speechbuddy.viewmodel import androidx.arch.core.executor.testing.InstantTaskExecutorRule import com.example.speechbuddy.R +import com.example.speechbuddy.data.remote.requests.AuthSignupRequest import com.example.speechbuddy.repository.AuthRepository +import com.example.speechbuddy.ui.models.SignupError import com.example.speechbuddy.ui.models.SignupErrorType +import com.example.speechbuddy.ui.models.SignupUiState +import com.example.speechbuddy.utils.ErrorResponse import com.example.speechbuddy.utils.ResponseHandler +import io.mockk.coEvery import io.mockk.impl.annotations.MockK import io.mockk.mockk import junit.framework.TestCase.assertEquals import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.newSingleThreadContext +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.setMain +import okhttp3.ResponseBody.Companion.toResponseBody import org.junit.After import org.junit.Before import org.junit.Rule import org.junit.Test +import retrofit2.Response class SignupViewModelTest { @@ -200,6 +209,61 @@ class SignupViewModelTest { assertEquals(false, viewModel.uiState.value.isValidPassword) } + @Test + fun `should set error type unknown when email is null`() { + viewModel.setEmail(null) + viewModel.setNickname(validNickname) + viewModel.setPassword(validPassword) + viewModel.setPasswordCheck(validPassword) + viewModel.signup {} + assertEquals( + SignupUiState( + isValidEmail = false, + isValidNickname = true, + isValidPassword = true, + error = SignupError( + type = SignupErrorType.UNKNOWN, + messageId = R.string.unknown_error + ) + ), viewModel.uiState.value + ) + } + + @Test + fun `should set error type email when abd request is returned`() = runBlocking { + viewModel.setEmail(validEmail) + viewModel.setNickname(validNickname) + viewModel.setPassword(validPassword) + viewModel.setPasswordCheck(validPassword) + + val errorResponseBody = "Error message or JSON here".toResponseBody(null) + val errorResponse: Response = Response.success(null) + val errorResponse2 = ErrorResponse(key = "email") + coEvery { + mockAuthRepository.signup( + AuthSignupRequest( + email = validEmail, + nickname = validNickname, + password = validPassword + ) + ) + } returns flowOf(errorResponse) +// coEvery { mockResponseHandler.parseErrorResponse(errorResponseBody)} returns errorResponse2 + val expectedUiState = SignupUiState( + isValidEmail = false, + isValidNickname = true, + isValidPassword = true, + buttonEnabled = !viewModel.uiState.value.buttonEnabled, + loading = !viewModel.uiState.value.loading, + error = null + ) + viewModel.signup {} + + assertEquals(expectedUiState, viewModel.uiState.value) + } + + + @Test fun `should set error type null after signup click when short password is changed to valid password`() { viewModel.setPassword(shortPassword)