Skip to content

Commit

Permalink
Add tests for skip positions
Browse files Browse the repository at this point in the history
  • Loading branch information
ashiagr committed Oct 26, 2022
1 parent 77824a5 commit 8edd34b
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Story>()
private val story2 = mock<Story>()
Expand Down Expand Up @@ -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<Float>()
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<Story>()
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<Float>()
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<Story>) : StoriesDataSource() {
override val stories = mutableListOf<Story>()

Expand Down

0 comments on commit 8edd34b

Please sign in to comment.