Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for BasePresenter #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.intrepid.skeleton.base

import io.intrepid.skotlinton.base.BaseContract
import io.intrepid.skotlinton.base.BasePresenter
import io.intrepid.skotlinton.base.PresenterConfiguration
import io.intrepid.skotlinton.testutils.PresenterTestBase
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito.*

class BasePresenterTest : PresenterTestBase<BasePresenter<BaseContract.View>>() {
@Mock
private lateinit var view: BaseContract.View

@Mock
private lateinit var onBind: Runnable

@Mock
private lateinit var onUnbind: Runnable

private lateinit var disposable: Disposable

@Before
fun setUp() {
presenter = TestPresenter(view, testConfiguration)
}

@Test
fun bind_onlyOnce() {
presenter.bindView(view)
presenter.bindView(view)
verify<Runnable>(onBind, times(1)).run()
}

@Test
fun unbind_onlyOnce() {
presenter.unbindView()
verify<Runnable>(onUnbind, never()).run()

presenter.bindView(view)
presenter.unbindView()
presenter.unbindView()
verify<Runnable>(onUnbind, times(1)).run()
}

@Test
fun unbind_clearsDisposables() {
presenter.bindView(view)
assertFalse(disposable.isDisposed)

presenter.unbindView()
assertTrue(disposable.isDisposed)
}

private inner class TestPresenter(view: BaseContract.View, configuration: PresenterConfiguration) :
BasePresenter<BaseContract.View>(view, configuration) {

override fun onViewBound() {
onBind.run()
disposable = Observable.never<Any>().subscribe()
disposables.add(disposable)
}

override fun onViewUnbound() {
onUnbind.run()
}
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.intrepid.skotlinton.screens.example1

import io.intrepid.skotlinton.testutils.BasePresenterTest
import io.intrepid.skotlinton.testutils.PresenterTestBase
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito.verify

internal class Example1PresenterTest : BasePresenterTest<Example1Presenter>() {
internal class Example1PresenterTest : PresenterTestBase<Example1Presenter>() {

@Mock
lateinit var mockView: Example1Contract.View
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package io.intrepid.skotlinton.screens.example2
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import io.intrepid.skotlinton.models.IpModel
import io.intrepid.skotlinton.testutils.BasePresenterTest
import io.intrepid.skotlinton.testutils.PresenterTestBase
import io.reactivex.Single
import org.junit.Before
import org.junit.Test
import org.mockito.Mock

internal class Example2PresenterTest : BasePresenterTest<Example2Presenter>() {
internal class Example2PresenterTest : PresenterTestBase<Example2Presenter>() {
@Mock
lateinit var mockView: Example2Contract.View

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.junit.Rule
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule

abstract class BasePresenterTest<P : BasePresenter<out BaseContract.View>> {
abstract class PresenterTestBase<P : BasePresenter<out BaseContract.View>> {
@Rule
@JvmField
val mockitoRule: MockitoRule = MockitoJUnit.rule()
Expand Down