This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #589: Add sample unit tests for a component
- Loading branch information
1 parent
a2031b9
commit 5cf61c9
Showing
9 changed files
with
170 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix | ||
|
||
import androidx.lifecycle.LifecycleOwner | ||
import io.mockk.Runs | ||
import io.mockk.every | ||
import io.mockk.just | ||
import io.mockk.mockk | ||
import io.reactivex.android.plugins.RxAndroidPlugins | ||
import io.reactivex.plugins.RxJavaPlugins | ||
import io.reactivex.schedulers.Schedulers | ||
import org.mozilla.fenix.mvi.ActionBusFactory | ||
|
||
object TestUtils { | ||
fun setRxSchedulers() { | ||
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() } | ||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() } | ||
} | ||
|
||
val owner = mockk<LifecycleOwner> { | ||
every { lifecycle } returns mockk() | ||
every { lifecycle.addObserver(any()) } just Runs | ||
} | ||
val bus: ActionBusFactory = ActionBusFactory.get(owner) | ||
} |
90 changes: 90 additions & 0 deletions
90
app/src/test/java/org/mozilla/fenix/library/history/HistoryComponentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.library.history | ||
|
||
import android.view.ViewGroup | ||
import io.mockk.MockKAnnotations | ||
import io.mockk.mockk | ||
import io.mockk.spyk | ||
import io.reactivex.Observer | ||
import io.reactivex.observers.TestObserver | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.mozilla.fenix.TestUtils.bus | ||
import org.mozilla.fenix.TestUtils.owner | ||
import org.mozilla.fenix.TestUtils.setRxSchedulers | ||
import org.mozilla.fenix.mvi.ActionBusFactory | ||
import org.mozilla.fenix.mvi.UIView | ||
import org.mozilla.fenix.mvi.getManagedEmitter | ||
|
||
class HistoryComponentTest { | ||
|
||
private lateinit var historyComponent: TestHistoryComponent | ||
private lateinit var historyObserver: TestObserver<HistoryState> | ||
private lateinit var emitter: Observer<HistoryChange> | ||
|
||
@BeforeEach | ||
fun setup() { | ||
MockKAnnotations.init(this) | ||
setRxSchedulers() | ||
|
||
historyComponent = spyk( | ||
TestHistoryComponent(mockk(), bus), | ||
recordPrivateCalls = true | ||
) | ||
historyObserver = historyComponent.internalRender(historyComponent.reducer).test() | ||
emitter = owner.getManagedEmitter() | ||
} | ||
|
||
@Test | ||
fun `add and remove one history item normally`() { | ||
val historyItem = HistoryItem(123, "http://mozilla.org") | ||
|
||
emitter.onNext(HistoryChange.Change(listOf(historyItem))) | ||
emitter.onNext(HistoryChange.EnterEditMode(historyItem)) | ||
emitter.onNext(HistoryChange.RemoveItemForRemoval(historyItem)) | ||
emitter.onNext(HistoryChange.AddItemForRemoval(historyItem)) | ||
emitter.onNext(HistoryChange.ExitEditMode) | ||
|
||
historyObserver.assertSubscribed().awaitCount(6).assertNoErrors() | ||
.assertValues( | ||
HistoryState(listOf(), HistoryState.Mode.Normal), | ||
HistoryState(listOf(historyItem), HistoryState.Mode.Normal), | ||
HistoryState(listOf(historyItem), HistoryState.Mode.Editing(listOf(historyItem))), | ||
HistoryState(listOf(historyItem), HistoryState.Mode.Editing(listOf())), | ||
HistoryState(listOf(historyItem), HistoryState.Mode.Editing(listOf(historyItem))), | ||
HistoryState(listOf(historyItem), HistoryState.Mode.Normal) | ||
) | ||
} | ||
|
||
@Test | ||
fun `try making changes when not in edit mode`() { | ||
val historyItems = listOf( | ||
HistoryItem(1337, "http://reddit.com"), | ||
HistoryItem(31337, "http://leethaxor.com") | ||
) | ||
|
||
emitter.onNext(HistoryChange.Change(historyItems)) | ||
emitter.onNext(HistoryChange.AddItemForRemoval(historyItems[0])) | ||
emitter.onNext(HistoryChange.EnterEditMode(historyItems[0])) | ||
emitter.onNext(HistoryChange.ExitEditMode) | ||
|
||
historyObserver.assertSubscribed().awaitCount(4).assertNoErrors() | ||
.assertValues( | ||
HistoryState(listOf(), HistoryState.Mode.Normal), | ||
HistoryState(historyItems, HistoryState.Mode.Normal), | ||
HistoryState(historyItems, HistoryState.Mode.Editing(listOf(historyItems[0]))), | ||
HistoryState(historyItems, HistoryState.Mode.Normal) | ||
) | ||
} | ||
|
||
@Suppress("MemberVisibilityCanBePrivate") | ||
class TestHistoryComponent(container: ViewGroup, bus: ActionBusFactory) : | ||
HistoryComponent(container, bus) { | ||
|
||
override val uiView: UIView<HistoryState, HistoryAction, HistoryChange> | ||
get() = mockk(relaxed = true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
architecture/src/main/java/org/mozilla/fenix/test/Mockable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.test | ||
|
||
annotation class Mockable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters