Skip to content

Commit

Permalink
Added more tests to NavigationContextStacksTest
Browse files Browse the repository at this point in the history
  • Loading branch information
chRyNaN committed May 22, 2023
1 parent 89c27f1 commit 58e062f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ internal class NavigationContextStacks<Destination : NavigationDestination, Cont
operator fun get(context: Context): Stack<Destination> =
destinationStacksByContext[context]?.toStack() ?: stackOf(context.initialDestination)

/**
* Sets the [Stack] for the provided [Context]. Note that this does not perform a check to make sure that the
* initial destination is correct.
*/
operator fun set(context: Context, destinations: Stack<Destination>) {
destinationStacksByContext[context] = destinations.toMutableStack()
}

/**
* Retrieves the current [Destination] on top of the [Stack] for the provided [Context] without removing it.
*/
Expand Down Expand Up @@ -85,15 +77,17 @@ internal class NavigationContextStacks<Destination : NavigationDestination, Cont

if (index == -1) {
stack.push(destination)

destinationStacksByContext[context] = stack
} else {
val dropCount = (stack.size - (stack.size - index)) + 1

stack.drop(dropCount)
val newStack = stack.toList().drop(dropCount).toMutableStack()

stack.push(destination)
}
newStack.push(destination)

destinationStacksByContext[context] = stack
destinationStacksByContext[context] = newStack
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,35 @@ internal class NavigationContextStacksTest {
}

@Test
fun clearResetsStacksToInitialState() {
fun clearResetsStackToInitialState() {
val stacks = NavigationContextStacks<TestDestination, TestContext>(initialContext = TestContext.Home)

stacks.push(context = TestContext.Home, destination = TestDestination.ITEM_DETAILS)
stacks.push(context = TestContext.Home, destination = TestDestination.CHANNEL_DETAILS)

stacks.push(context = TestContext.Favorites, destination = TestDestination.SETTINGS)

val homeStack = stacks[TestContext.Home]
val favoriteStack = stacks[TestContext.Favorites]

assertEquals(expected = 3, actual = homeStack.size)
assertEquals(expected = TestDestination.CHANNEL_DETAILS, actual = homeStack.peek())
assertEquals(expected = 2, actual = favoriteStack.size)
assertEquals(expected = TestDestination.SETTINGS, actual = favoriteStack.peek())

stacks.clear(context = TestContext.Home)

val updatedHomeStack = stacks[TestContext.Home]
val updatedFavoriteStack = stacks[TestContext.Favorites]

assertEquals(expected = 1, actual = updatedHomeStack.size)
assertEquals(expected = TestDestination.HOME, actual = updatedHomeStack.peek())
assertEquals(expected = 2, actual = updatedFavoriteStack.size)
assertEquals(expected = TestDestination.SETTINGS, actual = updatedFavoriteStack.peek())
}

@Test
fun clearAllResetsStacksToInitialState() {
val stacks = NavigationContextStacks<TestDestination, TestContext>(initialContext = TestContext.Home)

stacks.push(context = TestContext.Home, destination = TestDestination.ITEM_DETAILS)
Expand All @@ -152,4 +180,84 @@ internal class NavigationContextStacksTest {
assertEquals(expected = 1, actual = favoritesStack.size)
assertEquals(expected = TestDestination.FAVORITES, actual = favoritesStack.peek())
}

@Test
fun pushDroppingPopsTheStackToTheItem() {
val stacks = NavigationContextStacks<TestDestination, TestContext>(initialContext = TestContext.Home)

stacks.push(context = TestContext.Home, destination = TestDestination.ITEM_DETAILS)
stacks.push(context = TestContext.Home, destination = TestDestination.FAVORITES)
stacks.push(context = TestContext.Home, destination = TestDestination.SETTINGS)
stacks.push(context = TestContext.Home, destination = TestDestination.CHANNEL_DETAILS)

val initialStack = stacks[TestContext.Home]

assertEquals(expected = 5, actual = initialStack.size)
assertEquals(expected = TestDestination.CHANNEL_DETAILS, actual = initialStack.peek())

stacks.pushDropping(context = TestContext.Home, TestDestination.FAVORITES)

val updatedStack = stacks[TestContext.Home]

assertEquals(expected = true, actual = TestDestination.FAVORITES == TestDestination.FAVORITES)
assertEquals(expected = 3, actual = updatedStack.size)
assertEquals(expected = TestDestination.FAVORITES, actual = updatedStack.peek())
}

@Test
fun popToPreviousDestinationForContextRemovesItemOnTopOfStackAndReturnsTheNewTopOfStack() {
val stacks = NavigationContextStacks<TestDestination, TestContext>(initialContext = TestContext.Home)

stacks.push(context = TestContext.Home, destination = TestDestination.SETTINGS)

assertEquals(expected = 2, actual = stacks[TestContext.Home].size)
assertEquals(expected = TestDestination.SETTINGS, actual = stacks[TestContext.Home].peek())

val previous = stacks.popToPreviousDestinationForContext(context = TestContext.Home)

assertEquals(expected = 1, actual = stacks[TestContext.Home].size)
assertEquals(expected = TestDestination.HOME, actual = stacks[TestContext.Home].peek())
assertEquals(expected = TestDestination.HOME, actual = previous)
}

@Test
fun popToPreviousDestinationForContextReturnsNullForStackThatCannotBePopped() {
val stacks = NavigationContextStacks<TestDestination, TestContext>(initialContext = TestContext.Home)

assertEquals(expected = 1, actual = stacks[TestContext.Home].size)
assertEquals(expected = TestDestination.HOME, actual = stacks[TestContext.Home].peek())

val previous = stacks.popToPreviousDestinationForContext(context = TestContext.Home)

assertEquals(expected = 1, actual = stacks[TestContext.Home].size)
assertEquals(expected = TestDestination.HOME, actual = stacks[TestContext.Home].peek())
assertEquals(expected = null, actual = previous)
}

@Test
fun toMapReturnsExpectedMap() {
val stacks = NavigationContextStacks<TestDestination, TestContext>(initialContext = TestContext.Home)

stacks.push(context = TestContext.Home, destination = TestDestination.SETTINGS)
stacks.push(context = TestContext.Home, destination = TestDestination.CHANNEL_DETAILS)

stacks.push(context = TestContext.Favorites, destination = TestDestination.ITEM_DETAILS)

assertEquals(expected = 3, actual = stacks[TestContext.Home].size)
assertEquals(expected = 2, actual = stacks[TestContext.Favorites].size)
assertEquals(expected = TestDestination.CHANNEL_DETAILS, actual = stacks[TestContext.Home].peek())
assertEquals(expected = TestDestination.ITEM_DETAILS, actual = stacks[TestContext.Favorites].peek())

val map = stacks.toMap()

assertEquals(expected = 2, actual = map.size)

val homeStack = map[TestContext.Home]
val favoriteStack = map[TestContext.Favorites]

assertEquals(expected = 3, actual = homeStack?.size)
assertEquals(expected = 2, actual = favoriteStack?.size)
assertEquals(expected = TestDestination.CHANNEL_DETAILS, actual = homeStack?.peek())
assertEquals(expected = TestDestination.ITEM_DETAILS, actual = favoriteStack?.peek())
}
}

0 comments on commit 58e062f

Please sign in to comment.