-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dca82f3
commit ca4136a
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...ntSample/search/src/test/java/com/novoda/movies/mvi/search/domain/SearchMiddlewareTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.novoda.movies.mvi.search.domain | ||
|
||
import com.nhaarman.mockitokotlin2.doReturn | ||
import com.nhaarman.mockitokotlin2.mock | ||
import com.nhaarman.mockitokotlin2.stub | ||
import com.novoda.movies.mvi.search.data.SearchBackend | ||
import com.novoda.movies.mvi.search.presentation.ViewSearchResults | ||
import io.reactivex.Single | ||
import io.reactivex.observers.TestObserver | ||
import io.reactivex.schedulers.Schedulers | ||
import io.reactivex.subjects.PublishSubject | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class SearchMiddlewareTest { | ||
|
||
private val backend: SearchBackend = mock() | ||
private val searchMiddleware = SearchMiddleware(backend, Schedulers.trampoline()) | ||
|
||
private val actions = PublishSubject.create<SearchAction>() | ||
private val state = PublishSubject.create<SearchState>() | ||
private lateinit var changes: TestObserver<SearchChanges> | ||
|
||
@Before | ||
fun setUp() { | ||
changes = searchMiddleware.bind(actions, state).test() | ||
} | ||
|
||
@Test | ||
fun `GIVEN state with query WHEN query changed THEN query is updated`() { | ||
state.onNext(SearchState.Loading(queryString = "iron man")) | ||
|
||
actions.onNext(SearchAction.ChangeQuery(queryString = "superman")) | ||
|
||
changes.assertValue(SearchChanges.SearchQueryUpdate("superman")) | ||
} | ||
|
||
@Test | ||
fun `GIVEN state with query WHEN query cleared THEN updated query is empty`() { | ||
state.onNext(SearchState.Loading(queryString = "iron man")) | ||
|
||
actions.onNext(SearchAction.ClearQuery) | ||
|
||
changes.assertValue(SearchChanges.SearchQueryUpdate("")) | ||
} | ||
|
||
@Test | ||
fun `GIVEN backend has results WHEN execute search THEN search is in progress AND search is completed`() { | ||
val searchResults = SearchResults(items = listOf()) | ||
state.onNext(SearchState.Content(queryString = "iron man", results = ViewSearchResults())) | ||
backend.stub { on { search("iron man") } doReturn Single.just(searchResults) } | ||
|
||
actions.onNext(SearchAction.ExecuteSearch) | ||
|
||
changes.assertValues( | ||
SearchChanges.SearchInProgress, | ||
SearchChanges.SearchCompleted(results = searchResults) | ||
) | ||
} | ||
|
||
@Test | ||
fun `GIVEN backend errors WHEN execute search THEN search is in progress AND search failed`() { | ||
val exception = IllegalStateException("backend is down") | ||
state.onNext(SearchState.Content(queryString = "iron man", results = ViewSearchResults())) | ||
backend.stub { on { search("iron man") } doReturn (Single.error(exception)) } | ||
|
||
actions.onNext(SearchAction.ExecuteSearch) | ||
|
||
changes.assertValues( | ||
SearchChanges.SearchInProgress, | ||
SearchChanges.SearchFailed(exception) | ||
) | ||
} | ||
} |
ca4136a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Easy Peasy! Testing of the Middle Logic is super trivial with this architecture 👍