OXYGENHUB
# 1823bc12eda1 oxygenhub-android / app / src / test / java / io / oxygenhub / android / ui / IssueViewModelTest.kt
4.0 KiB · last changed Assign issues from the phone: the Assignees action, with Assign me and any username, saved as the whole set (refs #4) 8 days ago source blame history raw
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.Comment
6
import io.oxygenhub.android.api.HubException
7
import io.oxygenhub.android.api.Issue
8
import io.oxygenhub.android.ui.common.Load
9
import io.oxygenhub.android.ui.issues.IssueViewModel
10
import io.oxygenhub.android.ui.issues.NewIssueViewModel
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 IssueViewModelTest {
19
    @get:Rule val main = MainDispatcherRule()
20
21
    private val api = FakeHubApi().apply {
22
        issueStore[7] = Issue(7, "Crash on start", "It **crashes**.", author = "bob", comments = 1)
23
        commentStore[7] = mutableListOf(Comment(1, "bob", "Still happens"))
24
    }
25
26
    private fun thread(vm: IssueViewModel) = (vm.state.value as Load.Ready).value
27
28
    @Test
29
    fun showsTheIssueAndItsComments() {
30
        val vm = IssueViewModel(api, "andymccall", "demo", 7)
31
        assertEquals("Crash on start", thread(vm).issue.title)
32
        assertEquals(listOf("Still happens"), thread(vm).comments.map { it.body })
33
    }
34
35
    @Test
36
    fun aCommentJoinsTheThreadAndTheBoxEmpties() {
37
        val vm = IssueViewModel(api, "andymccall", "demo", 7)
38
        var done: Boolean? = null
39
        vm.comment("  Fixed in 4f07c5a0  ") { done = it }
40
        assertEquals(true, done)
41
        assertEquals(listOf("Still happens", "Fixed in 4f07c5a0"), thread(vm).comments.map { it.body })
42
        assertEquals(2, thread(vm).issue.comments)
43
        assertFalse(vm.working.value)
44
    }
45
46
    @Test
47
    fun closingAndReopeningChangeItsState() {
48
        val vm = IssueViewModel(api, "andymccall", "demo", 7)
49
        vm.close("not_planned")
50
        assertFalse(thread(vm).issue.isOpen)
51
        assertEquals("not_planned", thread(vm).issue.reason)
52
        assertTrue("state 7 closed:not_planned" in api.calls)
53
        vm.reopen()
54
        assertTrue(thread(vm).issue.isOpen)
55
    }
56
57
    @Test
58
    fun assigneesAreSetAsAWholeSetTidied() {
59
        val vm = IssueViewModel(api, "andymccall", "demo", 7)
60
        vm.setAssignees(listOf(" andymccall ", "@bob", "bob", ""))
61
        assertEquals(listOf("andymccall", "bob"), thread(vm).issue.assignees)
62
        assertTrue("assign 7 andymccall,bob" in api.calls)
63
        vm.setAssignees(emptyList())
64
        assertEquals(emptyList<String>(), thread(vm).issue.assignees)
65
    }
66
67
    @Test
68
    fun labelsAreSetAsAWholeSet() {
69
        val vm = IssueViewModel(api, "andymccall", "demo", 7)
70
        vm.loadLabels()
71
        assertEquals(listOf("bug", "ui"), (vm.labels.value as Load.Ready).value.map { it.name })
72
        vm.setLabels(listOf("bug"))
73
        assertEquals(listOf("bug"), thread(vm).issue.labels.map { it.name })
74
    }
75
76
    @Test
77
    fun whatTheHubRefusesIsSaidAndThePageStays() {
78
        val vm = IssueViewModel(api, "andymccall", "demo", 7)
79
        api.commentStore.clear()
80
        val failing = object : io.oxygenhub.android.api.HubApi by api {
81
            override suspend fun comment(owner: String, name: String, number: Long, body: String): Comment =
82
                throw HubException.Http(403, "the issue is locked")
83
        }
84
        val locked = IssueViewModel(failing, "andymccall", "demo", 7)
85
        var done: Boolean? = null
86
        locked.comment("hello") { done = it }
87
        assertEquals(false, done)
88
        assertTrue(locked.error.value!!.contains("locked"))
89
        assertEquals("Crash on start", thread(locked).issue.title)
90
        locked.dismissError()
91
        assertNull(locked.error.value)
92
        assertEquals("Crash on start", thread(vm).issue.title)
93
    }
94
95
    @Test
96
    fun aNewIssueOpensWithItsTitle() {
97
        val vm = NewIssueViewModel(api, "andymccall", "demo")
98
        var opened: Issue? = null
99
        vm.open("  ", "body") { opened = it }
100
        assertNull("no title, nothing opened", opened)
101
        vm.open(" Board grows ", "Done keeps growing") { opened = it }
102
        assertEquals("Board grows", opened?.title)
103
        assertEquals(8L, opened?.number)
104
    }
105
}