From 0b08ee1258cb0604c46c813b78009444beca8d27 Mon Sep 17 00:00:00 2001 From: Abdulrahman Ragab Date: Thu, 15 May 2025 14:12:27 +0300 Subject: [PATCH] add more test cases to increase the coverage for UpdateTaskStateUI --- .../task_state/UpdateTaskStateUI.kt | 4 +- .../task_state/UpdateTaskStateUITest.kt | 62 +++++++++++++++++-- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/presentation/task_state/UpdateTaskStateUI.kt b/src/main/kotlin/presentation/task_state/UpdateTaskStateUI.kt index 56bbf73..b5bd7ef 100644 --- a/src/main/kotlin/presentation/task_state/UpdateTaskStateUI.kt +++ b/src/main/kotlin/presentation/task_state/UpdateTaskStateUI.kt @@ -41,9 +41,9 @@ class UpdateTaskStateUI( newName, state.projectId ) - viewer.show(" $newName is updated Successfully") + viewer.show("$newName is updated Successfully") - } catch (ex: InvalidStateNameException) { + } catch (_: InvalidStateNameException) { viewer.show("State Name must not be empty or blank") } catch (_: InputCancelledException) { viewer.show("Cancelled!") diff --git a/src/test/kotlin/presentation/task_state/UpdateTaskStateUITest.kt b/src/test/kotlin/presentation/task_state/UpdateTaskStateUITest.kt index 3d79fb2..828e6db 100644 --- a/src/test/kotlin/presentation/task_state/UpdateTaskStateUITest.kt +++ b/src/test/kotlin/presentation/task_state/UpdateTaskStateUITest.kt @@ -1,5 +1,7 @@ package com.berlin.presentation.task_state +import com.berlin.domain.exception.InputCancelledException +import com.berlin.domain.exception.InvalidSelectionException import com.berlin.domain.exception.InvalidStateNameException import com.berlin.domain.model.TaskState import com.berlin.domain.usecase.task_state.GetAllTaskStatesUseCase @@ -26,13 +28,11 @@ class UpdateTaskStateUITest { updateTaskStateUI = UpdateTaskStateUI(updateTaskStateUseCase, getAllTaskStatesUseCase, viewer, reader) } - - @Test - fun `run should throw InvalidStateNameException when State Name is empty or blank`() { + fun `run should throw InvalidStateNameException when State Name is null, empty, or blank`() { //Given - every { getAllTaskStatesUseCase() } returns listOf(TaskState("Q1", "Menna", "P5")) - every { reader.read() } returnsMany listOf("1", " ") + every { getAllTaskStatesUseCase() } returns states + every { reader.read() } returnsMany listOf("1", null) every { updateTaskStateUseCase(any(), emptyStateName, any()) } throws InvalidStateNameException("State Name must not be empty or blank") @@ -44,10 +44,60 @@ class UpdateTaskStateUITest { verify { viewer.show("State Name must not be empty or blank") } } + @Test + fun `updateState should cancel the update when user enters x`() { + //Given + every { getAllTaskStatesUseCase() } returns states + every { reader.read() } returnsMany listOf("1", "x") + every { + updateTaskStateUseCase(any(), "x", any()) + } throws InputCancelledException("Cancelled!") + + //When + updateTaskStateUI.run() + + //Then + verify { viewer.show("Cancelled!") } + } + + + @Test + fun `updateState should trow invalid selection exception when the user choose invalid selection`() { + //Given + every { getAllTaskStatesUseCase() } returns states + every { reader.read() } returnsMany listOf("00", newValidStateName) + every { + updateTaskStateUseCase(any(), newValidStateName, any()) + } throws InvalidSelectionException("invalid selection") + + //When + updateTaskStateUI.run() + + //Then + verify { viewer.show("invalid selection") } + } + + @Test + fun `updateState should return success when the new state name is valid`() { + //Given + every { getAllTaskStatesUseCase() } returns states + every { reader.read() } returnsMany listOf("1", newValidStateName) + every { + updateTaskStateUseCase(any(), newValidStateName, any()) + } returns "$newValidStateName is updated Successfully" + + //When + updateTaskStateUI.run() + + //Then + verify { viewer.show("$newValidStateName is updated Successfully") } + } + + private companion object { private val state = TaskState("Q1", "Menna", "P5") private val states = listOf(state) - private const val successfullyStateNewName = "done" + private const val newValidStateName = "new state" private const val emptyStateName = "" }