OXYGENHUB
# 144cbf7bf2b5 oxygenhub-android / app / src / test / java / io / oxygenhub / android / ui / DashboardViewModelTest.kt
2.6 KiB · last changed Sign-in, dashboard, repositories, issues and inbox screens, adaptive (refs #2) 11 days ago source blame history raw
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.HubException
7
import io.oxygenhub.android.auth.Session
8
import io.oxygenhub.android.ui.common.Load
9
import io.oxygenhub.android.ui.dashboard.Dashboard
10
import io.oxygenhub.android.ui.dashboard.DashboardViewModel
11
import org.junit.Assert.assertEquals
12
import org.junit.Assert.assertFalse
13
import org.junit.Assert.assertNull
14
import org.junit.Assert.assertTrue
15
import org.junit.Rule
16
import org.junit.Test
17
18
class DashboardViewModelTest {
19
    @get:Rule val main = MainDispatcherRule()
20
21
    private val api = FakeHubApi()
22
    private val session = Session("https://oxygenhub.io", "andymccall", "t")
23
24
    private fun ready(vm: DashboardViewModel): Dashboard = (vm.state.value as Load.Ready).value
25
26
    @Test
27
    fun greetsAndListsTheUsersRecentRepositories() {
28
        val d = ready(DashboardViewModel(api, session) { 9 })
29
        assertEquals("Good morning, Andy McCall", d.greeting)
30
        assertTrue(d.recentAreYours)
31
        assertEquals(listOf("andymccall/oxygenflow", "andymccall/secret-plans"), d.recent.map { it.slug })
32
        assertEquals(2, d.repositoryCount)
33
    }
34
35
    @Test
36
    fun withNoRepositoriesOfTheirOwnShowsTheHubs() {
37
        val d = ready(DashboardViewModel(api, session.copy(username = "newcomer")) { 14 }.also {
38
            api.user = { Fixtures.user.copy(username = "newcomer", displayName = "") }
39
            it.refresh()
40
        })
41
        assertEquals("Good afternoon, newcomer", d.greeting)
42
        assertFalse(d.recentAreYours)
43
        assertEquals("doomsdayone/tetris", d.recent.first().slug)
44
    }
45
46
    @Test
47
    fun withoutUserReadGreetsByTheSessionsName() {
48
        api.user = { throw HubException.Http(403, "this token lacks the user:read scope") }
49
        val d = ready(DashboardViewModel(api, session) { 22 })
50
        assertEquals("Good evening, andymccall", d.greeting)
51
        assertNull(d.user)
52
    }
53
54
    @Test
55
    fun aFailureAndARetry() {
56
        api.repositories = { throw HubException.Http(503, "down") }
57
        val vm = DashboardViewModel(api, session) { 9 }
58
        assertTrue(vm.state.value is Load.Failed)
59
        api.repositories = { Fixtures.repos }
60
        vm.refresh()
61
        assertTrue(vm.state.value is Load.Ready)
62
    }
63
64
    @Test
65
    fun greetings() {
66
        assertEquals("Good evening, a", DashboardViewModel.greeting(4, "a"))
67
        assertEquals("Good morning, a", DashboardViewModel.greeting(5, "a"))
68
        assertEquals("Good afternoon, a", DashboardViewModel.greeting(12, "a"))
69
        assertEquals("Good evening, a", DashboardViewModel.greeting(18, "a"))
70
    }
71
}