|
| 1 | +package es.ffgiraldez.comicsearch.query.search.ui |
| 2 | + |
| 3 | +import android.view.View |
| 4 | +import android.widget.FrameLayout |
| 5 | +import android.widget.ProgressBar |
| 6 | +import android.widget.TextView |
| 7 | +import androidx.recyclerview.widget.RecyclerView |
| 8 | +import com.arlib.floatingsearchview.FloatingSearchView |
| 9 | +import com.arlib.floatingsearchview.FloatingSearchView.OnSearchListener |
| 10 | +import com.nhaarman.mockitokotlin2.argumentCaptor |
| 11 | +import com.nhaarman.mockitokotlin2.doNothing |
| 12 | +import com.nhaarman.mockitokotlin2.eq |
| 13 | +import com.nhaarman.mockitokotlin2.mock |
| 14 | +import com.nhaarman.mockitokotlin2.verify |
| 15 | +import com.nhaarman.mockitokotlin2.verifyZeroInteractions |
| 16 | +import com.nhaarman.mockitokotlin2.whenever |
| 17 | +import es.ffgiraldez.comicsearch.comic.gen.searchErrorViewState |
| 18 | +import es.ffgiraldez.comicsearch.comic.gen.searchViewState |
| 19 | +import es.ffgiraldez.comicsearch.query.base.presentation.QueryViewState |
| 20 | +import es.ffgiraldez.comicsearch.query.base.ui.OnVolumeSelectedListener |
| 21 | +import es.ffgiraldez.comicsearch.query.base.ui.QuerySearchSuggestion.ErrorSuggestion |
| 22 | +import es.ffgiraldez.comicsearch.query.base.ui.QuerySearchSuggestion.ResultSuggestion |
| 23 | +import es.ffgiraldez.comicsearch.query.base.ui.QueryVolumeAdapter |
| 24 | +import es.ffgiraldez.comicsearch.query.base.ui.results |
| 25 | +import es.ffgiraldez.comicsearch.query.base.ui.toHumanResponse |
| 26 | +import io.kotlintest.properties.Gen |
| 27 | +import io.kotlintest.properties.assertAll |
| 28 | +import io.kotlintest.specs.WordSpec |
| 29 | + |
| 30 | +class SearchBindingAdapterSpec : WordSpec({ |
| 31 | + "ProgressBar" should { |
| 32 | + "not have interaction on null state" { |
| 33 | + val progressBar = mock<ProgressBar>() |
| 34 | + |
| 35 | + progressBar.bindProgress(null) |
| 36 | + |
| 37 | + verifyZeroInteractions(progressBar) |
| 38 | + } |
| 39 | + |
| 40 | + "be visible on loading state" { |
| 41 | + val progressBar = mock<ProgressBar>() |
| 42 | + |
| 43 | + progressBar.bindProgress(QueryViewState.loading()) |
| 44 | + |
| 45 | + verify(progressBar).visibility = eq(View.VISIBLE) |
| 46 | + } |
| 47 | + |
| 48 | + "be gone on non loading state" { |
| 49 | + assertAll(Gen.searchViewState().filterNot { it is QueryViewState.Loading }) { state -> |
| 50 | + val progressBar = mock<ProgressBar>() |
| 51 | + |
| 52 | + progressBar.bindProgress(state) |
| 53 | + |
| 54 | + verify(progressBar).visibility = eq(View.GONE) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + "TextView" should { |
| 61 | + |
| 62 | + "not have interaction on null state" { |
| 63 | + val textView = mock<TextView>() |
| 64 | + |
| 65 | + textView.bindErrorText(null) |
| 66 | + |
| 67 | + verifyZeroInteractions(textView) |
| 68 | + } |
| 69 | + |
| 70 | + "not have interaction on non error state" { |
| 71 | + assertAll(Gen.searchViewState().filterNot { it is QueryViewState.Error }) { state -> |
| 72 | + val textView = mock<TextView>() |
| 73 | + |
| 74 | + textView.bindErrorText(state) |
| 75 | + |
| 76 | + verifyZeroInteractions(textView) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + "show error human description on error state" { |
| 81 | + assertAll(Gen.searchErrorViewState()) { state -> |
| 82 | + val textView = mock<TextView>() |
| 83 | + |
| 84 | + textView.bindErrorText(state) |
| 85 | + |
| 86 | + verify(textView).text = eq(state._error.toHumanResponse()) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + "FrameLayout" should { |
| 92 | + "not have interaction on null state" { |
| 93 | + val frameLayout = mock<FrameLayout>() |
| 94 | + |
| 95 | + frameLayout.bindStateVisibility(null) |
| 96 | + |
| 97 | + verifyZeroInteractions(frameLayout) |
| 98 | + } |
| 99 | + |
| 100 | + "be gone on non error state" { |
| 101 | + assertAll(Gen.searchViewState().filterNot { it is QueryViewState.Error }) { state -> |
| 102 | + val frameLayout = mock<FrameLayout>() |
| 103 | + |
| 104 | + frameLayout.bindStateVisibility(state) |
| 105 | + |
| 106 | + |
| 107 | + verify(frameLayout).visibility = eq(View.GONE) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + "be visible on error state" { |
| 112 | + assertAll(Gen.searchErrorViewState()) { state -> |
| 113 | + val frameLayout = mock<FrameLayout>() |
| 114 | + |
| 115 | + frameLayout.bindStateVisibility(state) |
| 116 | + |
| 117 | + |
| 118 | + verify(frameLayout).visibility = eq(View.VISIBLE) |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + "RecyclerView" should { |
| 124 | + "configure is adapter when is not defined" { |
| 125 | + val recyclerView = mock<RecyclerView>() |
| 126 | + val adapter = mock<QueryVolumeAdapter>() |
| 127 | + val onVolumeSelected = mock<OnVolumeSelectedListener>() |
| 128 | + |
| 129 | + recyclerView.bindStateData(adapter, null, onVolumeSelected) |
| 130 | + |
| 131 | + verify(recyclerView).adapter = eq(adapter) |
| 132 | + verify(adapter).onVolumeSelectedListener = eq(onVolumeSelected) |
| 133 | + } |
| 134 | + |
| 135 | + "update result list on state change" { |
| 136 | + assertAll(Gen.searchViewState()) { state -> |
| 137 | + val adapter = mock<QueryVolumeAdapter>() |
| 138 | + val recyclerView = mock<RecyclerView> { |
| 139 | + on { getAdapter() }.thenReturn(adapter) |
| 140 | + } |
| 141 | + val onVolumeSelected = mock<OnVolumeSelectedListener>() |
| 142 | + |
| 143 | + recyclerView.bindStateData(adapter, state, onVolumeSelected) |
| 144 | + |
| 145 | + verify(adapter).submitList(eq(state.results)) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + "be visible on non error state" { |
| 151 | + assertAll(Gen.searchViewState().filterNot { it is QueryViewState.Error }) { state -> |
| 152 | + val adapter = mock<QueryVolumeAdapter>() |
| 153 | + val recyclerView = mock<RecyclerView> { |
| 154 | + on { getAdapter() }.thenReturn(adapter) |
| 155 | + } |
| 156 | + val onVolumeSelected = mock<OnVolumeSelectedListener>() |
| 157 | + |
| 158 | + recyclerView.bindStateData(adapter, state, onVolumeSelected) |
| 159 | + |
| 160 | + verify(recyclerView).visibility = eq(View.VISIBLE) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + "be gone on non error state" { |
| 165 | + assertAll(Gen.searchErrorViewState()) { state -> |
| 166 | + val adapter = mock<QueryVolumeAdapter>() |
| 167 | + val recyclerView = mock<RecyclerView> { |
| 168 | + on { getAdapter() }.thenReturn(adapter) |
| 169 | + } |
| 170 | + val onVolumeSelected = mock<OnVolumeSelectedListener>() |
| 171 | + |
| 172 | + recyclerView.bindStateData(adapter, state, onVolumeSelected) |
| 173 | + |
| 174 | + verify(recyclerView).visibility = eq(View.GONE) |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + "FloatingView" should { |
| 180 | + "avoid search on error suggestion click" { |
| 181 | + val searchListenerCaptor = argumentCaptor<OnSearchListener>() |
| 182 | + val searchView = mock<FloatingSearchView> { |
| 183 | + doNothing().whenever(it).setOnSearchListener(searchListenerCaptor.capture()) |
| 184 | + } |
| 185 | + val click = mock<ClickConsumer>() |
| 186 | + |
| 187 | + |
| 188 | + searchView.bindSuggestionClick(click, mock()) |
| 189 | + |
| 190 | + searchListenerCaptor.firstValue.onSuggestionClicked(ErrorSuggestion(Gen.string().random().first())) |
| 191 | + |
| 192 | + verifyZeroInteractions(click) |
| 193 | + } |
| 194 | + |
| 195 | + "search on result suggestion click" { |
| 196 | + val searchListenerCaptor = argumentCaptor<OnSearchListener>() |
| 197 | + val searchView = mock<FloatingSearchView> { |
| 198 | + doNothing().whenever(it).setOnSearchListener(searchListenerCaptor.capture()) |
| 199 | + } |
| 200 | + val click = mock<ClickConsumer>() |
| 201 | + val suggestion = ResultSuggestion(Gen.string().random().first()) |
| 202 | + |
| 203 | + |
| 204 | + searchView.bindSuggestionClick(click, mock()) |
| 205 | + |
| 206 | + searchListenerCaptor.firstValue.onSuggestionClicked(suggestion) |
| 207 | + |
| 208 | + verify(click).accept(eq(suggestion)) |
| 209 | + } |
| 210 | + } |
| 211 | +}) |
0 commit comments