Skip to content

Instantly share code, notes, and snippets.

@featzima
Created February 21, 2019 14:57
Show Gist options
  • Save featzima/b47978f8edfefde931401c53dce3febf to your computer and use it in GitHub Desktop.
Save featzima/b47978f8edfefde931401c53dce3febf to your computer and use it in GitHub Desktop.
package com.olearis.notate.ui.screen.settinglist
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.nhaarman.mockitokotlin2.times
import com.olearis.notate.interactor.INoteInteractor
import com.olearis.notate.interactor.INoteListInteractor
import com.olearis.notate.model.SortCriteria
import com.olearis.notate.model.SortDirection
import com.olearis.notate.model.SortOrder
import com.olearis.notate.model.UniId
import com.olearis.notate.repository.IUserPreferencesRepository
import com.olearis.notate.repository.notebooks.INotebooksRepository
import com.olearis.notate.repository.notebooks.ISmartNotebookRepository
import com.olearis.notate.repository.notes.NotesRepository
import com.olearis.notate.repository.openin.IOpenInRepository
import com.olearis.notate.repository.policy.IPolicyRepository
import com.olearis.notate.ui.screen.navigation.NavigationModel
import com.olearis.notate.ui.screen.notelist.NoteListViewModel
import com.olearis.notate.ui.usecase.GetTitleForFolder
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.MockitoAnnotations
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class NoteListViewModelTest {
@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()
@Mock lateinit var navigationModel: NavigationModel
@Mock lateinit var openInRepository: IOpenInRepository
@Mock lateinit var policyRepository: IPolicyRepository
@Mock lateinit var notesRepository: NotesRepository
@Mock lateinit var notebookRepository: INotebooksRepository
@Mock lateinit var smartNotebookRepository: ISmartNotebookRepository
@Mock lateinit var getTitleForFolder: GetTitleForFolder
@Mock lateinit var noteListInteractor: INoteListInteractor
@Mock lateinit var noteInteractor: INoteInteractor
@Mock lateinit var notebookId: UniId
@Mock lateinit var userPreferenceRepository: IUserPreferencesRepository
private fun createViewModel() = NoteListViewModel(
navigationModel,
openInRepository,
policyRepository,
notesRepository,
notebookRepository,
smartNotebookRepository,
getTitleForFolder,
noteListInteractor,
noteInteractor,
notebookId,
userPreferenceRepository)
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
}
@Test
fun testSortOrderLoading() {
val userSortOrder = SortOrder(SortCriteria.SORT_BY_TITLE, SortDirection.SORT_ASC)
Mockito.`when`(userPreferenceRepository.notebookSortOrder).thenReturn(userSortOrder)
val viewModel = createViewModel()
Assert.assertEquals(userSortOrder, viewModel.sortOrder.value)
}
@Test
fun testSortOrderSaving() {
val userSortOrder = SortOrder(SortCriteria.SORT_BY_CREATED_DATE, SortDirection.SORT_DESC)
val viewModel = createViewModel()
viewModel.setSortOrder(userSortOrder)
Mockito.verify(userPreferenceRepository, times(1)).notebookSortOrder = userSortOrder
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment