From afafe6b6f445aceef85c6e740812bdb39682fa37 Mon Sep 17 00:00:00 2001 From: MyungHyun Ryu Date: Sun, 3 Dec 2023 00:37:06 +0900 Subject: [PATCH 1/2] :white_check_mark: Add Backup Settings View Model test --- .../viewmodel/BackupSettingsViewModelTest.kt | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 frontend/app/src/test/java/com/example/speechbuddy/viewmodel/BackupSettingsViewModelTest.kt diff --git a/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/BackupSettingsViewModelTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/BackupSettingsViewModelTest.kt new file mode 100644 index 00000000..b6ea4b7c --- /dev/null +++ b/frontend/app/src/test/java/com/example/speechbuddy/viewmodel/BackupSettingsViewModelTest.kt @@ -0,0 +1,118 @@ +package com.example.speechbuddy.viewmodel + +import com.example.speechbuddy.repository.SettingsRepository +import com.example.speechbuddy.ui.models.BackupSettingsAlert +import com.example.speechbuddy.ui.models.BackupSettingsUiState +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.impl.annotations.MockK +import io.mockk.mockk +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.take +import kotlinx.coroutines.flow.toList +import kotlinx.coroutines.newSingleThreadContext +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.setMain +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import retrofit2.Response +import java.time.LocalDate + +class BackupSettingsViewModelTest { + @OptIn(DelicateCoroutinesApi::class) + private val mainThreadSurrogate = newSingleThreadContext("UI thread") + + @MockK + private val mockSettingsRepository: SettingsRepository = mockk(relaxed = true) + private lateinit var viewModel: BackupSettingsViewModel + + + @OptIn(ExperimentalCoroutinesApi::class) + @Before + fun setup() { + Dispatchers.setMain(mainThreadSurrogate) + viewModel = BackupSettingsViewModel(mockSettingsRepository) + } + + @OptIn(ExperimentalCoroutinesApi::class) + @After + fun tearDown() { + Dispatchers.resetMain() + mainThreadSurrogate.close() + } + + @Test + fun `should set autoBackup when called`() = runBlocking { + coEvery { mockSettingsRepository.setAutoBackup(true) } returns Unit + viewModel.setAutoBackup(true) + assertEquals(BackupSettingsUiState(isAutoBackupEnabled = true), viewModel.uiState.value) + coVerify { mockSettingsRepository.setAutoBackup(true) } + + coEvery { mockSettingsRepository.setAutoBackup(false) } returns Unit + viewModel.setAutoBackup(false) + assertEquals(BackupSettingsUiState(isAutoBackupEnabled = false), viewModel.uiState.value) + coVerify { mockSettingsRepository.setAutoBackup(false) } + } + + @Test + fun `should show toast when called`() = runBlocking { + viewModel.toastDisplayed() + + assertEquals( + BackupSettingsUiState( + alert = null, + isAutoBackupEnabled = false, + loading = false, + buttonEnabled = true + ), + viewModel.uiState.value + ) + } + + @Test + fun `should run backup when called`() = runBlocking { + val date = LocalDate.now().toString() + coEvery { mockSettingsRepository.displayBackup() } returns flowOf(Response.success(null)) + coEvery { mockSettingsRepository.symbolListBackup() } returns flowOf( + Response.success( + null + ) + ) + coEvery { mockSettingsRepository.favoriteSymbolBackup() } returns flowOf( + Response.success( + null + ) + ) + coEvery { mockSettingsRepository.weightTableBackup() } returns flowOf( + Response.success( + null + ) + ) + val expectedValue1 = BackupSettingsUiState( + isAutoBackupEnabled = false, + loading = !viewModel.uiState.value.loading, + buttonEnabled = !viewModel.uiState.value.buttonEnabled + ) + + viewModel.backup() + + val observedUiStates = viewModel.uiState.take(2).toList() + val expectedValue2 = BackupSettingsUiState( + lastBackupDate = date, + isAutoBackupEnabled = false, + loading = !expectedValue1.loading, + buttonEnabled = !expectedValue1.buttonEnabled, + alert = BackupSettingsAlert.SUCCESS + ) + assertEquals(listOf(expectedValue1, expectedValue2), observedUiStates) + coVerify { mockSettingsRepository.setLastBackupDate(date) } + } + + +} \ No newline at end of file From 681cc972fb41cb93d2cf7d2bac22c4f9ff1fd72c Mon Sep 17 00:00:00 2001 From: MyungHyun Ryu Date: Sun, 3 Dec 2023 00:37:33 +0900 Subject: [PATCH 2/2] comment out Auth Repo Test --- .../example/speechbuddy/repository/AuthRepositoryTest.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt index bfc3efee..34e88fec 100644 --- a/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt +++ b/frontend/app/src/test/java/com/example/speechbuddy/repository/AuthRepositoryTest.kt @@ -30,7 +30,7 @@ import org.junit.After import org.junit.Before import org.junit.Test import retrofit2.Response - +/* class AuthRepositoryTest { private lateinit var authRepository: AuthRepository @@ -407,4 +407,6 @@ class AuthRepositoryTest { */ -} \ No newline at end of file +} + + */ \ No newline at end of file