Skip to content

Instantly share code, notes, and snippets.

View elihart's full-sized avatar

Eli Hart elihart

  • Airbnb
  • San Francisco
View GitHub Profile
@elihart
elihart / MvRxViewModelTextLoadExample.kt
Created November 20, 2019 20:28
MvRxViewModelTextLoadExample
fun loadText(textId: Long) {
buildRequest<String>(
path = "text/endpoint",
params = {
kv("id", textId)
}
).execute {
copy(text = it)
}
}
@elihart
elihart / MvRxViewModelTextLoadingTest.kt
Created November 20, 2019 20:29
MvRxViewModelTextLoadingTest
@Test
fun loadText() = TextViewModel::loadText {
withParams(1)
expectRequests {
GET("text/endpoint?id=1") shouldReturn "server result"
} expectState {
copy(text = "server result")
}
}
@elihart
elihart / MvRxViewModelAutomaticTestExample.kt
Created November 20, 2019 20:32
MvRxViewModelPropertyTestExample
@Test
fun setBold() = TextViewModel::setBold {
sets {  ::options {  ::bold } }
}
@elihart
elihart / MvRxViewModelTestSetsMappedExample.kt
Created November 20, 2019 20:33
MvRxViewModelTestSetsMappedExample
@Test
fun squared() = TestViewModel::squareNumber {
setsMapped(2 to 4, 5 to 25) {  ::result }
}
@elihart
elihart / MvRxViewModelInitializationTestExample.kt
Created November 20, 2019 20:34
MvRxViewModelInitializationTestExample
@Test
fun initialization() = testInitialization(
expectRequests = {
GET("text/endpoint")
},
expectState = {
copy(text = Loading())
}
)
@elihart
elihart / HappoTestActivity.kt
Created November 20, 2019 20:40
HappoTestActivity
class HappoTestActivity : IntegrationTestActivity() {
override fun testCurrentScreen(
mockProvider: MockedFragmentProvider,
fragment: MvRxFragment,
resetView: (onViewReset: (MvRxFragment) -> Unit) -> Unit,
finishedTestingFragment: () -> Unit
) {
happoViewSnapshotBuilder.snap(
activity = this,
component = mockProvider.fragmentName,
@elihart
elihart / InteractionTestActivity.kt
Created November 20, 2019 20:41
InteractionTestActivity
class InteractionTestActivity : IntegrationTestActivity() {
override fun testCurrentScreen(
mockProvider: MockedFragmentProvider,
fragment: MvRxFragment,
resetView: (onViewReset: (MvRxFragment) -> Unit) -> Unit,
finishedTestingFragment: () -> Unit
) {
ActivityInteractionTester(
activity = this,
resetViewCallback = resetView,
@elihart
elihart / AirbnbIntegrationTestExamples.kt
Created November 20, 2019 20:43
AirbnbIntegrationTestExamples
@Test
fun screenshotBookingFragment() = runScreenshots("com.airbnb.booking.BookingFragment")
@Test
fun screenshotSearchFragment() = runScreenshots("com.airbnb.search.SearchFragment")
fun runScreenshots(fragmentName: String) {
val intent = IntegrationTestActivity.intent<HappoTestActivity>(
context = InstrumentationRegistry.getInstrumentation().targetContext,
fragmentName = fragmentName
@elihart
elihart / HowToWaitForIdleLoopers.kt
Created November 20, 2019 20:47
HowToWaitForIdleHandlers
fun waitForLoopers(loopers: List<Looper>) {
val idleDetectors = loopers.map { HandlerIdleDetector(Handler(it)) }
while (idleDetectors.any {  !it.isIdle }) {
}
}
class HandlerIdleDetector(val handler: Handler) {
var isIdle = false
init {
@elihart
elihart / TestContextFailureMessageExample.kt
Created November 20, 2019 20:52
TestContextFailureMessageExample
val testContextMessage = "Error while testing 'Default State' mock for BookingFragment -> Clicking 'book_button' view on thread 'AsyncTask #1'"
throw IllegalStateException(testContextMessage, originalException)