-
Notifications
You must be signed in to change notification settings - Fork 137
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
Showing
1 changed file
with
135 additions
and
0 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
.../kotlin/com/woocommerce/android/ui/woopos/emailreceipt/WooPosEmailReceiptViewModelTest.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,135 @@ | ||
package com.woocommerce.android.ui.woopos.emailreceipt | ||
|
||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import androidx.lifecycle.SavedStateHandle | ||
import com.woocommerce.android.R | ||
import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule | ||
import com.woocommerce.android.viewmodel.ResourceProvider | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.test.advanceUntilIdle | ||
import kotlinx.coroutines.test.runTest | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
@ExperimentalCoroutinesApi | ||
class WooPosEmailReceiptViewModelTest { | ||
@get:Rule | ||
val coroutinesTestRule = WooPosCoroutineTestRule() | ||
|
||
@get:Rule | ||
val instantExecutorRule = InstantTaskExecutorRule() | ||
|
||
private val repository: WooPosEmailReceiptRepository = mock() | ||
private val resourceProvider: ResourceProvider = mock() | ||
|
||
private lateinit var viewModel: WooPosEmailReceiptViewModel | ||
|
||
@Before | ||
fun setUp() { | ||
val savedStateHandle = SavedStateHandle( | ||
mapOf(EMAIL_RECEIPT_ROUTE_ORDER_ID_KEY to 123L) | ||
) | ||
|
||
whenever(resourceProvider.getString(R.string.woopos_email_receipt_send_button)) | ||
.thenReturn("Send") | ||
whenever(resourceProvider.getString(R.string.woopos_email_receipt_send_error)) | ||
.thenReturn("Error sending email") | ||
|
||
whenever(repository.isEmailValid(any())).thenReturn(false) | ||
|
||
viewModel = WooPosEmailReceiptViewModel( | ||
repository = repository, | ||
resourceProvider = resourceProvider, | ||
savedState = savedStateHandle | ||
) | ||
} | ||
|
||
@Test | ||
fun `given initial state, when view model is created, then state should be Email with empty email and disabled button`() = runTest { | ||
// GIVEN | ||
|
||
// WHEN | ||
val state = viewModel.state.first() | ||
|
||
// THEN | ||
assertThat(state).isInstanceOf(WooPosEmailReceiptState.Email::class.java) | ||
val emailState = state as WooPosEmailReceiptState.Email | ||
assertThat(emailState.email).isEmpty() | ||
assertThat(emailState.errorMessage).isNull() | ||
assertThat(emailState.button.status).isEqualTo(WooPosEmailReceiptState.Email.Button.Status.DISABLED) | ||
} | ||
|
||
@Test | ||
fun `given invalid email, when EmailChanged called, then button should remain disabled`() = runTest { | ||
// GIVEN | ||
whenever(repository.isEmailValid("invalid")).thenReturn(false) | ||
|
||
// WHEN | ||
viewModel.onUIEvent(WooPosEmailReceiptUIEvent.EmailChanged("invalid")) | ||
val state = viewModel.state.first() | ||
|
||
// THEN | ||
val emailState = state as WooPosEmailReceiptState.Email | ||
assertThat(emailState.email).isEqualTo("invalid") | ||
assertThat(emailState.button.status).isEqualTo(WooPosEmailReceiptState.Email.Button.Status.DISABLED) | ||
} | ||
|
||
@Test | ||
fun `given valid email, when EmailChanged called, then button should become enabled`() = runTest { | ||
// GIVEN | ||
whenever(repository.isEmailValid("[email protected]")).thenReturn(true) | ||
|
||
// WHEN | ||
viewModel.onUIEvent(WooPosEmailReceiptUIEvent.EmailChanged("[email protected]")) | ||
val state = viewModel.state.first() | ||
|
||
// THEN | ||
val emailState = state as WooPosEmailReceiptState.Email | ||
assertThat(emailState.email).isEqualTo("[email protected]") | ||
assertThat(emailState.errorMessage).isNull() | ||
assertThat(emailState.button.status).isEqualTo(WooPosEmailReceiptState.Email.Button.Status.ENABLED) | ||
} | ||
|
||
@Test | ||
fun `given valid email and successful send, when SendEmailClicked called, then state should become Sent`() = runTest { | ||
// GIVEN | ||
whenever(repository.isEmailValid("[email protected]")).thenReturn(true) | ||
viewModel.onUIEvent(WooPosEmailReceiptUIEvent.EmailChanged("[email protected]")) | ||
whenever(repository.sendReceiptByEmail(orderId = 123L, "[email protected]")) | ||
.thenReturn(Result.success(Unit)) | ||
|
||
// WHEN | ||
viewModel.onUIEvent(WooPosEmailReceiptUIEvent.SendEmailClicked) | ||
advanceUntilIdle() | ||
|
||
// THEN | ||
val state = viewModel.state.value | ||
assertThat(state).isInstanceOf(WooPosEmailReceiptState.Sent::class.java) | ||
} | ||
|
||
@Test | ||
fun `given valid email and failed send, when SendEmailClicked called, then show error message and re-enable button`() = runTest { | ||
// GIVEN | ||
whenever(repository.isEmailValid("[email protected]")).thenReturn(true) | ||
viewModel.onUIEvent(WooPosEmailReceiptUIEvent.EmailChanged("[email protected]")) | ||
whenever(repository.sendReceiptByEmail(orderId = 123L, "[email protected]")) | ||
.thenReturn(Result.failure(RuntimeException("Failed"))) | ||
|
||
// WHEN | ||
viewModel.onUIEvent(WooPosEmailReceiptUIEvent.SendEmailClicked) | ||
advanceUntilIdle() | ||
|
||
// THEN | ||
val state = viewModel.state.value | ||
assertThat(state).isInstanceOf(WooPosEmailReceiptState.Email::class.java) | ||
val emailState = state as WooPosEmailReceiptState.Email | ||
assertThat(emailState.errorMessage).isEqualTo("Error sending email") | ||
assertThat(emailState.button.status).isEqualTo(WooPosEmailReceiptState.Email.Button.Status.ENABLED) | ||
} | ||
} |