Skip to content

Instantly share code, notes, and snippets.

@azzumw
Created May 30, 2022 14:54
Show Gist options
  • Save azzumw/cc53049cfda67106d415d2139bc6818a to your computer and use it in GitHub Desktop.
Save azzumw/cc53049cfda67106d415d2139bc6818a to your computer and use it in GitHub Desktop.
TaskViewModelTest class from TODO app Udacity
package com.example.android.architecture.blueprints.todoapp.tasks
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.android.architecture.blueprints.todoapp.data.Task
import com.example.android.architecture.blueprints.todoapp.data.source.FakeTestRepository
import kotlinx.coroutines.runBlocking
import org.hamcrest.CoreMatchers.*
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.concurrent.timerTask
class TasksViewModelTest{
private lateinit var taskViewModel:TasksViewModel
private lateinit var taskRepository: FakeTestRepository
@get:Rule
var instantExecutorRule = InstantTaskExecutorRule()
@Before
fun setupViewModel() {
taskRepository = FakeTestRepository()
val task1 = Task("Title1", "Description1")
val task2 = Task("Title2", "Description2", true)
val task3 = Task("Title3", "Description3", true)
taskRepository.addTasks(task1, task2, task3)
taskViewModel = TasksViewModel(taskRepository)
}
@Test
fun getTasks() = runBlocking {
val tasks = taskRepository.getTasks()
val value = taskViewModel.tasksAddViewVisible.getOrAwaitValue()
assertThat(value,`is`(true))
}
@Test
fun clearCompletedTasks() = runBlocking {
val task1 = Task("Title1", "Description1")
taskViewModel.completeTask(task1,true)
taskViewModel.clearCompletedTasks()
}
@Test
fun addNewTask_setsNewTaskEvent(){
//GIVEN: a fresh viewModel
//WHEN: adding a new task
taskViewModel.addNewTask()
//THEN: the new task event is triggered
// TODO test LiveData
val value = taskViewModel.newTaskEvent.getOrAwaitValue()
assertThat(value.getContentIfNotHandled(), (not(nullValue())))
}
@Test
fun setFilterAllTasks_tasksAddViewVisible() {
// Given a fresh ViewModel
// When the filter type is ALL_TASKS
taskViewModel.setFiltering(TasksFilterType.ALL_TASKS)
// Then the "Add task" action is visible
assertThat(taskViewModel.tasksAddViewVisible.getOrAwaitValue(), `is`(true))
}
@Test
fun setFilterActiveTasks_tasksAddViewNotVisible(){
//Given viewmodel
//when the filter type is ACTIVE_TASKS
taskViewModel.setFiltering(TasksFilterType.ACTIVE_TASKS)
assertThat(taskViewModel.tasksAddViewVisible.getOrAwaitValue(), `is`(false))
}
@Test
fun setFilterCompletedTasks_tasksAddViewNotVisible(){
//given the viewModel
//when the filter is completed tasks
taskViewModel.setFiltering(TasksFilterType.COMPLETED_TASKS)
assertThat(
taskViewModel.tasksAddViewVisible.getOrAwaitValue(), `is`(false)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment