package expo.modules.clerk import org.junit.Assert.assertEquals import org.junit.Test class ClerkUserProfileCustomPageStateTest { @Test fun coveredPageRemainsRetainedUntilItIsActuallyPopped() { val events = mutableListOf() val pushedPaths = mutableListOf() val state = state(events, push = pushedPaths::add) state.pageDidPresent("billing") state.navigate("push", "invoice-details") state.pageDidDismiss("billing") state.pageDidPresent("invoice-details") assertEquals(listOf("billing", "invoice-details"), state.retainedPathsForTesting()) assertEquals(listOf("invoice-details"), pushedPaths) assertEquals( listOf("presented:billing", "dismissed:billing", "presented:invoice-details"), events, ) state.pageDidDismiss("invoice-details") state.pageDidPresent("billing") assertEquals(listOf("billing"), state.retainedPathsForTesting()) } @Test fun closingUserButtonProfileDismissesCoveredPages() { val events = mutableListOf() val pendingResets = mutableListOf<() -> Unit>() val state = state( events, resetCoveredPathsWhenInactive = true, postInactiveReset = pendingResets::add, ) state.pageDidPresent("billing") state.navigate("push", "invoice-details") state.pageDidDismiss("billing") state.pageDidPresent("invoice-details") events.clear() state.pageDidDismiss("invoice-details") pendingResets.single().invoke() assertEquals(emptyList(), state.retainedPathsForTesting()) assertEquals(listOf("dismissed:invoice-details", "dismissed:billing"), events) } @Test fun returningToCoveredUserButtonPageCancelsInactiveReset() { val events = mutableListOf() val pendingResets = mutableListOf<() -> Unit>() val state = state( events, resetCoveredPathsWhenInactive = true, postInactiveReset = pendingResets::add, ) state.pageDidPresent("billing") state.navigate("push", "invoice-details") state.pageDidDismiss("billing") state.pageDidPresent("invoice-details") events.clear() state.pageDidDismiss("invoice-details") state.pageDidPresent("billing") pendingResets.single().invoke() assertEquals(listOf("billing"), state.retainedPathsForTesting()) assertEquals(listOf("dismissed:invoice-details", "presented:billing"), events) } @Test fun pushingAPathAlreadyInTheStackDoesNotPushOrCollapseIt() { val events = mutableListOf() val pushedPaths = mutableListOf() val state = state(events, push = pushedPaths::add) state.pageDidPresent("billing") state.navigate("push", "invoice-details") state.pageDidDismiss("billing") state.pageDidPresent("invoice-details") events.clear() state.navigate("push", "billing") assertEquals(listOf("billing", "invoice-details"), state.retainedPathsForTesting()) assertEquals(listOf("invoice-details"), pushedPaths) assertEquals(emptyList(), events) } @Test fun popToRootDismissesEveryRetainedPageDeepestFirst() { val events = mutableListOf() var popToRootCount = 0 val state = state(events, popToRoot = { popToRootCount += 1 }) state.pageDidPresent("billing") state.navigate("push", "invoice-details") state.pageDidDismiss("billing") state.pageDidPresent("invoice-details") events.clear() state.navigate("popToRoot", null) assertEquals(emptyList(), state.retainedPathsForTesting()) assertEquals(1, popToRootCount) assertEquals(listOf("dismissed:invoice-details", "dismissed:billing"), events) } @Test fun removingAnEarlierRouteInvalidatesTheWholeStack() { val events = mutableListOf() var popToRootCount = 0 val state = state(events, popToRoot = { popToRootCount += 1 }) state.pageDidPresent("billing") state.navigate("push", "invoice-details") state.pageDidDismiss("billing") state.pageDidPresent("invoice-details") events.clear() state.reconcileCustomPagePaths(setOf("invoice-details")) assertEquals(emptyList(), state.retainedPathsForTesting()) assertEquals(1, popToRootCount) assertEquals(listOf("dismissed:invoice-details", "dismissed:billing"), events) } @Test fun changingUsersInvalidatesRetainedNavigationOnce() { val events = mutableListOf() var popToRootCount = 0 val state = state(events, popToRoot = { popToRootCount += 1 }) state.userDidChange("user_1") state.pageDidPresent("billing") events.clear() state.userDidChange(null) state.userDidChange(null) assertEquals(emptyList(), state.retainedPathsForTesting()) assertEquals(1, popToRootCount) assertEquals(listOf("dismissed:billing"), events) } @Test fun aUserArrivingAfterMountKeepsThePresentedPage() { val events = mutableListOf() var popToRootCount = 0 val state = state(events, popToRoot = { popToRootCount += 1 }) state.userDidChange(null) state.pageDidPresent("billing") events.clear() state.userDidChange("user_1") assertEquals(listOf("billing"), state.retainedPathsForTesting()) assertEquals(0, popToRootCount) assertEquals(emptyList(), events) } @Test fun switchingToAnotherUserInvalidatesRetainedNavigation() { val events = mutableListOf() var popToRootCount = 0 val state = state(events, popToRoot = { popToRootCount += 1 }) state.userDidChange("user_1") state.pageDidPresent("billing") events.clear() state.userDidChange("user_2") assertEquals(emptyList(), state.retainedPathsForTesting()) assertEquals(1, popToRootCount) assertEquals(listOf("dismissed:billing"), events) } private fun state( events: MutableList, popToRoot: () -> Unit = {}, push: (String) -> Unit = {}, resetCoveredPathsWhenInactive: Boolean = false, postInactiveReset: ((() -> Unit) -> Unit) = { it() }, ): ClerkUserProfileCustomPageState { return ClerkUserProfileCustomPageState( resetCoveredPathsWhenInactive = resetCoveredPathsWhenInactive, postInactiveReset = postInactiveReset, ) { type, path -> events.add("$type:$path") }.also { it.configureNavigation(navigateBack = {}, popToRoot = popToRoot, push = push) } } }