Skip to content

Commit

Permalink
Add and fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Sep 11, 2024
1 parent 0c8557f commit 3385bfe
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() {
}

@Test
fun `when done is clicked, then exit with result`() = testBlocking {
fun `given editing an existing field, when done is clicked, then exit with result`() = testBlocking {
setup(editing = true)

val events = viewModel.event.runAndCaptureValues {
Expand All @@ -182,12 +182,35 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() {

assertThat(events).isEqualTo(
MultiLiveEvent.Event.ExitWithResult(
data = CustomFieldUiModel(id = CUSTOM_FIELD_ID, key = "new key", value = "new value"),
data = CustomFieldsEditorViewModel.CustomFieldUpdateResult(
CUSTOM_FIELD.key,
CustomFieldUiModel(id = CUSTOM_FIELD_ID, key = "new key", value = "new value")
),
key = CustomFieldsEditorViewModel.CUSTOM_FIELD_UPDATED_RESULT_KEY
)
)
}

@Test
fun `given creating a new field, when done is clicked, then exit with result`() = testBlocking {
setup(editing = false) {
whenever(repository.getDisplayableCustomFields(PARENT_ITEM_ID)).thenReturn(emptyList())
}

val events = viewModel.event.runAndCaptureValues {
viewModel.onKeyChanged("key")
viewModel.onValueChanged("value")
viewModel.onDoneClicked()
}.last()

assertThat(events).isEqualTo(
MultiLiveEvent.Event.ExitWithResult(
data = CustomFieldUiModel(key = "key", value = "value"),
key = CustomFieldsEditorViewModel.CUSTOM_FIELD_CREATED_RESULT_KEY
)
)
}

@Test
fun `given adding a new field, when key is duplicate, then show error`() = testBlocking {
setup(editing = false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class CustomFieldsViewModelTest : BaseUnitTest() {
setup()

val state = viewModel.state.runAndCaptureValues {
viewModel.onCustomFieldUpdated(customField)
viewModel.onCustomFieldUpdated(CUSTOM_FIELDS.first().key, customField)
advanceUntilIdle()
}.last()

Expand All @@ -230,8 +230,8 @@ class CustomFieldsViewModelTest : BaseUnitTest() {
setup()

val state = viewModel.state.runAndCaptureValues {
viewModel.onCustomFieldUpdated(customField.copy(value = "new value"))
viewModel.onCustomFieldUpdated(customField.copy(value = "new value 2"))
viewModel.onCustomFieldUpdated(CUSTOM_FIELDS.first().key, customField.copy(value = "new value"))
viewModel.onCustomFieldUpdated(CUSTOM_FIELDS.first().key, customField.copy(value = "new value 2"))
advanceUntilIdle()
}.last()

Expand All @@ -256,6 +256,25 @@ class CustomFieldsViewModelTest : BaseUnitTest() {
assertThat(state.customFields.last().value).isEqualTo(customField.value)
}

@Test
fun `when adding a custom field then updating it, then confirm the field is not duplicated`() = testBlocking {
val customField = CustomFieldUiModel(
key = "new key",
value = "new value"
)
setup()

val state = viewModel.state.runAndCaptureValues {
viewModel.onCustomFieldInserted(customField)
viewModel.onCustomFieldUpdated(customField.key, customField.copy(value = "new value 2"))
advanceUntilIdle()
}.last()

assertThat(state.customFields).hasSize(CUSTOM_FIELDS.size + 1)
assertThat(state.customFields.last().key).isEqualTo(customField.key)
assertThat(state.customFields.last().value).isEqualTo("new value 2")
}

@Test
fun `when deleting a custom field, then custom fields are refreshed`() = testBlocking {
val customField = CustomFieldUiModel(CUSTOM_FIELDS.first())
Expand Down Expand Up @@ -325,7 +344,7 @@ class CustomFieldsViewModelTest : BaseUnitTest() {

setup()

viewModel.onCustomFieldUpdated(updatedField)
viewModel.onCustomFieldUpdated(CUSTOM_FIELDS.first().key, updatedField)
viewModel.onCustomFieldInserted(insertedField)
viewModel.onSaveClicked()

Expand Down

0 comments on commit 3385bfe

Please sign in to comment.