|
| 1 | +package com.flipcash.app.core.navigation |
| 2 | + |
| 3 | +import androidx.navigation3.runtime.NavKey |
| 4 | +import com.flipcash.app.core.AppRoute |
| 5 | +import kotlin.test.Test |
| 6 | +import kotlin.test.assertEquals |
| 7 | +import kotlin.test.assertSame |
| 8 | + |
| 9 | +/** |
| 10 | + * A tab press used to clear the backstack, which took each tab's ViewModels and saved state with it. |
| 11 | + * These assert the shape of the stack it leaves instead — the retained entries are the whole point, |
| 12 | + * since an entry that stays on the stack is an entry Nav3 does not tear down. |
| 13 | + */ |
| 14 | +class TabSwitchTest { |
| 15 | + |
| 16 | + private val scanner = AppRoute.Main.Scanner |
| 17 | + private val wallet = AppRoute.Sheets.Wallet |
| 18 | + private val menu = AppRoute.Sheets.Menu |
| 19 | + |
| 20 | + @Test |
| 21 | + fun `the tab already visited stays on the stack, under the one being opened`() { |
| 22 | + val stack = listOf<NavKey>(wallet) |
| 23 | + |
| 24 | + assertEquals(listOf(wallet, scanner), stack.afterSwitchingTo(NavBarButton.Scanner)) |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + fun `switching back reuses the entry that is already there`() { |
| 29 | + val stack = listOf<NavKey>(wallet).afterSwitchingTo(NavBarButton.Scanner) |
| 30 | + |
| 31 | + val back = stack.afterSwitchingTo(NavBarButton.Wallet) |
| 32 | + |
| 33 | + assertEquals(listOf(scanner, wallet), back) |
| 34 | + // Same key instance, so it keeps the same contentKey and Nav3 hands the entry — its |
| 35 | + // ViewModelStore, its saveable state — straight back rather than building a new one. |
| 36 | + assertSame(wallet, back.last()) |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `a tab is never on the stack twice, however often it is pressed`() { |
| 41 | + var stack = listOf<NavKey>(wallet) |
| 42 | + repeat(3) { |
| 43 | + stack = stack.afterSwitchingTo(NavBarButton.Scanner).afterSwitchingTo(NavBarButton.Wallet) |
| 44 | + } |
| 45 | + |
| 46 | + assertEquals(listOf(scanner, wallet), stack) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `screens pushed on the outgoing tab are dropped, not carried across`() { |
| 51 | + val stack = listOf<NavKey>(wallet, AppRoute.Main.Sheet(AppRoute.Sheets.ActivityHistory)) |
| 52 | + |
| 53 | + // The wallet's home is kept — that is the entry being retained — but its open sheet is not, |
| 54 | + // or it would sit underneath the scanner and be what back returned to. |
| 55 | + assertEquals(listOf(wallet, scanner), stack.afterSwitchingTo(NavBarButton.Scanner)) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `pressing the tab you are already on pops back to its home`() { |
| 60 | + val stack = listOf<NavKey>(wallet, AppRoute.Main.Sheet(AppRoute.Sheets.ActivityHistory)) |
| 61 | + |
| 62 | + assertEquals(listOf(wallet), stack.afterSwitchingTo(NavBarButton.Wallet)) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `pressing the tab you are on with nothing over it changes nothing`() { |
| 67 | + val stack = listOf<NavKey>(scanner, wallet) |
| 68 | + |
| 69 | + assertEquals(stack, stack.afterSwitchingTo(NavBarButton.Wallet)) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `back walks the tabs in the order they were visited`() { |
| 74 | + var stack = listOf<NavKey>(wallet) |
| 75 | + stack = stack.afterSwitchingTo(NavBarButton.TipCard) |
| 76 | + stack = stack.afterSwitchingTo(NavBarButton.Scanner) |
| 77 | + |
| 78 | + // Back from the scanner returns to the You tab, then the wallet, then leaves the app — |
| 79 | + // the deliberate cost of keeping the entries alive. |
| 80 | + assertEquals(listOf(wallet, menu, scanner), stack) |
| 81 | + } |
| 82 | +} |
0 commit comments