|
1 | 1 | package com.woocommerce.android.ui.login.jetpack.wpcom |
2 | 2 |
|
3 | | -import androidx.lifecycle.SavedStateHandle |
| 3 | +import com.woocommerce.android.OnChangedException |
| 4 | +import com.woocommerce.android.R |
4 | 5 | import com.woocommerce.android.analytics.AnalyticsTrackerWrapper |
| 6 | +import com.woocommerce.android.model.JetpackStatus |
5 | 7 | import com.woocommerce.android.ui.login.WPComLoginRepository |
| 8 | +import com.woocommerce.android.ui.login.jetpack.wpcom.JetpackActivationWPComEmailViewModel.ShowMagicLinkScreen |
| 9 | +import com.woocommerce.android.ui.login.jetpack.wpcom.JetpackActivationWPComEmailViewModel.ShowPasswordScreen |
| 10 | +import com.woocommerce.android.util.StringUtils |
| 11 | +import com.woocommerce.android.util.runAndCaptureValues |
6 | 12 | import com.woocommerce.android.viewmodel.BaseUnitTest |
7 | 13 | import com.woocommerce.android.viewmodel.MultiLiveEvent |
8 | 14 | import kotlinx.coroutines.ExperimentalCoroutinesApi |
9 | | -import org.junit.Before |
10 | | -import org.junit.Test |
11 | | -import org.mockito.MockitoAnnotations |
| 15 | +import org.assertj.core.api.Assertions.assertThat |
12 | 16 | import org.mockito.kotlin.mock |
13 | 17 | import org.mockito.kotlin.verify |
| 18 | +import org.mockito.kotlin.whenever |
| 19 | +import org.wordpress.android.fluxc.store.AccountStore.AuthOptionsError |
| 20 | +import org.wordpress.android.fluxc.store.AccountStore.AuthOptionsErrorType |
| 21 | +import org.wordpress.android.login.AuthOptions |
| 22 | +import kotlin.test.Test |
| 23 | +import kotlin.test.assertTrue |
14 | 24 |
|
15 | | -@ExperimentalCoroutinesApi |
16 | 25 | @OptIn(ExperimentalCoroutinesApi::class) |
17 | 26 | class JetpackActivationWPComEmailViewModelTest : BaseUnitTest() { |
| 27 | + companion object { |
| 28 | + const val WPCOM_EMAIL = "[email protected]" |
| 29 | + const val UNKNOWN_EMAIL = "[email protected]" |
| 30 | + const val UNKNOWN_USERNAME = "newUser" |
| 31 | + val JETPACK_STATUS = JetpackStatus( |
| 32 | + isJetpackInstalled = true, |
| 33 | + isJetpackConnected = false, |
| 34 | + wpComEmail = "" |
| 35 | + ) |
| 36 | + } |
18 | 37 |
|
19 | | - private val savedStateHandle: SavedStateHandle = mock() |
| 38 | + private val saveStateHandle = JetpackActivationWPComEmailFragmentArgs( |
| 39 | + jetpackStatus = JETPACK_STATUS, |
| 40 | + ).toSavedStateHandle() |
20 | 41 | private val wpComLoginRepository: WPComLoginRepository = mock() |
21 | 42 | private val analyticsTrackerWrapper: AnalyticsTrackerWrapper = mock() |
| 43 | + private val stringUtils: StringUtils = mock() |
22 | 44 | private lateinit var viewModel: JetpackActivationWPComEmailViewModel |
23 | 45 |
|
24 | | - @Before |
25 | | - fun setUp() { |
26 | | - MockitoAnnotations.openMocks(this) |
| 46 | + fun setup(wpComEmail: String) { |
| 47 | + saveStateHandle["email"] = wpComEmail |
27 | 48 | viewModel = JetpackActivationWPComEmailViewModel( |
28 | | - savedStateHandle, |
29 | | - wpComLoginRepository, |
30 | | - analyticsTrackerWrapper |
| 49 | + savedStateHandle = saveStateHandle, |
| 50 | + wpComLoginRepository = wpComLoginRepository, |
| 51 | + analyticsTrackerWrapper = analyticsTrackerWrapper, |
| 52 | + stringUtils = stringUtils |
31 | 53 | ) |
32 | 54 | } |
33 | 55 |
|
34 | 56 | @Test |
35 | 57 | fun `when close is clicked, then trigger Exit event and clear access token`() = testBlocking { |
36 | | - viewModel.onCloseClick() |
| 58 | + setup(WPCOM_EMAIL) |
| 59 | + val event = viewModel.event.runAndCaptureValues { |
| 60 | + viewModel.onCloseClick() |
| 61 | + }.last() |
37 | 62 |
|
38 | | - assert(viewModel.event.value is MultiLiveEvent.Event.Exit) |
| 63 | + assertTrue(event is MultiLiveEvent.Event.Exit) |
39 | 64 | verify(wpComLoginRepository).clearAccessToken() |
40 | 65 | } |
41 | | -} |
42 | 66 |
|
| 67 | + @Test |
| 68 | + fun `given email is not WPcom, when onContinueClick clicked, then trigger ShowMagicLinkScreen with new wpcom account true`() = |
| 69 | + testBlocking { |
| 70 | + setup(UNKNOWN_EMAIL) |
| 71 | + whenever(stringUtils.isValidEmail(UNKNOWN_EMAIL)).thenReturn(true) |
| 72 | + whenever(wpComLoginRepository.fetchAuthOptions(UNKNOWN_EMAIL)) |
| 73 | + .thenReturn( |
| 74 | + Result.failure(OnChangedException(AuthOptionsError(AuthOptionsErrorType.UNKNOWN_USER, ""))) |
| 75 | + ) |
| 76 | + |
| 77 | + val event = viewModel.event.runAndCaptureValues { |
| 78 | + viewModel.onContinueClick() |
| 79 | + }.last() |
| 80 | + |
| 81 | + assertThat(event).isEqualTo( |
| 82 | + ShowMagicLinkScreen( |
| 83 | + UNKNOWN_EMAIL, |
| 84 | + JETPACK_STATUS, |
| 85 | + isNewWpComAccount = true |
| 86 | + ) |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `given username is not WPcom, when onContinueClick clicked, then show user name not wpcom error`() = |
| 92 | + testBlocking { |
| 93 | + setup(UNKNOWN_USERNAME) |
| 94 | + whenever(stringUtils.isValidEmail(UNKNOWN_USERNAME)).thenReturn(false) |
| 95 | + whenever(wpComLoginRepository.fetchAuthOptions(UNKNOWN_USERNAME)).thenReturn( |
| 96 | + Result.failure( |
| 97 | + OnChangedException( |
| 98 | + AuthOptionsError(AuthOptionsErrorType.UNKNOWN_USER, "") |
| 99 | + ) |
| 100 | + ) |
| 101 | + ) |
| 102 | + |
| 103 | + val state = viewModel.viewState.runAndCaptureValues { |
| 104 | + viewModel.onContinueClick() |
| 105 | + }.last() |
| 106 | + |
| 107 | + assertThat(state.errorMessage).isEqualTo(R.string.username_not_registered_wpcom) |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + fun `given email not allowed, when onContinueClick clicked, then show user name not wpcom error`() = |
| 112 | + testBlocking { |
| 113 | + setup(UNKNOWN_USERNAME) |
| 114 | + whenever(wpComLoginRepository.fetchAuthOptions(UNKNOWN_USERNAME)).thenReturn( |
| 115 | + Result.failure( |
| 116 | + OnChangedException( |
| 117 | + AuthOptionsError(AuthOptionsErrorType.EMAIL_LOGIN_NOT_ALLOWED, "") |
| 118 | + ) |
| 119 | + ) |
| 120 | + ) |
| 121 | + |
| 122 | + val state = viewModel.viewState.runAndCaptureValues { |
| 123 | + viewModel.onContinueClick() |
| 124 | + }.last() |
| 125 | + |
| 126 | + assertThat(state.errorMessage).isEqualTo(R.string.error_user_username_instead_of_email) |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + fun `given email is WPcom user, when onContinueClick clicked, then trigger ShowPasswordScreen`() = |
| 131 | + testBlocking { |
| 132 | + setup(WPCOM_EMAIL) |
| 133 | + whenever(wpComLoginRepository.fetchAuthOptions(WPCOM_EMAIL)).thenReturn( |
| 134 | + Result.success( |
| 135 | + AuthOptions( |
| 136 | + isPasswordless = false, |
| 137 | + isEmailVerified = true |
| 138 | + ) |
| 139 | + ) |
| 140 | + ) |
| 141 | + |
| 142 | + val event = viewModel.event.runAndCaptureValues { |
| 143 | + viewModel.onContinueClick() |
| 144 | + }.last() |
| 145 | + |
| 146 | + assertThat(event).isEqualTo( |
| 147 | + ShowPasswordScreen( |
| 148 | + WPCOM_EMAIL, |
| 149 | + JETPACK_STATUS |
| 150 | + ) |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + fun `given email is WPcom passwordless user, when onContinueClick clicked, then trigger ShowPasswordScreen`() = |
| 156 | + testBlocking { |
| 157 | + setup(WPCOM_EMAIL) |
| 158 | + whenever(wpComLoginRepository.fetchAuthOptions(WPCOM_EMAIL)).thenReturn( |
| 159 | + Result.success( |
| 160 | + AuthOptions( |
| 161 | + isPasswordless = true, |
| 162 | + isEmailVerified = true |
| 163 | + ) |
| 164 | + ) |
| 165 | + ) |
| 166 | + |
| 167 | + val event = viewModel.event.runAndCaptureValues { |
| 168 | + viewModel.onContinueClick() |
| 169 | + }.last() |
| 170 | + |
| 171 | + assertThat(event).isEqualTo( |
| 172 | + ShowMagicLinkScreen( |
| 173 | + WPCOM_EMAIL, |
| 174 | + JETPACK_STATUS, |
| 175 | + isNewWpComAccount = false |
| 176 | + ) |
| 177 | + ) |
| 178 | + } |
| 179 | +} |
0 commit comments