Skip to content

Commit

Permalink
Updated and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Mar 12, 2024
1 parent 8d23e02 commit ba167ea
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CustomListActionUseCaseTest {
}

@Test
fun `give action create when successful should return created result`() = runTest {
fun `create action should return success when ok`() = runTest {
// Arrange
val name = "test"
val locationCode = "AB"
Expand Down Expand Up @@ -81,7 +81,7 @@ class CustomListActionUseCaseTest {
}

@Test
fun `give action create when list name already exits should return error`() = runTest {
fun `create action should return error when name already exists`() = runTest {
// Arrange
val name = "test"
val locationCode = "AB"
Expand All @@ -101,7 +101,7 @@ class CustomListActionUseCaseTest {
}

@Test
fun `give action rename when successful should return rename result`() = runTest {
fun `rename action should return success when ok`() = runTest {
// Arrange
val name = "test"
val newName = "test2"
Expand All @@ -121,7 +121,7 @@ class CustomListActionUseCaseTest {
}

@Test
fun `give action rename when list name already exists should return error`() = runTest {
fun `rename action should return error when name already exists`() = runTest {
// Arrange
val name = "test"
val newName = "test2"
Expand All @@ -144,7 +144,7 @@ class CustomListActionUseCaseTest {
}

@Test
fun `give action delete when successful should return delete result`() = runTest {
fun `delete action should return successful with deleted list`() = runTest {
// Arrange
val mockCustomList: CustomList = mockk()
val mockLocation: GeographicLocationConstraint.Country = mockk()
Expand Down Expand Up @@ -173,46 +173,45 @@ class CustomListActionUseCaseTest {
}

@Test
fun `give action update locations when successful should return locations changed result`() =
runTest {
// Arrange
val name = "test"
val oldLocationCodes = listOf("AB", "CD")
val newLocationCodes = listOf("EF", "GH")
val oldLocations: ArrayList<GeographicLocationConstraint> =
arrayListOf(
GeographicLocationConstraint.Country("AB"),
GeographicLocationConstraint.Country("CD")
)
val customListId = "1"
val customList = CustomList(id = customListId, name = name, locations = oldLocations)
val action =
CustomListAction.UpdateLocations(
customListId = customListId,
locations = newLocationCodes
)
val expectedResult =
Result.success(
CustomListResult.LocationsChanged(
name = name,
undo = action.not(locations = oldLocationCodes)
)
fun `update locations action should return success with changed locations`() = runTest {
// Arrange
val name = "test"
val oldLocationCodes = listOf("AB", "CD")
val newLocationCodes = listOf("EF", "GH")
val oldLocations: ArrayList<GeographicLocationConstraint> =
arrayListOf(
GeographicLocationConstraint.Country("AB"),
GeographicLocationConstraint.Country("CD")
)
val customListId = "1"
val customList = CustomList(id = customListId, name = name, locations = oldLocations)
val action =
CustomListAction.UpdateLocations(
customListId = customListId,
locations = newLocationCodes
)
val expectedResult =
Result.success(
CustomListResult.LocationsChanged(
name = name,
undo = action.not(locations = oldLocationCodes)
)
coEvery { mockCustomListsRepository.getCustomListById(customListId) } returns customList
)
coEvery { mockCustomListsRepository.getCustomListById(customListId) } returns customList

coEvery {
mockCustomListsRepository.updateCustomListLocationsFromCodes(
customListId,
newLocationCodes
)
} returns UpdateCustomListResult.Ok
coEvery {
mockCustomListsRepository.updateCustomListLocationsFromCodes(
customListId,
newLocationCodes
)
} returns UpdateCustomListResult.Ok

// Act
val result = customListActionUseCase.performAction(action)
// Act
val result = customListActionUseCase.performAction(action)

// Assert
assertEquals(expectedResult, result)
}
// Assert
assertEquals(expectedResult, result)
}

companion object {
private const val RELAY_LIST_EXTENSIONS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import net.mullvad.mullvadvpn.compose.state.CustomListLocationsUiState
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.model.GeographicLocationConstraint
import net.mullvad.mullvadvpn.relaylist.RelayItem
import net.mullvad.mullvadvpn.relaylist.allChildren
import net.mullvad.mullvadvpn.relaylist.descendants
import net.mullvad.mullvadvpn.usecase.RelayListUseCase
import net.mullvad.mullvadvpn.usecase.customlists.CustomListActionUseCase
import org.junit.jupiter.api.Assertions.assertEquals
Expand Down Expand Up @@ -83,7 +83,7 @@ class CustomListLocationsViewModelTest {
}
customListFlow.value = listOf(customList)
val expectedSelection =
(DUMMY_COUNTRIES + DUMMY_COUNTRIES.flatMap { it.allChildren() }).toSet()
(DUMMY_COUNTRIES + DUMMY_COUNTRIES.flatMap { it.descendants() }).toSet()
val viewModel = createViewModel(customListId, true)
relayListFlow.value = expectedList

Expand All @@ -106,7 +106,7 @@ class CustomListLocationsViewModelTest {
// Arrange
val expectedList = DUMMY_COUNTRIES
val initialSelection =
(DUMMY_COUNTRIES + DUMMY_COUNTRIES.flatMap { it.allChildren() }).toSet()
(DUMMY_COUNTRIES + DUMMY_COUNTRIES.flatMap { it.descendants() }).toSet()
val customListId = "id"
val customListName = "name"
val customList: RelayItem.CustomList = mockk {
Expand Down Expand Up @@ -138,7 +138,7 @@ class CustomListLocationsViewModelTest {
// Arrange
val expectedList = DUMMY_COUNTRIES
val initialSelection =
(DUMMY_COUNTRIES + DUMMY_COUNTRIES.flatMap { it.allChildren() }).toSet()
(DUMMY_COUNTRIES + DUMMY_COUNTRIES.flatMap { it.descendants() }).toSet()
val customListId = "id"
val customListName = "name"
val customList: RelayItem.CustomList = mockk {
Expand Down

0 comments on commit ba167ea

Please sign in to comment.