|
| 1 | +package org.cru.godtools.ui.dashboard.lessons |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import android.content.Context |
| 5 | +import androidx.compose.ui.test.ExperimentalTestApi |
| 6 | +import androidx.compose.ui.test.onNodeWithText |
| 7 | +import androidx.compose.ui.test.performClick |
| 8 | +import androidx.compose.ui.test.v2.runComposeUiTest |
| 9 | +import androidx.test.core.app.ApplicationProvider |
| 10 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 11 | +import com.slack.circuit.test.TestEventSink |
| 12 | +import kotlin.test.Test |
| 13 | +import org.cru.godtools.R |
| 14 | +import org.cru.godtools.model.randomTranslation |
| 15 | +import org.cru.godtools.ui.dashboard.lessons.LessonsPresenter.UiEvent |
| 16 | +import org.cru.godtools.ui.dashboard.lessons.LessonsPresenter.UiState |
| 17 | +import org.cru.godtools.ui.tools.ToolCardPresenter |
| 18 | +import org.junit.runner.RunWith |
| 19 | +import org.robolectric.annotation.Config |
| 20 | + |
| 21 | +@RunWith(AndroidJUnit4::class) |
| 22 | +@Config(application = Application::class) |
| 23 | +@OptIn(ExperimentalTestApi::class) |
| 24 | +class LessonsLayoutTest { |
| 25 | + private val context: Context get() = ApplicationProvider.getApplicationContext() |
| 26 | + private val events = TestEventSink<UiEvent>() |
| 27 | + |
| 28 | + // region PersonalizationToggle |
| 29 | + @Test |
| 30 | + fun `PersonalizationToggle - not shown when personalization is disabled`() = runComposeUiTest { |
| 31 | + setContent { LessonsLayout(UiState(isPersonalizationEnabled = false, eventSink = events)) } |
| 32 | + |
| 33 | + onNodeWithText(context.getString(R.string.dashboard_lessons_toggle_personalized)).assertDoesNotExist() |
| 34 | + onNodeWithText(context.getString(R.string.dashboard_lessons_toggle_all)).assertDoesNotExist() |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun `PersonalizationToggle - shown when personalization is enabled`() = runComposeUiTest { |
| 39 | + setContent { LessonsLayout(UiState(isPersonalizationEnabled = true, eventSink = events)) } |
| 40 | + |
| 41 | + onNodeWithText(context.getString(R.string.dashboard_lessons_toggle_personalized)).assertExists() |
| 42 | + onNodeWithText(context.getString(R.string.dashboard_lessons_toggle_all)).assertExists() |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `PersonalizationToggle - click Personalized fires ChangeMode(PERSONALIZATION)`() = runComposeUiTest { |
| 47 | + setContent { |
| 48 | + LessonsLayout(UiState(isPersonalizationEnabled = true, mode = UiState.Mode.ALL_LESSONS, eventSink = events)) |
| 49 | + } |
| 50 | + |
| 51 | + onNodeWithText(context.getString(R.string.dashboard_lessons_toggle_personalized)).performClick() |
| 52 | + events.assertEvent(UiEvent.ChangeMode(UiState.Mode.PERSONALIZATION)) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `PersonalizationToggle - click All Lessons fires ChangeMode(ALL_LESSONS)`() = runComposeUiTest { |
| 57 | + setContent { |
| 58 | + LessonsLayout( |
| 59 | + UiState(isPersonalizationEnabled = true, mode = UiState.Mode.PERSONALIZATION, eventSink = events) |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + onNodeWithText(context.getString(R.string.dashboard_lessons_toggle_all)).performClick() |
| 64 | + events.assertEvent(UiEvent.ChangeMode(UiState.Mode.ALL_LESSONS)) |
| 65 | + } |
| 66 | + // endregion PersonalizationToggle |
| 67 | + |
| 68 | + // region LessonToolCard |
| 69 | + @Test |
| 70 | + fun `LessonToolCard - click fires ToolCardEvent_Click`() = runComposeUiTest { |
| 71 | + val cardEvents = TestEventSink<ToolCardPresenter.UiEvent>() |
| 72 | + setContent { |
| 73 | + LessonsLayout( |
| 74 | + UiState( |
| 75 | + lessons = listOf( |
| 76 | + ToolCardPresenter.UiState( |
| 77 | + toolCode = "lesson1", |
| 78 | + translation = randomTranslation(toolCode = "lesson1", name = "Test Lesson"), |
| 79 | + eventSink = cardEvents, |
| 80 | + ) |
| 81 | + ), |
| 82 | + eventSink = events, |
| 83 | + ) |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + onNodeWithText("Test Lesson").performClick() |
| 88 | + cardEvents.assertEvent(ToolCardPresenter.ToolCardEvent.Click) |
| 89 | + events.assertNoEvents() |
| 90 | + } |
| 91 | + // endregion LessonToolCard |
| 92 | +} |
0 commit comments