Skip to content

Commit

Permalink
Update logic to disable the done button instead of hiding it
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Oct 2, 2024
1 parent c04e5f3 commit ec00c7f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,11 @@ private fun CustomFieldsEditorScreen(
title = "Custom Field",
onNavigationButtonClick = onBackButtonClick,
actions = {
if (state.showDoneButton) {
WCTextButton(
onClick = onDoneClicked,
text = stringResource(R.string.done)
)
}
WCTextButton(
onClick = onDoneClicked,
text = stringResource(R.string.done),
enabled = state.enableDoneButton
)
WCOverflowMenu(
items = listOfNotNull(
R.string.custom_fields_editor_copy_key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class CustomFieldsEditorViewModel @Inject constructor(
val keyErrorMessage: UiString? = null,
val isCreatingNewItem: Boolean = false,
) {
val showDoneButton
val enableDoneButton
get() = customField.key.isNotEmpty() && hasChanges && keyErrorMessage == null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,47 +119,47 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() {
}

@Test
fun `given editing an existing field, when key is changed, then show done button`() = testBlocking {
fun `given editing an existing field, when key is changed, then enable done button`() = testBlocking {
setup(editing = true)

val state = viewModel.state.runAndCaptureValues {
viewModel.onKeyChanged("new key")
}.last()

assertThat(state.showDoneButton).isTrue()
assertThat(state.enableDoneButton).isTrue()
}

@Test
fun `given editing an existing field, when value is changed, then show done button`() = testBlocking {
fun `given editing an existing field, when value is changed, then enable done button`() = testBlocking {
setup(editing = true)

val state = viewModel.state.runAndCaptureValues {
viewModel.onValueChanged("new value")
}.last()

assertThat(state.showDoneButton).isTrue()
assertThat(state.enableDoneButton).isTrue()
}

@Test
fun `given creating a new field, when the key is not empty, then show done button`() = testBlocking {
fun `given creating a new field, when the key is not empty, then enable done button`() = testBlocking {
setup(editing = false)

val state = viewModel.state.runAndCaptureValues {
viewModel.onKeyChanged("key")
}.last()

assertThat(state.showDoneButton).isTrue()
assertThat(state.enableDoneButton).isTrue()
}

@Test
fun `when key is empty, then hide done button`() = testBlocking {
fun `when key is empty, then disable done button`() = testBlocking {
setup(editing = false)

val state = viewModel.state.runAndCaptureValues {
viewModel.onKeyChanged("")
}.last()

assertThat(state.showDoneButton).isFalse()
assertThat(state.enableDoneButton).isFalse()
}

@Test
Expand Down Expand Up @@ -249,7 +249,7 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() {

assertThat(state.keyErrorMessage)
.isEqualTo(UiString.UiStringRes(R.string.custom_fields_editor_key_error_duplicate))
assertThat(state.showDoneButton).isFalse()
assertThat(state.enableDoneButton).isFalse()
}

@Test
Expand Down Expand Up @@ -291,7 +291,7 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() {

assertThat(state.keyErrorMessage)
.isEqualTo(UiString.UiStringRes(R.string.custom_fields_editor_key_error_underscore))
assertThat(state.showDoneButton).isFalse()
assertThat(state.enableDoneButton).isFalse()
}

@Test
Expand Down

0 comments on commit ec00c7f

Please sign in to comment.