Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
Kotlin
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107package social.grain
import androidx.test.ext.junit.runners.AndroidJUnit4import org.junit.Afterimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Beforeimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport social.grain.feature.explore.ExploreViewModelimport social.grain.support.Fixturesimport social.grain.support.Harnessimport social.grain.support.MainDispatcherRuleimport social.grain.support.awaitCallimport social.grain.support.awaitValueimport social.grain.support.json
/** * Explore: the recent feed paged like any other, plus two chip rows that * arrive and fail on their own. */@RunWith(AndroidJUnit4::class)class ExploreViewModelTest {
@get:Rule val mainDispatcher = MainDispatcherRule()
private lateinit var harness: Harness
@Before fun setUp() { harness = Harness() harness.server .on("dev.hatk.describeLabels", """{"definitions":[]}""") .on("dev.hatk.getFeed", """{"items":[${json(Fixtures.gallery())}]}""") .on( "social.grain.unspecced.getCameras", """{"cameras":[{"camera":"Leica M6","photoCount":120}]}""", ) .on( "social.grain.unspecced.getLocations", """{"locations":[{"name":"Lisbon, PT","h3Index":"85390cb7fffffff","galleryCount":3}]}""", ) }
@After fun tearDown() = harness.tearDown()
private fun model() = ExploreViewModel( client = harness.client, auth = harness.auth, labelDefsCache = harness.labelDefinitions, )
@Test fun theGridIsTheRecentFeed() { val model = model() val state = model.state.awaitValue("the first page") { it.galleries.isNotEmpty() }
assertEquals("A walk in the fog", state.galleries.single().title) assertEquals("recent", harness.server.lastCall("dev.hatk.getFeed").params["feed"]) }
@Test fun theChipsComeFromBothIndexes() { val model = model() val discovery = model.discovery.awaitValue("both indexes") { it.cameras.isNotEmpty() && it.locations.isNotEmpty() }
assertEquals("Leica M6", discovery.cameras.single().camera) assertEquals("Lisbon, PT", discovery.locations.single().name) assertEquals("85390cb7fffffff", discovery.locations.single().h3Index) }
@Test fun oneIndexFailingLeavesTheOther() { harness.server.on("social.grain.unspecced.getCameras", """{"error":"no"}""", code = 500) val model = model()
val discovery = model.discovery.awaitValue("the locations") { it.locations.isNotEmpty() } harness.server.awaitCall("social.grain.unspecced.getCameras")
assertTrue(discovery.cameras.isEmpty()) assertEquals("Lisbon, PT", discovery.locations.single().name) }
@Test fun pullingToRefreshReReadsTheChipsAsWellAsTheGalleries() { val model = model() model.state.awaitValue("the first page") { it.galleries.isNotEmpty() } harness.server.awaitCall("social.grain.unspecced.getCameras") harness.server.awaitCall("social.grain.unspecced.getLocations") val feeds = harness.server.countOf("dev.hatk.getFeed") val cameras = harness.server.countOf("social.grain.unspecced.getCameras") val locations = harness.server.countOf("social.grain.unspecced.getLocations")
model.refreshAll()
harness.server.awaitCall("dev.hatk.getFeed", feeds + 1) harness.server.awaitCall("social.grain.unspecced.getCameras", cameras + 1) harness.server.awaitCall("social.grain.unspecced.getLocations", locations + 1) model.state.awaitValue("the refresh to finish") { !it.isRefreshing } }}