diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/currency_converter/presentation/view/CurrencyConverterScreen.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/currency_converter/presentation/view/CurrencyConverterScreen.kt index df86d974..c568ce10 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/currency_converter/presentation/view/CurrencyConverterScreen.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/currency_converter/presentation/view/CurrencyConverterScreen.kt @@ -45,7 +45,7 @@ fun CurrencyFromToScreen(viewModel: CurrencyViewModel = hiltViewModel()) { text = it viewModel.handleIntent(CurrencyIntent.CurrencyInputChanged(it)) }, - label = { Text("Enter EUR Amount") }, + placeholder = { Text("Enter EUR Amount") }, modifier = Modifier.fillMaxWidth() ) diff --git a/app/src/test/java/com/example/jetpack_compose_all_in_one/viewmodel/CurrencyConverterViewModelTest.kt b/app/src/test/java/com/example/jetpack_compose_all_in_one/viewmodel/CurrencyConverterViewModelTest.kt new file mode 100644 index 00000000..f210c8f3 --- /dev/null +++ b/app/src/test/java/com/example/jetpack_compose_all_in_one/viewmodel/CurrencyConverterViewModelTest.kt @@ -0,0 +1,65 @@ +package com.example.jetpack_compose_all_in_one.viewmodel + +import com.example.jetpack_compose_all_in_one.demos.currency_converter.data.dto.CurrencyResponse +import com.example.jetpack_compose_all_in_one.demos.currency_converter.data.repository.CurrencyRepository +import com.example.jetpack_compose_all_in_one.demos.currency_converter.presentation.intent.CurrencyIntent +import com.example.jetpack_compose_all_in_one.demos.currency_converter.presentation.intent.CurrencyState +import com.example.jetpack_compose_all_in_one.demos.currency_converter.presentation.viewmodel.CurrencyViewModel +import io.mockk.coEvery +import io.mockk.mockk +import junit.framework.TestCase +import junit.framework.TestCase.assertEquals +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.delay +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.test.setMain +import org.junit.After +import org.junit.Before +import org.junit.Test + +class CurrencyConverterViewModelTest { + + private val testDispatcher = StandardTestDispatcher() + private val repository = mockk(relaxed = true) + private lateinit var viewModel: CurrencyViewModel + + @Before + fun setup() { + Dispatchers.setMain(testDispatcher) + viewModel = CurrencyViewModel(repository) + } + + @After + fun tearDown(){ + Dispatchers.resetMain() + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `Given user input, When fetching currency data, Then state is success and contains converted value`() = runTest { + val expectedResponse = CurrencyResponse("2010-01-01", 130.0) + coEvery { repository.getEURToJPY() } returns Result.success(expectedResponse) + + viewModel.handleIntent(CurrencyIntent.CurrencyInputChanged("100")) + advanceUntilIdle() + + val actualState = viewModel.state.value + val expectedState = CurrencyState.Success(expectedResponse) + assertEquals(expectedState, actualState) + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `Given user input, When encountering network exception, Then state is Error`() = runTest { + coEvery { repository.getEURToJPY() } returns Result.failure(RuntimeException("Network Error")) + + viewModel.handleIntent(CurrencyIntent.CurrencyInputChanged("100")) + advanceUntilIdle() + + TestCase.assertTrue(viewModel.state.value is CurrencyState.Error) + } +} \ No newline at end of file