| 1 |
package io.oxygenhub.android.ui |
| 2 |
|
| 3 |
import io.oxygenhub.android.FakeHubApi |
| 4 |
import io.oxygenhub.android.Fixtures |
| 5 |
import io.oxygenhub.android.MainDispatcherRule |
| 6 |
import io.oxygenhub.android.api.Avatar |
| 7 |
import io.oxygenhub.android.api.HubException |
| 8 |
import io.oxygenhub.android.auth.Session |
| 9 |
import io.oxygenhub.android.ui.common.Load |
| 10 |
import io.oxygenhub.android.ui.dashboard.Dashboard |
| 11 |
import io.oxygenhub.android.ui.dashboard.DashboardViewModel |
| 12 |
import org.junit.Assert.assertEquals |
| 13 |
import org.junit.Assert.assertFalse |
| 14 |
import org.junit.Assert.assertNull |
| 15 |
import org.junit.Assert.assertTrue |
| 16 |
import org.junit.Rule |
| 17 |
import org.junit.Test |
| 18 |
|
| 19 |
class DashboardViewModelTest { |
| 20 |
@get:Rule val main = MainDispatcherRule() |
| 21 |
|
| 22 |
private val api = FakeHubApi() |
| 23 |
private val session = Session("https://oxygenhub.io", "andymccall", "t") |
| 24 |
|
| 25 |
private fun ready(vm: DashboardViewModel): Dashboard = (vm.state.value as Load.Ready).value |
| 26 |
|
| 27 |
@Test |
| 28 |
fun greetsAndListsTheUsersRecentRepositories() { |
| 29 |
val d = ready(DashboardViewModel(api, session) { 9 }) |
| 30 |
assertEquals("Good morning, Andy McCall", d.greeting) |
| 31 |
assertTrue(d.recentAreYours) |
| 32 |
assertEquals(listOf("andymccall/oxygenflow", "andymccall/secret-plans"), d.recent.map { it.slug }) |
| 33 |
assertEquals(2, d.repositoryCount) |
| 34 |
} |
| 35 |
|
| 36 |
@Test |
| 37 |
fun showsTheAccountsAvatarAndKeepsItWhenTheHubFails() { |
| 38 |
val mine = Avatar.Identicon(196, setOf(3 to 3)) |
| 39 |
api.avatar = { if (it == "andymccall") mine else null } |
| 40 |
val vm = DashboardViewModel(api, session) { 9 } |
| 41 |
assertEquals(mine, ready(vm).avatar) |
| 42 |
assertEquals("andymccall", ready(vm).username) |
| 43 |
// A failed fetch keeps the one before; the page still loads. |
| 44 |
api.avatar = { throw HubException.Http(500, "down") } |
| 45 |
vm.refresh() |
| 46 |
assertEquals(mine, ready(vm).avatar) |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
fun showsProposalsToReviewAndNoneFromAHubWithoutTheCall() { |
| 51 |
val vm = DashboardViewModel(api, session) { 9 } |
| 52 |
assertEquals(listOf(300L), ready(vm).proposals?.review?.map { it.number }) |
| 53 |
assertEquals("andymccall" to "oxygenhub", ready(vm).proposals!!.review[0].let { it.owner to it.name }) |
| 54 |
api.userProposals = { throw HubException.Http(404, "no such endpoint") } |
| 55 |
vm.refresh() |
| 56 |
assertNull(ready(vm).proposals) |
| 57 |
assertEquals(2, ready(vm).repositoryCount) |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
fun withNoRepositoriesOfTheirOwnShowsTheHubs() { |
| 62 |
val d = ready(DashboardViewModel(api, session.copy(username = "newcomer")) { 14 }.also { |
| 63 |
api.user = { Fixtures.user.copy(username = "newcomer", displayName = "") } |
| 64 |
it.refresh() |
| 65 |
}) |
| 66 |
assertEquals("Good afternoon, newcomer", d.greeting) |
| 67 |
assertFalse(d.recentAreYours) |
| 68 |
assertEquals("doomsdayone/tetris", d.recent.first().slug) |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
fun withoutUserReadGreetsByTheSessionsName() { |
| 73 |
api.user = { throw HubException.Http(403, "this token lacks the user:read scope") } |
| 74 |
val d = ready(DashboardViewModel(api, session) { 22 }) |
| 75 |
assertEquals("Good evening, andymccall", d.greeting) |
| 76 |
assertNull(d.user) |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
fun aFailureAndARetry() { |
| 81 |
api.repositories = { throw HubException.Http(503, "down") } |
| 82 |
val vm = DashboardViewModel(api, session) { 9 } |
| 83 |
assertTrue(vm.state.value is Load.Failed) |
| 84 |
api.repositories = { Fixtures.repos } |
| 85 |
vm.refresh() |
| 86 |
assertTrue(vm.state.value is Load.Ready) |
| 87 |
} |
| 88 |
|
| 89 |
@Test |
| 90 |
fun greetings() { |
| 91 |
assertEquals("Good evening, a", DashboardViewModel.greeting(4, "a")) |
| 92 |
assertEquals("Good morning, a", DashboardViewModel.greeting(5, "a")) |
| 93 |
assertEquals("Good afternoon, a", DashboardViewModel.greeting(12, "a")) |
| 94 |
assertEquals("Good evening, a", DashboardViewModel.greeting(18, "a")) |
| 95 |
} |
| 96 |
} |