From 6c80e5c476f86251b2163652b5c06e20f5991f19 Mon Sep 17 00:00:00 2001 From: mcarare <48995920+mcarare@users.noreply.github.com> Date: Wed, 24 May 2023 17:35:33 +0300 Subject: [PATCH] Bug 1834746 - Refactor tests to remove mockkStatic. --- .../fenix/search/SearchDialogFragmentTest.kt | 64 ++++++++----------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/app/src/test/java/org/mozilla/fenix/search/SearchDialogFragmentTest.kt b/app/src/test/java/org/mozilla/fenix/search/SearchDialogFragmentTest.kt index 3d04859139..c7aa3e72bb 100644 --- a/app/src/test/java/org/mozilla/fenix/search/SearchDialogFragmentTest.kt +++ b/app/src/test/java/org/mozilla/fenix/search/SearchDialogFragmentTest.kt @@ -20,8 +20,6 @@ import io.mockk.unmockkStatic import io.mockk.verify import mozilla.components.browser.state.search.SearchEngine import mozilla.components.browser.state.state.SearchState -import mozilla.components.browser.state.state.searchEngines -import mozilla.components.browser.state.state.selectedOrDefaultSearchEngine import mozilla.components.support.test.robolectric.testContext import org.junit.After import org.junit.Assert.assertEquals @@ -31,8 +29,7 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mozilla.fenix.HomeActivity -import org.mozilla.fenix.components.Components -import org.mozilla.fenix.ext.requireComponents +import org.mozilla.fenix.ext.components import org.mozilla.fenix.helpers.FenixRobolectricTestRunner @RunWith(FenixRobolectricTestRunner::class) @@ -99,58 +96,53 @@ internal class SearchDialogFragmentTest { @Test fun `GIVEN the default search engine is currently selected WHEN checking the need to update the current search engine THEN don't to anything`() { - fragment.interactor = mockk() + val searchDialogFragment = spyk(fragment) + val interactor = spyk(SearchDialogInteractor(mockk())) + + every { searchDialogFragment.interactor } returns interactor + val defaultSearchEngine: SearchEngine = mockk { every { id } returns "default" } val otherSearchEngine: SearchEngine = mockk { every { id } returns "other" } - val components: Components = mockk { - every { core.store.state.search } returns mockk(relaxed = true) { - every { searchEngines } returns listOf(otherSearchEngine, defaultSearchEngine) - } - } - mockkStatic( - "org.mozilla.fenix.ext.FragmentKt", - "mozilla.components.browser.state.state.SearchStateKt", - ) { - every { any().requireComponents } returns components - every { any().selectedOrDefaultSearchEngine } returns defaultSearchEngine - fragment.maybeSelectShortcutEngine(defaultSearchEngine.id) + every { searchDialogFragment.requireContext() } returns testContext + every { testContext.components.core.store.state.search } returns SearchState( + regionSearchEngines = listOf(defaultSearchEngine, otherSearchEngine), + userSelectedSearchEngineId = "default", + ) - verify { fragment.interactor wasNot Called } - } + searchDialogFragment.maybeSelectShortcutEngine(defaultSearchEngine.id) + + verify { interactor wasNot Called } } @Test fun `GIVEN the default search engine is not currently selected WHEN checking the need to update the current search engine THEN update it to the current engine`() { - fragment.interactor = mockk { - every { onSearchShortcutEngineSelected(any()) } just Runs - } + val searchDialogFragment = spyk(fragment) + val interactor = spyk(SearchDialogInteractor(mockk())) + + every { searchDialogFragment.interactor } returns interactor + every { interactor.onSearchShortcutEngineSelected(any()) } just Runs + val defaultSearchEngine: SearchEngine = mockk { every { id } returns "default" } val otherSearchEngine: SearchEngine = mockk { every { id } returns "other" } - val components: Components = mockk { - every { core.store.state.search } returns mockk(relaxed = true) { - every { searchEngines } returns listOf(otherSearchEngine, defaultSearchEngine) - } - } - mockkStatic( - "org.mozilla.fenix.ext.FragmentKt", - "mozilla.components.browser.state.state.SearchStateKt", - ) { - every { any().requireComponents } returns components - every { any().selectedOrDefaultSearchEngine } returns defaultSearchEngine - fragment.maybeSelectShortcutEngine(otherSearchEngine.id) + every { searchDialogFragment.requireContext() } returns testContext + every { testContext.components.core.store.state.search } returns SearchState( + regionSearchEngines = listOf(defaultSearchEngine, otherSearchEngine), + userSelectedSearchEngineId = "default", + ) - verify { fragment.interactor.onSearchShortcutEngineSelected(otherSearchEngine) } - } + searchDialogFragment.maybeSelectShortcutEngine(otherSearchEngine.id) + + verify { interactor.onSearchShortcutEngineSelected(any()) } } @Test