Created
May 30, 2022 14:54
-
-
Save azzumw/cc53049cfda67106d415d2139bc6818a to your computer and use it in GitHub Desktop.
TaskViewModelTest class from TODO app Udacity
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
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