-
Notifications
You must be signed in to change notification settings - Fork 499
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.airbnb.mvrx | ||
|
||
|
||
object MvRxMocker { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
var enabled: Boolean = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
private val mockedState: MutableMap<MvRxStateStore<*>, MvRxState> = mutableMapOf() | ||
|
||
fun <S : MvRxState> setMockedState(viewModel: BaseMvRxViewModel<S>, state: S) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe state should be nullable so the mocked state can be cleared? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah that makes sense will address in another PR |
||
mockedState[viewModel.stateStore] = state | ||
} | ||
|
||
fun <S : MvRxState> setMockedStateFromArgs(viewModel: BaseMvRxViewModel<S>, args: Any?) { | ||
mockedState[viewModel.stateStore] = _initialStateProvider(viewModel.state::class.java, args) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you chain this to |
||
} | ||
|
||
internal fun <S : Any> getMockedState(stateStore: MvRxStateStore<S>): S? { | ||
@Suppress("UNCHECKED_CAST") | ||
return if (enabled) mockedState[stateStore] as? S else null | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.airbnb.mvrx | ||
|
||
import junit.framework.Assert | ||
import org.junit.Test | ||
|
||
class MvRxMockerTest : BaseTest() { | ||
|
||
private val initialState = TestState("initial", 0) | ||
private val mockState = TestState("updated", 1) | ||
|
||
@Test | ||
fun testDisabled() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice tests! |
||
MvRxMocker.enabled = false | ||
|
||
val viewModel = TestViewModel(initialState) | ||
MvRxMocker.setMockedState(viewModel, mockState) | ||
|
||
withState(viewModel) { state -> | ||
Assert.assertEquals(state, initialState) | ||
} | ||
} | ||
|
||
@Test | ||
fun testEnabled() { | ||
MvRxMocker.enabled = true | ||
|
||
val viewModel = TestViewModel(initialState) | ||
MvRxMocker.setMockedState(viewModel, mockState) | ||
|
||
withState(viewModel) { state -> | ||
Assert.assertEquals(mockState, state) | ||
} | ||
} | ||
|
||
@Test | ||
fun testMockNotSet() { | ||
MvRxMocker.enabled = true | ||
|
||
val viewModel = TestViewModel(initialState) | ||
|
||
withState(viewModel) { state -> | ||
Assert.assertEquals(initialState, state) | ||
} | ||
} | ||
|
||
@Test | ||
fun testMockArgs() { | ||
MvRxMocker.enabled = true | ||
|
||
val viewModel = TestViewModel(initialState) | ||
val args = ParcelableArgs("test args") | ||
MvRxMocker.setMockedStateFromArgs(viewModel, args) | ||
|
||
withState(viewModel) { state -> | ||
Assert.assertEquals(TestState(args), state) | ||
} | ||
} | ||
|
||
private class TestViewModel(initialState: TestState) : TestMvRxViewModel<TestState>(initialState) | ||
} |
There was a problem hiding this comment.
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