[fenix] Close https://github.com/mozilla-mobile/fenix/issues/20320: Use scope from testDispatcher in RecentBookmarksFeatureTest

pull/600/head
Jonathan Almeida 3 years ago committed by mergify[bot]
parent c695a5c533
commit c12a5b98ba

@ -4,6 +4,7 @@
package org.mozilla.fenix.home.recentbookmarks package org.mozilla.fenix.home.recentbookmarks
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@ -22,16 +23,18 @@ import org.mozilla.fenix.home.HomeFragmentStore
* @param bookmarksUseCase the [BookmarksUseCase] for retrieving the list of recently saved * @param bookmarksUseCase the [BookmarksUseCase] for retrieving the list of recently saved
* bookmarks from storage. * bookmarks from storage.
* @param scope the [CoroutineScope] used to fetch the bookmarks list * @param scope the [CoroutineScope] used to fetch the bookmarks list
* @param ioDispatcher the [CoroutineDispatcher] for performing read/write operations.
*/ */
class RecentBookmarksFeature( class RecentBookmarksFeature(
private val homeStore: HomeFragmentStore, private val homeStore: HomeFragmentStore,
private val bookmarksUseCase: BookmarksUseCase, private val bookmarksUseCase: BookmarksUseCase,
private val scope: CoroutineScope private val scope: CoroutineScope,
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
) : LifecycleAwareFeature { ) : LifecycleAwareFeature {
internal var job: Job? = null internal var job: Job? = null
override fun start() { override fun start() {
job = scope.launch(Dispatchers.IO) { job = scope.launch(ioDispatcher) {
val bookmarks = bookmarksUseCase.retrieveRecentBookmarks() val bookmarks = bookmarksUseCase.retrieveRecentBookmarks()
homeStore.dispatch(HomeFragmentAction.RecentBookmarksChange(bookmarks)) homeStore.dispatch(HomeFragmentAction.RecentBookmarksChange(bookmarks))

@ -7,11 +7,9 @@ package org.mozilla.fenix.home.recentbookmarks
import io.mockk.coEvery import io.mockk.coEvery
import io.mockk.coVerify import io.mockk.coVerify
import io.mockk.mockk import io.mockk.mockk
import io.mockk.spyk import kotlinx.coroutines.CoroutineScope
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.runBlockingTest import kotlinx.coroutines.test.runBlockingTest
import mozilla.components.concept.storage.BookmarkNode import mozilla.components.concept.storage.BookmarkNode
import mozilla.components.concept.storage.BookmarkNodeType import mozilla.components.concept.storage.BookmarkNodeType
@ -20,8 +18,6 @@ import mozilla.components.support.test.middleware.CaptureActionsMiddleware
import mozilla.components.support.test.rule.MainCoroutineRule import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.After import org.junit.After
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
@ -36,7 +32,6 @@ class RecentBookmarksFeatureTest {
private val middleware = CaptureActionsMiddleware<HomeFragmentState, HomeFragmentAction>() private val middleware = CaptureActionsMiddleware<HomeFragmentState, HomeFragmentAction>()
private val homeStore = HomeFragmentStore(middlewares = listOf(middleware)) private val homeStore = HomeFragmentStore(middlewares = listOf(middleware))
private val bookmarksUseCases: BookmarksUseCase = mockk(relaxed = true) private val bookmarksUseCases: BookmarksUseCase = mockk(relaxed = true)
private val scope = TestCoroutineScope()
private val testDispatcher = TestCoroutineDispatcher() private val testDispatcher = TestCoroutineDispatcher()
private val bookmark = BookmarkNode( private val bookmark = BookmarkNode(
type = BookmarkNodeType.ITEM, type = BookmarkNodeType.ITEM,
@ -58,7 +53,6 @@ class RecentBookmarksFeatureTest {
@After @After
fun cleanUp() { fun cleanUp() {
scope.cleanupTestCoroutines()
testDispatcher.cleanupTestCoroutines() testDispatcher.cleanupTestCoroutines()
} }
@ -68,13 +62,14 @@ class RecentBookmarksFeatureTest {
val feature = RecentBookmarksFeature( val feature = RecentBookmarksFeature(
homeStore, homeStore,
bookmarksUseCases, bookmarksUseCases,
scope CoroutineScope(testDispatcher),
testDispatcher
) )
feature.start()
assertEquals(emptyList<BookmarkNode>(), homeStore.state.recentBookmarks) assertEquals(emptyList<BookmarkNode>(), homeStore.state.recentBookmarks)
feature.start()
testDispatcher.advanceUntilIdle() testDispatcher.advanceUntilIdle()
homeStore.waitUntilIdle() homeStore.waitUntilIdle()
@ -86,23 +81,4 @@ class RecentBookmarksFeatureTest {
assertEquals(listOf(bookmark), it.recentBookmarks) assertEquals(listOf(bookmark), it.recentBookmarks)
} }
} }
@Test
fun `WHEN the feature is destroyed THEN the job is cancelled`() {
val feature = spyk(RecentBookmarksFeature(
homeStore,
bookmarksUseCases,
scope
))
assertNull(feature.job)
feature.start()
assertNotNull(feature.job)
feature.stop()
verify(exactly = 1) { feature.stop() }
}
} }

Loading…
Cancel
Save