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

Hookup MvRxMocker and add tests #81

Closed
wants to merge 4 commits into from

Conversation

mmcwong
Copy link
Contributor

@mmcwong mmcwong commented Sep 5, 2018

This PR builds on to #72
It finishes up setting state directly and through args.
It also adds a test for the MvRxMocker

@elihart

Copy link
Contributor

@elihart elihart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmcwong nice, this is looking good. I'm not planning to merge my Pr since it's incomplete, so could you target master instead?

have just a few minor comments, then let's get this merged :)

@@ -79,8 +78,6 @@ internal open class MvRxStateStore<S : Any>(
* all of the code required.
*/
fun set(stateReducer: S.() -> S) {
if (mocker != null) return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we prevent other state from being set? I suppose it doesn't really matter, since we will still override it with the mocked state, but if other things set state I think it will trigger view invalidations and subscribe callbacks, which may interfere with the tests


fun <S : MvRxState> setMockedState(viewModel: BaseMvRxViewModel<S>, state: S) {
mockedState[viewModel] = state
fun <S : MvRxState> setMockedState(viewModel: BaseMvRxViewModel<S>, state: S) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe state should be nullable so the mocked state can be cleared?

@@ -1,23 +1,21 @@
package com.airbnb.mvrx

import android.os.Parcelable


object MvRxMocker {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some documentation to this file? always good to have it on the class and the methods to explain usage a bit

private val mockState = TestState("updated", 1)

@Test
fun testDisabled() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice tests!

fun <S : MvRxState> setMockedStateFromArg(viewModel: BaseMvRxViewModel<S>, args: Parcelable) {
mockedState[viewModel] = TODO()
fun <S : MvRxState> setMockedStateFromArgs(viewModel: BaseMvRxViewModel<S>, args: Any?) {
mockedState[viewModel.stateStore] = _initialStateProvider(viewModel.state::class.java, args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you chain this to setMockedState?


fun <S : MvRxState> setMockedState(viewModel: BaseMvRxViewModel<S>, state: S) {
mockedState[viewModel] = state
fun <S : MvRxState> setMockedState(viewModel: BaseMvRxViewModel<S>, state: S) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we may want to have the viewmodel trigger it's subscriber callbacks so they can act on the new state - that seems important for tests that want to test state transitions.

not immediately important - could be a follow up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that makes sense will address in another PR

@mmcwong mmcwong changed the base branch from eli-mvrx_mock to master September 6, 2018 18:25
@mmcwong
Copy link
Contributor Author

mmcwong commented Sep 6, 2018

@elihart updated with your feedback and changed the base branch

Copy link
Contributor

@elihart elihart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmcwong nice! just two minor things.

@BenSchwab @hellohuanlin could you also take a look?

initialState: S
) : Disposable {

internal val isMocked = MvRxMocker.enabled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need get() =, otherwise this won't update

@@ -72,6 +78,8 @@ internal open class MvRxStateStore<S : Any>(initialState: S) : Disposable {
* all of the code required.
*/
fun set(stateReducer: S.() -> S) {
if (MvRxMocker.getMockedState(this) != null) return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you see reasons to only block this if mocked state is set? I think it should be just

if (isMocked)

Because the view model or fragment might do things to set state before the mock is in place (the mock can only be set once the view model is created). Since setting state is async and can trigger subscribe callbacks it would be nice to eliminate any side effects from that

/**
* Enable or disable whether the viewModels use mocked states
*/
var enabled: Boolean = false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using static variables can cause a lot of headaches for tests. This will not be reset between tests. So if some test turns this on and forgets to turn it off it will stay on for all remaining tests.

@mmcwong
Copy link
Contributor Author

mmcwong commented Sep 21, 2018

Closing as #88 is a more testable approach

@mmcwong mmcwong closed this Sep 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants