|
15 | 15 | */
|
16 | 16 | package com.squareup.workflow
|
17 | 17 |
|
| 18 | +import com.squareup.workflow.WorkflowAction.Companion.enterState |
18 | 19 | import com.squareup.workflow.testing.testFromStart
|
19 | 20 | import kotlin.test.Test
|
20 | 21 | import kotlin.test.assertEquals
|
21 | 22 | import kotlin.test.assertFailsWith
|
| 23 | +import kotlin.test.assertTrue |
22 | 24 |
|
23 | 25 | class CompositionIntegrationTest {
|
24 | 26 |
|
@@ -91,4 +93,88 @@ class CompositionIntegrationTest {
|
91 | 93 | }
|
92 | 94 | }
|
93 | 95 | }
|
| 96 | + |
| 97 | + @Test fun `all childrens teardown hooks invoked when parent discards it`() { |
| 98 | + val teardowns = mutableListOf<String>() |
| 99 | + val child1 = Workflow.stateless<Nothing, Unit> { context -> |
| 100 | + context.onTeardown { teardowns += "child1" } |
| 101 | + } |
| 102 | + val child2 = Workflow.stateless<Nothing, Unit> { context -> |
| 103 | + context.onTeardown { teardowns += "child2" } |
| 104 | + } |
| 105 | + // A workflow that will render child1 and child2 until its rendering is invoked, at which point |
| 106 | + // it will compose neither of them, which should trigger the teardown callbacks. |
| 107 | + val root = object : StatefulWorkflow<Unit, Boolean, Nothing, () -> Unit>() { |
| 108 | + override fun initialState( |
| 109 | + input: Unit, |
| 110 | + snapshot: Snapshot? |
| 111 | + ): Boolean = true |
| 112 | + |
| 113 | + override fun compose( |
| 114 | + input: Unit, |
| 115 | + state: Boolean, |
| 116 | + context: WorkflowContext<Boolean, Nothing> |
| 117 | + ): () -> Unit { |
| 118 | + if (state) { |
| 119 | + context.compose(child1, key = "child1") |
| 120 | + context.compose(child2, key = "child2") |
| 121 | + } |
| 122 | + return context.onEvent<Unit> { enterState(false) }::invoke |
| 123 | + } |
| 124 | + |
| 125 | + override fun snapshotState(state: Boolean): Snapshot = Snapshot.EMPTY |
| 126 | + } |
| 127 | + |
| 128 | + root.testFromStart { tester -> |
| 129 | + tester.withNextRendering { teardownChildren -> |
| 130 | + assertTrue(teardowns.isEmpty()) |
| 131 | + |
| 132 | + teardownChildren() |
| 133 | + |
| 134 | + assertEquals(listOf("child1", "child2"), teardowns) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Test fun `nested childrens teardown hooks invoked when parent discards it`() { |
| 140 | + val teardowns = mutableListOf<String>() |
| 141 | + val grandchild = Workflow.stateless<Nothing, Unit> { context -> |
| 142 | + context.onTeardown { teardowns += "grandchild" } |
| 143 | + } |
| 144 | + val child = Workflow.stateless<Nothing, Unit> { context -> |
| 145 | + context.compose(grandchild) |
| 146 | + context.onTeardown { teardowns += "child" } |
| 147 | + } |
| 148 | + // A workflow that will render child1 and child2 until its rendering is invoked, at which point |
| 149 | + // it will compose neither of them, which should trigger the teardown callbacks. |
| 150 | + val root = object : StatefulWorkflow<Unit, Boolean, Nothing, () -> Unit>() { |
| 151 | + override fun initialState( |
| 152 | + input: Unit, |
| 153 | + snapshot: Snapshot? |
| 154 | + ): Boolean = true |
| 155 | + |
| 156 | + override fun compose( |
| 157 | + input: Unit, |
| 158 | + state: Boolean, |
| 159 | + context: WorkflowContext<Boolean, Nothing> |
| 160 | + ): () -> Unit { |
| 161 | + if (state) { |
| 162 | + context.compose(child) |
| 163 | + } |
| 164 | + return context.onEvent<Unit> { enterState(false) }::invoke |
| 165 | + } |
| 166 | + |
| 167 | + override fun snapshotState(state: Boolean): Snapshot = Snapshot.EMPTY |
| 168 | + } |
| 169 | + |
| 170 | + root.testFromStart { tester -> |
| 171 | + tester.withNextRendering { teardownChildren -> |
| 172 | + assertTrue(teardowns.isEmpty()) |
| 173 | + |
| 174 | + teardownChildren() |
| 175 | + |
| 176 | + assertEquals(listOf("grandchild", "child"), teardowns) |
| 177 | + } |
| 178 | + } |
| 179 | + } |
94 | 180 | }
|
0 commit comments