Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
: unit tests for the viewmodel
  • Loading branch information
PratyushSingh07 committed Jul 27, 2023
1 parent c77c614 commit 5124d0c
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions app/src/test/java/org/mifos/mobile/viewModels/HelpViewModelTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.mifos.mobile.viewModels

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.Observer
import junit.framework.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mifos.mobile.models.FAQ
import org.mifos.mobile.utils.HelpUiState
import org.mockito.Mock
import org.mockito.Mockito.*
import org.mockito.junit.MockitoJUnitRunner

@RunWith(MockitoJUnitRunner::class)
class HelpViewModelTest {

@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

@Mock
private lateinit var mockFAQArrayList: ArrayList<FAQ?>

@Mock
private lateinit var helpUiStateObserver: Observer<HelpUiState>

private lateinit var viewModel: HelpViewModel

@Before
fun setUp() {
viewModel = HelpViewModel()
viewModel.helpUiState.observeForever(helpUiStateObserver)
}

@Test
fun testLoadFaq() {
val qs = arrayOf("Question1", "Question2")
val ans = arrayOf("Answer1", "Answer2")

viewModel.loadFaq(qs, ans)

verify(helpUiStateObserver).onChanged(
HelpUiState.ShowFaq(
arrayListOf(
FAQ(
"Question1",
"Answer1",
false
),
FAQ(
"Question2",
"Answer2",
false
)
)
)
)
verifyNoMoreInteractions(helpUiStateObserver)
}

@Test
fun testFilterList() {
val query = "app"
val mockFAQ1 = mock(FAQ::class.java)
val mockFAQ2 = mock(FAQ::class.java)

`when`(mockFAQ1.question).thenReturn("How to use the app?")
`when`(mockFAQ2.question).thenReturn("Is there a user guide available?")

val filteredList = viewModel.filterList(arrayListOf(mockFAQ1,mockFAQ2), query)

assertEquals(mockFAQ1, filteredList[0])
}
}

0 comments on commit 5124d0c

Please sign in to comment.