Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ private fun <T> AvatarStack(
}
}

// A typist can change without changing key, such as when their profile resolves after they
// started typing. The effect above only runs when the keys change, so without this the avatar
// keeps drawing the typist as they first arrived.
LaunchedEffect(shown) {
shown.forEach { item ->
val itemKey = key(item)
entries.firstOrNull { it.key == itemKey && !it.leaving }?.item = item
}
}

// Stacking order: later slots on top, and a leaving avatar beneath everything, so the ones
// that close the gap slide over it.
fun StackEntry<T>.z(): Float = if (leaving) -1f else slot.value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.getcode.ui.components.chat

import androidx.compose.material.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.getcode.theme.DesignSystem
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class TypingIndicatorTest {

@get:Rule
val composeTestRule = createComposeRule()

private data class Typist(val id: String, val name: String?)

// A typist can appear before their profile resolves, and the resolved profile arrives as the
// same typist (same key) with more to draw. The avatar has to pick that up.
@Test
fun avatarRedrawsWhenATypistChangesUnderTheSameKey() {
var typists by mutableStateOf(listOf(Typist("a", name = null)))
composeTestRule.setContent {
DesignSystem {
TypingIndicator(
typists = typists,
key = { it.id },
avatar = { Text(it.name ?: "fallback") },
)
}
}
composeTestRule.onNodeWithText("fallback").assertExists()

typists = listOf(Typist("a", name = "Alice"))
composeTestRule.waitForIdle()

composeTestRule.onNodeWithText("Alice").assertExists()
composeTestRule.onNodeWithText("fallback").assertDoesNotExist()
}
}
Loading