Skip to content

Commit

Permalink
add unit tests for pining/unpining tools in tool details
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Mar 1, 2024
1 parent ce33bf8 commit c6a2827
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import org.cru.godtools.model.Language.Companion.getSortedDisplayNames
import org.cru.godtools.model.Tool
import org.cru.godtools.model.Translation
import org.cru.godtools.shortcuts.GodToolsShortcutManager
import org.cru.godtools.sync.GodToolsSyncService
import org.cru.godtools.ui.tooldetails.ToolDetailsScreen.Event
import org.cru.godtools.ui.tooldetails.ToolDetailsScreen.State
import org.cru.godtools.ui.tools.ToolCard
Expand All @@ -70,6 +71,7 @@ class ToolDetailsPresenter @AssistedInject constructor(
private val manifestManager: ManifestManager,
private val settings: Settings,
private val shortcutManager: GodToolsShortcutManager,
private val syncService: GodToolsSyncService,
private val toolCardPresenter: ToolCardPresenter,
@Assisted private val screen: ToolDetailsScreen,
@Assisted private val navigator: Navigator,
Expand Down Expand Up @@ -111,17 +113,19 @@ class ToolDetailsPresenter @AssistedInject constructor(
// TODO: record analytics event when launching a tool
if (intent != null) navigator.goTo(IntentScreen(intent))
}
is Event.SwitchVariant -> toolCode = it.variant
Event.PinTool -> {
coroutineScope.launch { toolsRepository.pinTool(toolCode) }
// TODO: trigger favorite tools sync
// TODO: mark favorite tools feature discovered

Event.PinTool -> coroutineScope.launch {
settings.setFeatureDiscovered(Settings.FEATURE_TOOL_FAVORITE)
toolsRepository.pinTool(toolCode)
syncService.syncDirtyFavoriteTools()
}
Event.UnpinTool -> {
coroutineScope.launch { toolsRepository.unpinTool(toolCode) }
// TODO: trigger favorite tools sync
// TODO: mark favorite tools feature discovered

Event.UnpinTool -> coroutineScope.launch {
toolsRepository.unpinTool(toolCode)
syncService.syncDirtyFavoriteTools()
}

is Event.SwitchVariant -> toolCode = it.variant
Event.PinShortcut -> pendingShortcut?.let { shortcutManager.pinShortcut(it) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.slack.circuit.test.FakeNavigator
import com.slack.circuit.test.test
import io.mockk.Runs
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
Expand Down Expand Up @@ -40,6 +41,7 @@ import org.cru.godtools.model.randomTool
import org.cru.godtools.model.randomTranslation
import org.cru.godtools.shortcuts.GodToolsShortcutManager
import org.cru.godtools.shortcuts.PendingShortcut
import org.cru.godtools.sync.GodToolsSyncService
import org.cru.godtools.ui.tooldetails.ToolDetailsScreen.Event
import org.cru.godtools.ui.tools.FakeToolCardPresenter
import org.junit.runner.RunWith
Expand Down Expand Up @@ -85,6 +87,7 @@ class ToolDetailsPresenterTest {
private val shortcutManager: GodToolsShortcutManager = mockk {
every { canPinToolShortcut(any()) } returns false
}
private val syncService: GodToolsSyncService = mockk()

private fun createPresenter(screen: ToolDetailsScreen = ToolDetailsScreen(TOOL)) = ToolDetailsPresenter(
context = ApplicationProvider.getApplicationContext(),
Expand All @@ -97,6 +100,7 @@ class ToolDetailsPresenterTest {
manifestManager = manifestManager,
settings = settings,
shortcutManager = shortcutManager,
syncService = syncService,
toolCardPresenter = FakeToolCardPresenter(),
screen = screen,
navigator = navigator,
Expand Down Expand Up @@ -239,6 +243,43 @@ class ToolDetailsPresenterTest {
}
// endregion Event.PinShortcut

// region Event.PinTool
@Test
fun `Event - PinTool`() = runTest {
coEvery { toolsRepository.pinTool(any(), any()) } just Runs
every { settings.setFeatureDiscovered(any()) } just Runs
coEvery { syncService.syncDirtyFavoriteTools() } returns true

createPresenter().test {
expectMostRecentItem().eventSink(Event.PinTool)
}

coVerify {
toolsRepository.pinTool(TOOL)
settings.setFeatureDiscovered(Settings.FEATURE_TOOL_FAVORITE)
syncService.syncDirtyFavoriteTools()
}
}
// endregion Event.PinTool

// region Event.UnpinTool
@Test
fun `Event - UnpinTool`() = runTest {
coEvery { toolsRepository.unpinTool(any()) } just Runs
coEvery { syncService.syncDirtyFavoriteTools() } returns true

createPresenter().test {
expectMostRecentItem().eventSink(Event.UnpinTool)
}

coVerify {
toolsRepository.unpinTool(TOOL)
syncService.syncDirtyFavoriteTools()
}
coVerify(exactly = 0) { settings.setFeatureDiscovered(any()) }
}
// endregion Event.UnpinTool

// region Event.SwitchVariant
@Test
fun `Event - SwitchVariant`() = runTest {
Expand Down

0 comments on commit c6a2827

Please sign in to comment.