-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 109 additions & 97 deletions
206
...app/src/test/java/com/example/studentportal/grades/ui/viewmodel/EditGradeViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,109 @@ | ||
|
||
import com.example.studentportal.grades.service.GradeRepository | ||
import com.example.studentportal.grades.ui.model.GradeUiModel | ||
import com.example.studentportal.grades.ui.viewmodel.EditGradeViewModel | ||
import io.mockk.coEvery | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.TestCoroutineDispatcher | ||
import kotlinx.coroutines.test.advanceUntilIdle | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.runBlockingTest | ||
import kotlinx.coroutines.test.setMain | ||
import okhttp3.ResponseBody.Companion.toResponseBody | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import retrofit2.Response | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
@ExperimentalCoroutinesApi | ||
class EditGradeViewModelTest { | ||
|
||
private lateinit var viewModel: EditGradeViewModel | ||
private val gradeRepository: GradeRepository = mockk() | ||
private val initialGrade = GradeUiModel("1", 85, "John", "Doe", "123", "link") | ||
|
||
private val testDispatcher = TestCoroutineDispatcher() | ||
|
||
@Before | ||
fun setup() { | ||
Dispatchers.setMain(testDispatcher) // Use a controlled test dispatcher | ||
viewModel = EditGradeViewModel(initialGrade, gradeRepository, "STUDENT", testDispatcher) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
testDispatcher.cleanupTestCoroutines() | ||
Dispatchers.resetMain() // Reset main dispatcher after the test | ||
} | ||
|
||
@Test | ||
fun `updateText updates text state`() { | ||
viewModel.updateText("new text") | ||
assertEquals("new text", viewModel.text.value) | ||
} | ||
|
||
@Test | ||
fun `onButtonClick updates the submission link for STUDENT userType`() = runBlockingTest { | ||
viewModel = EditGradeViewModel(initialGrade, gradeRepository, "STUDENT", testDispatcher) | ||
val newText = "updated link" | ||
viewModel.updateText(newText) | ||
coEvery { gradeRepository.updateGrade(any()) } returns Response.success(Unit) | ||
|
||
viewModel.onButtonClick() | ||
advanceUntilIdle() // Ensure all coroutines are completed | ||
|
||
assertEquals(newText, viewModel.grade.value.submissionLink) | ||
} | ||
|
||
@Test | ||
fun `onButtonClick updates the score for non-STUDENT userType`() = runBlockingTest { | ||
viewModel = EditGradeViewModel(initialGrade, gradeRepository, "FACULTY", testDispatcher) | ||
val newScore = "92" | ||
viewModel.updateText(newScore) | ||
assertEquals("92", viewModel.text.value) | ||
coEvery { gradeRepository.updateGrade(any()) } returns Response.success(Unit) | ||
|
||
viewModel.onButtonClick() | ||
advanceUntilIdle() // Ensure all coroutines are completed | ||
|
||
assertEquals(92, viewModel.grade.value.score) | ||
} | ||
|
||
@Test | ||
fun `showDialog when update fails`() = runBlockingTest { | ||
viewModel.updateText("invalid score") | ||
coEvery { gradeRepository.updateGrade(any()) } returns Response.error(400, "Bad Request".toResponseBody()) | ||
|
||
viewModel.onButtonClick() | ||
advanceUntilIdle() // Ensure all coroutines are completed | ||
|
||
assertTrue(viewModel.showDialog) | ||
assertEquals("Failed to update grade: Bad Request", viewModel.dialogMessage) | ||
} | ||
|
||
@Test | ||
fun `dismissDialog works correctly`() { | ||
viewModel.showDialog = true // Manually trigger dialog | ||
|
||
viewModel.dismissDialog() | ||
|
||
assertFalse(viewModel.showDialog) | ||
} | ||
} | ||
// | ||
// import com.example.studentportal.MainDispatcherTestRule | ||
// import com.example.studentportal.common.service.models.successFlow | ||
// import com.example.studentportal.course.ui.model.UserType | ||
// import com.example.studentportal.grades.ui.model.GradeUiModel | ||
// import com.example.studentportal.grades.ui.viewmodel.EditGradeViewModel | ||
// import com.example.studentportal.grades.usecase.EditGradeUseCase | ||
// import com.example.studentportal.grades.usecase.model.GradeUseCaseModel | ||
// import io.mockk.coEvery | ||
// import io.mockk.mockkConstructor | ||
// import io.mockk.unmockkConstructor | ||
// import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
// import kotlinx.coroutines.test.StandardTestDispatcher | ||
// import kotlinx.coroutines.test.advanceUntilIdle | ||
// import kotlinx.coroutines.test.runBlockingTest | ||
// import org.junit.After | ||
// import org.junit.Before | ||
// import org.junit.Rule | ||
// import org.junit.Test | ||
// import org.koin.core.context.stopKoin | ||
// import kotlin.test.assertEquals | ||
// | ||
// @ExperimentalCoroutinesApi | ||
// class EditGradeViewModelTest { | ||
// private val mainDispatcher = StandardTestDispatcher() | ||
// private lateinit var viewModel: EditGradeViewModel | ||
// val initialGrade = GradeUiModel( | ||
// id = "1", | ||
// score = 10, | ||
// studentFirstName = "First-N1", | ||
// studentLastName = "Last-N1", | ||
// studentId = "1", | ||
// submissionLink = null, | ||
// ) | ||
// @get:Rule | ||
// var instantExecutorRule = InstantTaskExecutorRule() | ||
// | ||
// @get:Rule | ||
// var mainDispatcherRule = MainDispatcherTestRule(mainDispatcher) | ||
// | ||
// @Before | ||
// fun before() { | ||
// mockkConstructor(EditGradeUseCase::class) | ||
// viewModel = EditGradeViewModel(mainDispatcher) | ||
// } | ||
// | ||
// @After | ||
// fun tearDown() { | ||
// unmockkConstructor(EditGradeUseCase::class) | ||
// stopKoin() | ||
// } | ||
// | ||
// @Test | ||
// fun `updateText updates text state`() { | ||
// viewModel.updateText("new text") | ||
// assertEquals("new text", viewModel.uiResultLiveData.value?.text) | ||
// } | ||
// | ||
// @Test | ||
// fun `onButtonClick updates the submission link for STUDENT userType`() = runBlockingTest { | ||
// val useCaseModel = GradeUseCaseModel( | ||
// id = "1", | ||
// score = 10, | ||
// studentFirstName = "First-N1", | ||
// studentLastName = "Last-N1", | ||
// studentId = "1", | ||
// submissionLink = null, | ||
// assignmentId = "1" | ||
// ) | ||
// val newText = "newText" | ||
// viewModel.updateText(newText) | ||
// coEvery { anyConstructed<EditGradeUseCase>().launch() } returns successFlow( | ||
// useCaseModel | ||
// ) | ||
// | ||
// viewModel.onButtonClick( | ||
// initialGrade = initialGrade, | ||
// userType = UserType.STUDENT | ||
// ) | ||
// advanceUntilIdle() // Ensure all coroutines are completed | ||
// | ||
// assertEquals(newText, viewModel.uiResultLiveData.value?.text) | ||
// } | ||
// | ||
// @Test | ||
// fun `onButtonClick updates the score for non-STUDENT userType`() = runBlockingTest { | ||
// val useCaseModel = GradeUseCaseModel( | ||
// id = "1", | ||
// score = 10, | ||
// studentFirstName = "First-N1", | ||
// studentLastName = "Last-N1", | ||
// studentId = "1", | ||
// submissionLink = null, | ||
// assignmentId = "1" | ||
// ) | ||
// val newScore = "92" | ||
// viewModel.updateScore(newScore) | ||
// coEvery { anyConstructed<EditGradeUseCase>().launch() } returns successFlow( | ||
// useCaseModel | ||
// ) | ||
// viewModel.onButtonClick( | ||
// initialGrade = initialGrade, | ||
// userType = UserType.FACULTY | ||
// ) | ||
// advanceUntilIdle() // Ensure all coroutines are completed | ||
// | ||
// assertEquals(92, viewModel.uiResultLiveData.value?.score?.toInt()) | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters