Skip to content

Commit

Permalink
prefer stored primary & parallel languages when opening a favorite tool
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Nov 26, 2024
1 parent a885e0c commit c90c688
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ class AllFavoritesPresenter @AssistedInject constructor(
ToolCard.Event.OpenTool -> {
val intent = tool.createToolIntent(
context = context,
languages = listOfNotNull(state.translation?.languageCode),
languages = listOfNotNull(
tool.primaryLocale ?: state.translation?.languageCode,
tool.parallelLocale
),
)

if (intent != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ class HomePresenter @AssistedInject constructor(
ToolCard.Event.OpenTool -> {
val intent = tool.createToolIntent(
context = context,
languages = listOfNotNull(state.translation?.languageCode),
languages = listOfNotNull(
tool.primaryLocale ?: state.translation?.languageCode,
tool.parallelLocale
),
)

if (intent != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ class AllFavoritesPresenterTest {
coVerify { toolsRepository.storeToolOrder(listOf("tool1", "tool3", "tool2", "tool4")) }
}

// region ToolCard.Event.Click
@Test
fun `ToolCard - Event - Click`() = runTest {
val tool = randomTool("tool", Tool.Type.TRACT)
val tool = randomTool("tool", Tool.Type.TRACT, primaryLocale = null, parallelLocale = null)
toolsFlow.value = listOf(tool)
everyComposable { toolCardPresenter.present(tool = tool, eventSink = any()) }.answers {
ToolCard.State(
Expand All @@ -165,6 +166,31 @@ class AllFavoritesPresenterTest {
verifyAll { eventBus.post(OpenAnalyticsActionEvent(ACTION_OPEN_TOOL, tool.code, SOURCE_FAVORITE)) }
}

@Test
fun `ToolCard - Event - Click - Saved Languages`() = runTest {
val tool = randomTool("tool", Tool.Type.TRACT, primaryLocale = Locale.FRENCH, parallelLocale = Locale.GERMAN)
toolsFlow.value = listOf(tool)
everyComposable { toolCardPresenter.present(tool = tool, eventSink = any()) }.answers {
ToolCard.State(
toolCode = firstArg<Tool>().code,
translation = randomTranslation(languageCode = Locale.ENGLISH),
eventSink = arg(4)
)
}

presenter.test {
expectMostRecentItem().tools[0].eventSink(ToolCard.Event.Click)

assertTrue {
assertIs<IntentScreen>(navigator.awaitNextScreen()).intent
.equalsIntent(tool.createToolIntent(context, languages = listOf(Locale.FRENCH, Locale.GERMAN)))
}
}

verifyAll { eventBus.post(OpenAnalyticsActionEvent(ACTION_OPEN_TOOL, tool.code, SOURCE_FAVORITE)) }
}
// endregion ToolCard.Event.Click

@Test
fun `ToolCard - Event - OpenToolDetails`() = runTest {
val tool = randomTool("tool", Tool.Type.TRACT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down Expand Up @@ -60,7 +61,11 @@ class HomePresenterTest {
}
private val toolCardPresenter: ToolCardPresenter = mockk {
everyComposable { present(tool = any(), eventSink = any()) }.answers {
ToolCard.State(toolCode = firstArg<Tool>().code, eventSink = arg(4))
ToolCard.State(
toolCode = firstArg<Tool>().code,
translation = randomTranslation(languageCode = Locale.ENGLISH),
eventSink = arg(4)
)
}
}

Expand Down Expand Up @@ -178,14 +183,6 @@ class HomePresenterTest {
// endregion State.spotlightLessons

// region State.favoriteTools
private val favoriteTool = randomTool(type = Tool.Type.TRACT, isHidden = false)
private val favoriteToolTranslation = randomTranslation(favoriteTool.code, languageCode = Locale.FRENCH)
init {
everyComposable { toolCardPresenter.present(tool = favoriteTool, eventSink = any()) }.answers {
ToolCard.State(toolCode = favoriteTool.code, translation = favoriteToolTranslation, eventSink = arg(4))
}
}

@Test
fun `State - favoriteTools`() = runTest {
val tools = List(3) { randomTool(type = Tool.Type.TRACT, isHidden = false) }
Expand All @@ -211,37 +208,64 @@ class HomePresenterTest {

@Test
fun `State - favoriteTools - Event - Click`() = runTest {
presenter.test {
toolsFlow.emit(listOf(favoriteTool))
expectMostRecentItem().favoriteTools[0].eventSink(ToolCard.Event.Click)
val tool = randomTool(type = Tool.Type.TRACT, primaryLocale = null, parallelLocale = null)

assertIs<IntentScreen>(navigator.awaitNextScreen()).let {
val expected = favoriteTool.createToolIntent(context, listOf(favoriteToolTranslation.languageCode))
assertTrue(expected equalsIntent it.intent)
presenter.test {
toolsFlow.emit(listOf(tool))
assertNotNull(expectMostRecentItem().favoriteTools[0]) { toolState ->
toolState.eventSink(ToolCard.Event.Click)

assertIs<IntentScreen>(navigator.awaitNextScreen()).let { screen ->
val expected = tool.createToolIntent(context, listOf(toolState.translation!!.languageCode))
assertTrue(expected equalsIntent screen.intent)
}
}
}
}

@Test
fun `State - favoriteTools - Event - OpenTool`() = runTest {
val tool = randomTool(type = Tool.Type.TRACT, primaryLocale = null, parallelLocale = null)

presenter.test {
toolsFlow.emit(listOf(favoriteTool))
expectMostRecentItem().favoriteTools[0].eventSink(ToolCard.Event.OpenTool)
toolsFlow.emit(listOf(tool))
assertNotNull(expectMostRecentItem().favoriteTools[0]) { toolState ->
toolState.eventSink(ToolCard.Event.OpenTool)

assertIs<IntentScreen>(navigator.awaitNextScreen()).let { screen ->
val expected = tool.createToolIntent(context, listOf(toolState.translation!!.languageCode))
assertTrue(expected equalsIntent screen.intent)
}
}
}
}

assertIs<IntentScreen>(navigator.awaitNextScreen()).let {
val expected = favoriteTool.createToolIntent(context, listOf(favoriteToolTranslation.languageCode))
assertTrue(expected equalsIntent it.intent)
@Test
fun `State - favoriteTools - Event - OpenTool - Saved Languages`() = runTest {
val tool = randomTool(type = Tool.Type.TRACT, primaryLocale = Locale.GERMAN, parallelLocale = Locale.FRENCH)

presenter.test {
toolsFlow.emit(listOf(tool))
assertNotNull(expectMostRecentItem().favoriteTools[0]) { toolState ->
toolState.eventSink(ToolCard.Event.OpenTool)

assertIs<IntentScreen>(navigator.awaitNextScreen()).let { screen ->
val expected = tool.createToolIntent(context, listOf(Locale.GERMAN, Locale.FRENCH))
assertTrue(expected equalsIntent screen.intent)
}
}
}
}

@Test
fun `State - favoriteTools - Event - OpenToolDetails`() = runTest {
val tool = randomTool(type = Tool.Type.TRACT)

presenter.test {
toolsFlow.emit(listOf(favoriteTool))
toolsFlow.emit(listOf(tool))
expectMostRecentItem().favoriteTools[0].eventSink(ToolCard.Event.OpenToolDetails)

assertEquals(ToolDetailsScreen(favoriteTool.code!!), navigator.awaitNextScreen())
assertEquals(ToolDetailsScreen(tool.code!!), navigator.awaitNextScreen())
}
}
// endregion State.favoriteTools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ abstract class ToolsRepositoryIT {
// region updateToolLocales()
@Test
fun `updateToolLocales()`() = testScope.runTest {
val tool = randomTool("tool")
val tool = randomTool("tool", primaryLocale = null, parallelLocale = null)
repository.storeInitialTools(listOf(tool))

assertNotNull(repository.findTool("tool")) {
Expand Down
4 changes: 4 additions & 0 deletions library/model/src/main/kotlin/org/cru/godtools/model/Tool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ fun randomTool(
isSpotlight: Boolean = Random.nextBoolean(),
shares: Int = Random.nextInt(),
pendingShares: Int = Random.nextInt(),
primaryLocale: Locale? = Locale.GERMAN.takeIf { Random.nextBoolean() },
parallelLocale: Locale? = Locale.FRENCH.takeIf { Random.nextBoolean() },
metatoolCode: String? = UUID.randomUUID().toString().takeIf { Random.nextBoolean() },
defaultVariantCode: String? = UUID.randomUUID().toString().takeIf { Random.nextBoolean() },
apiId: Long? = Random.nextLong().takeIf { Random.nextBoolean() },
Expand All @@ -291,6 +293,8 @@ fun randomTool(
isScreenShareDisabled = Random.nextBoolean(),
shares = shares,
pendingShares = pendingShares,
primaryLocale = primaryLocale,
parallelLocale = parallelLocale,
metatoolCode = metatoolCode,
defaultVariantCode = defaultVariantCode,
apiId = apiId,
Expand Down

0 comments on commit c90c688

Please sign in to comment.