#2154 moved some tests to page view model tests WIP

This commit is contained in:
Frans-Lukas 2020-07-10 11:37:55 +02:00
parent 6a83a9e123
commit f8e7c9ab8c
5 changed files with 52 additions and 93 deletions

View File

@ -23,8 +23,9 @@ import org.kiwix.kiwixmobile.core.page.bookmark.adapter.BookmarkItem
import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.BookmarkState
import org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem
import org.kiwix.kiwixmobile.core.page.history.viewmodel.HistoryState
import org.kiwix.kiwixmobile.core.page.viewmodel.TestablePageState
class PageImpl(
data class PageImpl(
override val zimFilePath: String? = "zimFilePath",
override val url: String = "url",
override var isSelected: Boolean = false,
@ -92,8 +93,10 @@ fun bookmark(
}
fun bookmarkState(
bookmarks: List<BookmarkItem> = listOf(),
bookmarks: List<BookmarkItem> = emptyList(),
showAll: Boolean = true,
zimId: String = "id",
searchTerm: String = ""
): BookmarkState = BookmarkState(bookmarks, showAll, zimId, searchTerm)
fun pageState(): TestablePageState = TestablePageState()

View File

@ -34,18 +34,13 @@ import org.kiwix.kiwixmobile.core.page.bookmark
import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.effects.ShowDeleteBookmarksDialog
import org.kiwix.kiwixmobile.core.page.bookmark.viewmodel.effects.UpdateAllBookmarksPreference
import org.kiwix.kiwixmobile.core.page.bookmarkState
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.Exit
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.ExitActionModeMenu
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.Filter
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.OnItemClick
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.OnItemLongClick
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.UpdatePages
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.UserClickedDeleteButton
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.UserClickedDeleteSelectedPages
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.UserClickedShowAllToggle
import org.kiwix.kiwixmobile.core.page.viewmodel.effects.OpenPage
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.Finish
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.sharedFunctions.InstantExecutorExtension
import org.kiwix.sharedFunctions.setScheduler
@ -78,13 +73,7 @@ internal class BookmarkViewModelTest {
}
@Test
fun `initial state is Initialising`() {
viewModel.state.test().assertValue(bookmarkState())
}
@Test
internal fun `ExitBookmarks finishes activity`() {
viewModel.effects.test().also { viewModel.actions.offer(Exit) }.assertValue(Finish)
fun `Initial state is initialising a bookmark state`() {
viewModel.state.test().assertValue(bookmarkState())
}
@ -119,30 +108,6 @@ internal class BookmarkViewModelTest {
viewModel.state.test().assertValue(bookmarkState(showAll = false))
}
@Test
internal fun `OnItemClick selects item if one is selected`() {
val bookmark = bookmark(isSelected = true)
viewModel.state.postValue(bookmarkState(listOf(bookmark)))
viewModel.actions.offer(OnItemClick(bookmark))
viewModel.state.test().assertValue(bookmarkState(listOf(bookmark())))
}
@Test
internal fun `OnItemClick offers OpenBookmark if none is selected`() {
viewModel.state.postValue(bookmarkState(listOf(bookmark())))
viewModel.effects.test().also { viewModel.actions.offer(OnItemClick(bookmark())) }
.assertValue(OpenPage(bookmark(), zimReaderContainer))
viewModel.state.test().assertValue(bookmarkState(listOf(bookmark())))
}
@Test
internal fun `OnItemLongClick selects item if none is selected`() {
val bookmark = bookmark()
viewModel.state.postValue(bookmarkState(listOf(bookmark)))
viewModel.actions.offer(OnItemLongClick(bookmark))
viewModel.state.test().assertValue(bookmarkState(listOf(bookmark(isSelected = true))))
}
@Test
fun `Filter updates search term`() {
val searchTerm = "searchTerm"
@ -151,7 +116,7 @@ internal class BookmarkViewModelTest {
}
@Test
internal fun `UpdateBookmarks updates bookmarks`() {
internal fun `UpdatePages updates bookmarks`() {
viewModel.actions.offer(UpdatePages(listOf(bookmark())))
viewModel.state.test().assertValue(bookmarkState(listOf(bookmark())))
}

View File

@ -19,13 +19,10 @@ import org.kiwix.kiwixmobile.core.page.historyItem
import org.kiwix.kiwixmobile.core.page.historyState
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.ExitActionModeMenu
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.Filter
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.OnItemClick
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.OnItemLongClick
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.UpdatePages
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.UserClickedDeleteButton
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.UserClickedDeleteSelectedPages
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.UserClickedShowAllToggle
import org.kiwix.kiwixmobile.core.page.viewmodel.effects.OpenPage
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.sharedFunctions.InstantExecutorExtension
@ -60,7 +57,7 @@ internal class HistoryViewModelTest {
}
@Test
fun `initial state is Initialising`() {
fun `Initial state is initialising a history state`() {
viewModel.state.test().assertValue(historyState())
}
@ -76,26 +73,14 @@ internal class HistoryViewModelTest {
@Test
internal fun `UserClickedDeleteButton offers ShowDeleteHistoryDialog`() {
viewModel.effects.test().also { viewModel.actions.offer(UserClickedDeleteButton) }
.assertValue(
ShowDeleteHistoryDialog(
viewModel.effects,
historyState(),
historyDao
)
)
.assertValue(ShowDeleteHistoryDialog(viewModel.effects, historyState(), historyDao))
viewModel.state.test().assertValue(historyState())
}
@Test
internal fun `UserClickedDeleteSelectedHistoryItems offers ShowDeleteHistoryDialog`() {
viewModel.effects.test().also { viewModel.actions.offer(UserClickedDeleteSelectedPages) }
.assertValue(
ShowDeleteHistoryDialog(
viewModel.effects,
historyState(),
historyDao
)
)
.assertValue(ShowDeleteHistoryDialog(viewModel.effects, historyState(), historyDao))
viewModel.state.test().assertValue(historyState())
}
@ -107,30 +92,6 @@ internal class HistoryViewModelTest {
viewModel.state.test().assertValue(historyState(showAll = false))
}
@Test
internal fun `OnItemClick selects item if one is selected`() {
val historyItem = historyItem(isSelected = true)
viewModel.state.postValue(historyState(listOf(historyItem)))
viewModel.actions.offer(OnItemClick(historyItem))
viewModel.state.test().assertValue(historyState(listOf(historyItem())))
}
@Test
internal fun `OnItemClick offers OpenHistoryItem if none is selected`() {
viewModel.state.postValue(historyState(listOf(historyItem())))
viewModel.effects.test().also { viewModel.actions.offer(OnItemClick(historyItem())) }
.assertValue(OpenPage(historyItem(), zimReaderContainer))
viewModel.state.test().assertValue(historyState(listOf(historyItem())))
}
@Test
internal fun `OnItemLongClick selects item if none is selected`() {
val historyItem = historyItem()
viewModel.state.postValue(historyState(listOf(historyItem)))
viewModel.actions.offer(OnItemLongClick(historyItem))
viewModel.state.test().assertValue(historyState(listOf(historyItem(isSelected = true))))
}
@Test
fun `Filter updates search term`() {
val searchTerm = "searchTerm"
@ -139,7 +100,7 @@ internal class HistoryViewModelTest {
}
@Test
internal fun `UpdateHistory updates history`() {
internal fun `UpdatePages updates history`() {
viewModel.actions.offer(UpdatePages(listOf(historyItem())))
viewModel.state.test().assertValue(historyState(listOf(historyItem())))
}

View File

@ -30,7 +30,11 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.kiwix.kiwixmobile.core.dao.PageDao
import org.kiwix.kiwixmobile.core.page.PageImpl
import org.kiwix.kiwixmobile.core.page.adapter.Page
import org.kiwix.kiwixmobile.core.page.pageState
import org.kiwix.kiwixmobile.core.page.viewmodel.Action.Exit
import org.kiwix.kiwixmobile.core.page.viewmodel.effects.OpenPage
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.Finish
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
@ -42,7 +46,6 @@ internal class PageViewModelTest {
private val pageDao: PageDao = mockk()
private val zimReaderContainer: ZimReaderContainer = mockk()
private val sharedPreferenceUtil: SharedPreferenceUtil = mockk()
private val state = TestablePageState()
private lateinit var viewModel: TestablePageViewModel
private val testScheduler = TestScheduler()
@ -64,9 +67,38 @@ internal class PageViewModelTest {
viewModel = TestablePageViewModel(zimReaderContainer, sharedPreferenceUtil, pageDao)
}
@Test
fun `initial state is Initialising`() {
viewModel.state.test().assertValue(pageState())
}
@Test
fun `Exit finishes activity`() {
viewModel.effects.test().also { viewModel.actions.offer(Action.Exit) }.assertValue(Finish)
viewModel.state.test().assertValue(state)
viewModel.effects.test().also { viewModel.actions.offer(Exit) }.assertValue(Finish)
viewModel.state.test().assertValue(pageState())
}
@Test
internal fun `OnItemClick selects item if one is selected`() {
val page = PageImpl(isSelected = true)
viewModel.state.postValue(TestablePageState(listOf(page)))
viewModel.actions.offer(Action.OnItemClick(page))
viewModel.state.test().assertValue(TestablePageState(listOf(PageImpl())))
}
@Test
internal fun `OnItemClick offers OpenPage if none is selected`() {
viewModel.state.postValue(TestablePageState(listOf(PageImpl())))
viewModel.effects.test().also { viewModel.actions.offer(Action.OnItemClick(PageImpl())) }
.assertValue(OpenPage(PageImpl(), zimReaderContainer))
viewModel.state.test().assertValue(TestablePageState(listOf(PageImpl())))
}
@Test
internal fun `OnItemLongClick selects item if none is selected`() {
val page = PageImpl()
viewModel.state.postValue(TestablePageState(listOf(page)))
viewModel.actions.offer(Action.OnItemLongClick(page))
viewModel.state.test().assertValue(TestablePageState(listOf(PageImpl(isSelected = true))))
}
}

View File

@ -23,19 +23,17 @@ import org.kiwix.kiwixmobile.core.base.SideEffect
import org.kiwix.kiwixmobile.core.dao.PageDao
import org.kiwix.kiwixmobile.core.page.adapter.Page
import org.kiwix.kiwixmobile.core.page.adapter.PageRelated
import org.kiwix.kiwixmobile.core.page.pageState
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
class TestablePageViewModel(
override val zimReaderContainer: ZimReaderContainer,
override val sharedPreferenceUtil: SharedPreferenceUtil,
zimReaderContainer: ZimReaderContainer,
sharedPreferenceUtil: SharedPreferenceUtil,
val dao: PageDao
) : PageViewModel<Page, TestablePageState>(dao) {
override fun initialState(): TestablePageState = TestablePageState()
) : PageViewModel<Page, TestablePageState>(dao, sharedPreferenceUtil, zimReaderContainer) {
init {
addDisposablesToCompositeDisposable()
}
override fun initialState(): TestablePageState = pageState()
override fun updatePagesBasedOnFilter(
state: TestablePageState,
@ -62,7 +60,7 @@ class TestablePageViewModel(
override fun createDeletePageDialogEffect(state: TestablePageState): SideEffect<*> = mockk()
}
class TestablePageState(
data class TestablePageState(
override val pageItems: List<Page> = emptyList(),
override val visiblePageItems: List<PageRelated> = pageItems,
override val showAll: Boolean = true,