Skip to content

Commit

Permalink
Add a test for DisposableEffect callbacks
Browse files Browse the repository at this point in the history
Initially I'm adding just the test we already pass, where
the effect is executed because it is removed from the
composition.

#2248
  • Loading branch information
squarejesse committed Dec 9, 2024
1 parent 8c03b03 commit 7bba4bc
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@
package app.cash.redwood.treehouse

import com.example.redwood.testapp.treehouse.HostApi
import kotlinx.coroutines.channels.Channel

class FakeHostApi : HostApi {
private val messagesChannel = Channel<String>(capacity = Int.MAX_VALUE)

override suspend fun httpCall(url: String, headers: Map<String, String>): String {
error("unexpected call")
}

override fun log(message: String) {
messagesChannel.trySend(message)
}

suspend fun takeMessage(): String {
return messagesChannel.receive()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2024 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.redwood.treehouse

import assertk.assertThat
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import com.example.redwood.testapp.testing.ButtonValue
import com.example.redwood.testapp.testing.TextInputValue
import kotlin.test.Test
import kotlinx.coroutines.test.runTest

class GuestLifecycleTest {
@Test
fun disposableEffectDisposedWhenRemovedFromComposition() = runTest {
val tester = TreehouseTester(this)
val treehouseApp = tester.loadApp()
val content = tester.content(treehouseApp)
val view = tester.view()

content.bind(view)

content.awaitContent(1)
val textInputValue = view.views.single() as TextInputValue
assertThat(textInputValue.text).isEqualTo("what would you like to see?")
textInputValue.onChange!!.invoke("GuestLifecycleTestShowDisposable")

tester.sendFrame()
assertThat(tester.hostApi.takeMessage()).isEqualTo("DisposableEffect.effect()")
content.awaitContent(2)
val nextButton = view.views.single() as ButtonValue
assertThat(nextButton.text).isEqualTo("Next")
nextButton.onClick!!.invoke()

tester.sendFrame()
assertThat(tester.hostApi.takeMessage()).isEqualTo("DisposableEffect.dispose()")
content.awaitContent(3)
assertThat(view.views).isEmpty()

treehouseApp.stop()
treehouseApp.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class TreehouseTester(
) {
val eventLog = EventLog()

var hostApi: HostApi = FakeHostApi()
var hostApi = FakeHostApi()

var eventListenerFactory: EventListener.Factory = FakeEventListener.Factory(eventLog)

Expand Down Expand Up @@ -98,7 +98,7 @@ internal class TreehouseTester(
override fun create(scope: CoroutineScope, dispatchers: TreehouseDispatchers) = frameClock
}

val treehouseAppFactory = RealTreehouseApp.Factory(
private val treehouseAppFactory = RealTreehouseApp.Factory(
platform = platform,
httpClient = httpClient,
frameClockFactory = frameClockFactory,
Expand Down Expand Up @@ -129,7 +129,7 @@ internal class TreehouseTester(
treehouseApp: TreehouseApp<TestAppPresenter>,
zipline: Zipline,
) {
zipline.bind("HostApi", hostApi)
zipline.bind<HostApi>("HostApi", hostApi)
}

override fun create(zipline: Zipline): TestAppPresenter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ import kotlin.native.ObjCName
interface HostApi : ZiplineService {
/** Decodes the response as a string and returns it. */
suspend fun httpCall(url: String, headers: Map<String, String>): String

/** Records a message for any potential consumer. */
fun log(message: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class RealTestAppPresenter(
}

override fun launchForTester(): ZiplineTreehouseUi {
return TesterTreehouseUi().asZiplineTreehouseUi(appLifecycle)
val treehouseUi = TesterTreehouseUi(hostApi)
return treehouseUi.asZiplineTreehouseUi(appLifecycle)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.example.redwood.testapp.treehouse

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -24,19 +25,25 @@ import app.cash.redwood.treehouse.TreehouseUi
import com.example.redwood.testapp.compose.Button
import com.example.redwood.testapp.compose.TextInput

class TesterTreehouseUi : TreehouseUi {
class TesterTreehouseUi(
private val hostApi: HostApi,
) : TreehouseUi {
@Composable
override fun Show() {
var content by remember { mutableStateOf(Content.InitialValue) }
content.Show { newContent ->
content = newContent
}
content.Show(
changeContent = { newContent -> content = newContent },
log = { message -> hostApi.log(message) },
)
}

enum class Content {
InitialValue {
@Composable
override fun Show(changeContent: (Content) -> Unit) {
override fun Show(
changeContent: (Content) -> Unit,
log: (String) -> Unit,
) {
TextInput(
text = "what would you like to see?",
customType = null,
Expand All @@ -47,9 +54,33 @@ class TesterTreehouseUi : TreehouseUi {
}
},

GuestLifecycleTestShowDisposable {
@Composable
override fun Show(
changeContent: (Content) -> Unit,
log: (String) -> Unit,
) {
DisposableEffect(log) {
log("DisposableEffect.effect()")
onDispose {
log("DisposableEffect.dispose()")
}
}
Button(
text = "Next",
onClick = {
changeContent(Empty)
},
)
}
},

TreehouseTesterTestHappyPathStep2 {
@Composable
override fun Show(changeContent: (Content) -> Unit) {
override fun Show(
changeContent: (Content) -> Unit,
log: (String) -> Unit,
) {
Button(
text = "This is TreehouseTesterTestHappyPathStep2",
onClick = {
Expand All @@ -60,13 +91,19 @@ class TesterTreehouseUi : TreehouseUi {

Empty {
@Composable
override fun Show(changeContent: (Content) -> Unit) {
override fun Show(
changeContent: (Content) -> Unit,
log: (String) -> Unit,
) {
}
},

;

@Composable
abstract fun Show(changeContent: (Content) -> Unit)
abstract fun Show(
changeContent: (Content) -> Unit,
log: (String) -> Unit,
)
}
}

0 comments on commit 7bba4bc

Please sign in to comment.