| 1 |
package io.oxygenhub.android.ui |
| 2 |
|
| 3 |
import io.oxygenhub.android.FakeHubApi |
| 4 |
import io.oxygenhub.android.MainDispatcherRule |
| 5 |
import io.oxygenhub.android.api.HubException |
| 6 |
import io.oxygenhub.android.api.Organisation |
| 7 |
import io.oxygenhub.android.api.Owner |
| 8 |
import io.oxygenhub.android.api.Repository |
| 9 |
import io.oxygenhub.android.api.User |
| 10 |
import io.oxygenhub.android.ui.common.Load |
| 11 |
import io.oxygenhub.android.ui.owners.OwnerViewModel |
| 12 |
import org.junit.Assert.assertEquals |
| 13 |
import org.junit.Assert.assertTrue |
| 14 |
import org.junit.Rule |
| 15 |
import org.junit.Test |
| 16 |
|
| 17 |
class OwnerViewModelTest { |
| 18 |
@get:Rule val main = MainDispatcherRule() |
| 19 |
|
| 20 |
private val api = FakeHubApi().apply { |
| 21 |
repositories = { |
| 22 |
listOf( |
| 23 |
Repository("doomsdayone", "tetris", updatedMs = 1), |
| 24 |
Repository("andymccall", "oxygen", updatedMs = 5), |
| 25 |
Repository("doomsdayone", "supermarioland2", updatedMs = 9), |
| 26 |
) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
@Test |
| 31 |
fun aNameIsAnAccountOrElseAnOrganisation() { |
| 32 |
api.accounts = mapOf("andymccall" to User("andymccall", displayName = "Andy McCall")) |
| 33 |
api.orgs = mapOf("doomsdayone" to Organisation("doomsdayone", "Doomsday One", members = listOf("andymccall"))) |
| 34 |
|
| 35 |
val andy = (OwnerViewModel(api, "andymccall").state.value as Load.Ready).value |
| 36 |
assertEquals("Andy McCall", andy.owner.shownName) |
| 37 |
assertEquals(listOf("oxygen"), andy.repositories.map { it.name }) |
| 38 |
|
| 39 |
val org = (OwnerViewModel(api, "doomsdayone").state.value as Load.Ready).value |
| 40 |
assertTrue(org.owner is Owner.Org) |
| 41 |
assertEquals(listOf("andymccall"), (org.owner as Owner.Org).org.members) |
| 42 |
assertEquals(listOf("supermarioland2", "tetris"), org.repositories.map { it.name }) |
| 43 |
assertTrue("asked as an organisation once no account has the name", "org doomsdayone" in api.calls) |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
fun neitherIsAFailure() { |
| 48 |
assertTrue(OwnerViewModel(api, "nobody").state.value is Load.Failed) |
| 49 |
} |
| 50 |
} |