|
| 1 | +package net.mullvad.mullvadvpn.compose.dialog |
| 2 | + |
| 3 | +import androidx.compose.ui.test.ExperimentalTestApi |
| 4 | +import androidx.compose.ui.test.onNodeWithTag |
| 5 | +import androidx.compose.ui.test.onNodeWithText |
| 6 | +import androidx.compose.ui.test.performClick |
| 7 | +import androidx.compose.ui.test.performTextInput |
| 8 | +import io.mockk.MockKAnnotations |
| 9 | +import io.mockk.mockk |
| 10 | +import io.mockk.verify |
| 11 | +import net.mullvad.mullvadvpn.compose.createEdgeToEdgeComposeExtension |
| 12 | +import net.mullvad.mullvadvpn.compose.setContentWithTheme |
| 13 | +import net.mullvad.mullvadvpn.compose.state.CreateCustomListUiState |
| 14 | +import net.mullvad.mullvadvpn.compose.test.CREATE_CUSTOM_LIST_DIALOG_INPUT_TEST_TAG |
| 15 | +import net.mullvad.mullvadvpn.model.CustomListsError |
| 16 | +import org.junit.jupiter.api.BeforeEach |
| 17 | +import org.junit.jupiter.api.Test |
| 18 | +import org.junit.jupiter.api.extension.RegisterExtension |
| 19 | + |
| 20 | +class CreateCustomListDialogTest { |
| 21 | + @OptIn(ExperimentalTestApi::class) |
| 22 | + @JvmField |
| 23 | + @RegisterExtension |
| 24 | + val composeExtension = createEdgeToEdgeComposeExtension() |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + fun setup() { |
| 28 | + MockKAnnotations.init(this) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun givenNoErrorShouldShowNoErrorMessage() = |
| 33 | + composeExtension.use { |
| 34 | + // Arrange |
| 35 | + val state = CreateCustomListUiState(error = null) |
| 36 | + setContentWithTheme { CreateCustomListDialog(state = state) } |
| 37 | + |
| 38 | + // Assert |
| 39 | + onNodeWithText(NAME_EXIST_ERROR_TEXT).assertDoesNotExist() |
| 40 | + onNodeWithText(OTHER_ERROR_TEXT).assertDoesNotExist() |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun givenCustomListExistsShouldShowCustomListExitsErrorText() = |
| 45 | + composeExtension.use { |
| 46 | + // Arrange |
| 47 | + val state = CreateCustomListUiState(error = CustomListsError.CustomListExists) |
| 48 | + setContentWithTheme { CreateCustomListDialog(state = state) } |
| 49 | + |
| 50 | + // Assert |
| 51 | + onNodeWithText(NAME_EXIST_ERROR_TEXT).assertExists() |
| 52 | + onNodeWithText(OTHER_ERROR_TEXT).assertDoesNotExist() |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun givenOtherCustomListErrorShouldShowAnErrorOccurredErrorText() = |
| 57 | + composeExtension.use { |
| 58 | + // Arrange |
| 59 | + val state = CreateCustomListUiState(error = CustomListsError.OtherError) |
| 60 | + setContentWithTheme { CreateCustomListDialog(state = state) } |
| 61 | + |
| 62 | + // Assert |
| 63 | + onNodeWithText(NAME_EXIST_ERROR_TEXT).assertDoesNotExist() |
| 64 | + onNodeWithText(OTHER_ERROR_TEXT).assertExists() |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun whenCancelIsClickedShouldDismissDialog() = |
| 69 | + composeExtension.use { |
| 70 | + // Arrange |
| 71 | + val mockedOnDismiss: () -> Unit = mockk(relaxed = true) |
| 72 | + val state = CreateCustomListUiState() |
| 73 | + setContentWithTheme { |
| 74 | + CreateCustomListDialog(state = state, onDismiss = mockedOnDismiss) |
| 75 | + } |
| 76 | + |
| 77 | + // Act |
| 78 | + onNodeWithText(CANCEL_BUTTON_TEXT).performClick() |
| 79 | + |
| 80 | + // Assert |
| 81 | + verify { mockedOnDismiss.invoke() } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun givenEmptyTextInputWhenSubmitIsClickedThenShouldNotCallOnCreate() = |
| 86 | + composeExtension.use { |
| 87 | + // Arrange |
| 88 | + val mockedCreateCustomList: (String) -> Unit = mockk(relaxed = true) |
| 89 | + val state = CreateCustomListUiState() |
| 90 | + setContentWithTheme { |
| 91 | + CreateCustomListDialog(state = state, createCustomList = mockedCreateCustomList) |
| 92 | + } |
| 93 | + |
| 94 | + // Act |
| 95 | + onNodeWithText(CREATE_BUTTON_TEXT).performClick() |
| 96 | + |
| 97 | + // Assert |
| 98 | + verify(exactly = 0) { mockedCreateCustomList.invoke(any()) } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun givenValidTextInputWhenSubmitIsClickedThenShouldCallOnCreate() = |
| 103 | + composeExtension.use { |
| 104 | + // Arrange |
| 105 | + val mockedCreateCustomList: (String) -> Unit = mockk(relaxed = true) |
| 106 | + val inputText = "NEW LIST" |
| 107 | + val state = CreateCustomListUiState() |
| 108 | + setContentWithTheme { |
| 109 | + CreateCustomListDialog(state = state, createCustomList = mockedCreateCustomList) |
| 110 | + } |
| 111 | + |
| 112 | + // Act |
| 113 | + onNodeWithTag(CREATE_CUSTOM_LIST_DIALOG_INPUT_TEST_TAG).performTextInput(inputText) |
| 114 | + onNodeWithText(CREATE_BUTTON_TEXT).performClick() |
| 115 | + |
| 116 | + // Assert |
| 117 | + verify { mockedCreateCustomList.invoke(inputText) } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun whenInputIsChangedShouldCallOnInputChanged() = |
| 122 | + composeExtension.use { |
| 123 | + // Arrange |
| 124 | + val mockedOnInputChanged: () -> Unit = mockk(relaxed = true) |
| 125 | + val inputText = "NEW LIST" |
| 126 | + val state = CreateCustomListUiState() |
| 127 | + setContentWithTheme { |
| 128 | + CreateCustomListDialog(state = state, onInputChanged = mockedOnInputChanged) |
| 129 | + } |
| 130 | + |
| 131 | + // Act |
| 132 | + onNodeWithTag(CREATE_CUSTOM_LIST_DIALOG_INPUT_TEST_TAG).performTextInput(inputText) |
| 133 | + |
| 134 | + // Assert |
| 135 | + verify { mockedOnInputChanged.invoke() } |
| 136 | + } |
| 137 | + |
| 138 | + companion object { |
| 139 | + private const val NAME_EXIST_ERROR_TEXT = "Name is already taken." |
| 140 | + private const val OTHER_ERROR_TEXT = "An error occurred." |
| 141 | + private const val CANCEL_BUTTON_TEXT = "Cancel" |
| 142 | + private const val CREATE_BUTTON_TEXT = "Create" |
| 143 | + } |
| 144 | +} |
0 commit comments