From 8edd34bfcfde75a73e11c1e307ffd05bfb1a7798 Mon Sep 17 00:00:00 2001 From: ashiagr Date: Wed, 26 Oct 2022 13:19:35 +0530 Subject: [PATCH] Add tests for skip positions --- .../endofyear/StoriesViewModelTest.kt | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/modules/features/endofyear/src/test/java/au/com/shiftyjelly/pocketcasts/endofyear/StoriesViewModelTest.kt b/modules/features/endofyear/src/test/java/au/com/shiftyjelly/pocketcasts/endofyear/StoriesViewModelTest.kt index 97234b38330..1610f1051a3 100644 --- a/modules/features/endofyear/src/test/java/au/com/shiftyjelly/pocketcasts/endofyear/StoriesViewModelTest.kt +++ b/modules/features/endofyear/src/test/java/au/com/shiftyjelly/pocketcasts/endofyear/StoriesViewModelTest.kt @@ -1,6 +1,7 @@ package au.com.shiftyjelly.pocketcasts.endofyear import au.com.shiftyjelly.pocketcasts.endofyear.stories.Story +import au.com.shiftyjelly.pocketcasts.utils.seconds import junit.framework.TestCase.assertEquals import junit.framework.TestCase.assertTrue import kotlinx.coroutines.Dispatchers @@ -18,6 +19,8 @@ import org.junit.runner.RunWith import org.mockito.junit.MockitoJUnitRunner import org.mockito.kotlin.mock import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever +import kotlin.math.roundToInt private val story1 = mock() private val story2 = mock() @@ -109,6 +112,48 @@ class StoriesViewModelTest { assertEquals(state.currentStory, story1) } + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `when next is invoked, then story skips to correct position`() = runTest { + whenever(story1.storyLength).thenReturn(5.seconds()) + whenever(story2.storyLength).thenReturn(10.seconds()) + val viewModel = StoriesViewModel(MockStoriesDataSource(listOf(story1, story2))) + val progress = mutableListOf() + val collectJob = launch(UnconfinedTestDispatcher()) { + viewModel.progress.collect { + progress.add(it) + } + } + + viewModel.skipNext() + + assertEquals(0.34f, (progress.last() * 100f).roundToInt() / 100f) + collectJob.cancel() + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `when prev is invoked, then story skips to correct position`() = runTest { + val story3 = mock() + whenever(story1.storyLength).thenReturn(5.seconds()) + whenever(story2.storyLength).thenReturn(10.seconds()) + whenever(story3.storyLength).thenReturn(5.seconds()) + val viewModel = StoriesViewModel(MockStoriesDataSource(listOf(story1, story2, story3))) + viewModel.skipNext() + viewModel.skipNext() // At last story + val progress = mutableListOf() + val collectJob = launch(UnconfinedTestDispatcher()) { + viewModel.progress.collect { + progress.add(it) + } + } + + viewModel.skipPrevious() // Skip to middle story + + assertEquals(0.25f, (progress.last() * 100f).roundToInt() / 100f) + collectJob.cancel() + } + class MockStoriesDataSource(private val mockStories: List) : StoriesDataSource() { override val stories = mutableListOf()