Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Commit

Permalink
LiveDataAction Unit tests implemented. Readme updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
İbrahim Yilmaz committed Jul 27, 2019
1 parent 6407310 commit bfddb8c
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## [Development]
- LiveDataAction implementation.
- ChangeLog updated for Bintray Download.
- ChangeLog format fixed.

Expand Down
25 changes: 23 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Extension Functions:

* `nonNullObserve` is extension function, which eliminates nullable invokes of Observer.

LiveDataAction:
* `LiveDataAction` is special object type for LiveData,which enables observer can listen LiveData object without parameter.
It is useful when Action/Command use cases.


Usage
-----
Expand Down Expand Up @@ -57,8 +61,7 @@ Usage

* **`nonNullObserve`**

LiveData that buffers all items it observes and replays them to any `Observer` that subscribes.

observe method which eliminates nullable invocation of observers.
```kotlin

val liveData = MutableLiveData<String>()
Expand All @@ -73,6 +76,24 @@ Usage
}

```
* **`LiveDataAction`**

Object Type which enables invocation listener methods without parameter

```kotlin

val liveData = MutableLiveData<LiveDataAction>()
...

viewModel.liveData.nonNullObserve(lifeCycleOwner,::onLiveDataAction)

...

fun onLiveDataEvent(){
//Do something really good!.
}

```

Download
--------
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/me/ibrahimyilmaz/arch_data/LiveDataExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package me.ibrahimyilmaz.arch_data

import androidx.annotation.MainThread
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer

object LiveDataAction

fun <T> mutableLiveDataOf(): MutableLiveData<T> = MutableLiveData()

Expand All @@ -17,3 +19,25 @@ fun <T> LiveData<T>.nonNullObserve(owner: LifecycleOwner, observer: (t: T) -> Un
it?.let(observer)
})
}

fun LiveData<LiveDataAction>.observe(owner: LifecycleOwner, observer: () -> Unit) {
this.observe(owner, Observer {
observer()
})
}

fun LiveData<LiveDataAction>.nonNullObserve(owner: LifecycleOwner, observer: () -> Unit) {
this.observe(owner, Observer {
it?.let { observer() }
})
}


fun MutableLiveData<LiveDataAction>.postValue() {
this.postValue(LiveDataAction)
}

@MainThread
fun MutableLiveData<LiveDataAction>.sendValue() {
this.value = LiveDataAction
}
93 changes: 93 additions & 0 deletions src/test/java/me/ibrahimyilmaz/arch_data/LiveDataExtensionsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package me.ibrahimyilmaz.arch_data

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.MutableLiveData
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import org.mockito.junit.MockitoJUnitRunner

@RunWith(MockitoJUnitRunner::class)
class LiveDataExtensionsTest {

@get:Rule
var rule: TestRule = InstantTaskExecutorRule()

private val lifecycleOwner = TestingLifeCycleOwner("lifecycleOwner")

@Test
fun `nonNullObserve should fire method with non null parameter when it is fired with non null data `() {
//GIVEN
val testData = "Test"
val liveData = MutableLiveData<String>()
val nonNullObserver = mock<(t: String) -> Unit>()
liveData.nonNullObserve(lifecycleOwner, nonNullObserver)

//WHEN
liveData.value = testData

//THEN
verify(nonNullObserver).invoke(testData)
}

@Test
fun `nonNullObserve should not fire method with non null parameter when it is fired with null data `() {
//GIVEN
val liveData = MutableLiveData<String>()
val nonNullObserver = mock<(t: String) -> Unit>()
liveData.nonNullObserve(lifecycleOwner, nonNullObserver)

//WHEN
liveData.value = null

//THEN
verify(nonNullObserver, times(0)).invoke(any())
}

@Test
fun `LiveData with LiveDataAction should fire observer without parameter`() {
//GIVEN
val liveData = MutableLiveData<LiveDataAction>()
val observerWithoutParameter = mock<() -> Unit>()
liveData.observe(lifecycleOwner, observerWithoutParameter)

//WHEN
liveData.value = null

//THEN
verify(observerWithoutParameter).invoke()
}

@Test
fun `LiveData with LiveDataAction should fire observer with nonNull value when it is observed by nonNullObserver`() {
//GIVEN
val liveData = MutableLiveData<LiveDataAction>()
val observerWithoutParameter = mock<() -> Unit>()
liveData.nonNullObserve(lifecycleOwner, observerWithoutParameter)

//WHEN
liveData.postValue()

//THEN
verify(observerWithoutParameter).invoke()
}

@Test
fun `LiveData with LiveDataAction should not fire observer with null value when it is observed by nonNullObserver`() {
//GIVEN
val liveData = MutableLiveData<LiveDataAction>()
val observerWithoutParameter = mock<() -> Unit>()
liveData.nonNullObserve(lifecycleOwner, observerWithoutParameter)

//WHEN
liveData.postValue(null)

//THEN
verify(observerWithoutParameter, times(0)).invoke()
}
}

0 comments on commit bfddb8c

Please sign in to comment.