-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsersPagingSourceTest.kt
127 lines (106 loc) · 3.47 KB
/
UsersPagingSourceTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.theozgurr.composesamplepaginationapp
import androidx.paging.PagingSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.*
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
@OptIn(ExperimentalCoroutinesApi::class)
class UsersPagingSourceTest {
private val testScope = TestScope()
private val testDispatcher = StandardTestDispatcher(testScope.testScheduler)
private val userDtos = listOf(
UserDto(id = 0),
UserDto(id = 1),
UserDto(id = 2)
)
@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
}
@After
fun tearDown() {
Dispatchers.resetMain()
}
@Test
fun `when users are given then users paging source returns success load result`() =
testScope.runTest {
// given
val repository = FakeUserRemoteRepositorySuccessImpl()
val usersPagingSource = UsersPagingSource(
repository = repository
)
val params = PagingSource
.LoadParams
.Refresh(
key = 0,
loadSize = 1,
placeholdersEnabled = false
)
val expected = PagingSource
.LoadResult
.Page(
data = userDtos,
prevKey = null,
nextKey = 20
)
// when
val actual = usersPagingSource.load(params = params)
// then
assertEquals(expected, actual)
}
@Test
fun `when users are given then users paging source returns error load result`() =
testScope.runTest {
// given
val repository = FakeUserRemoteRepositoryErrorImpl()
val usersPagingSource = UsersPagingSource(
repository = repository
)
val params = PagingSource
.LoadParams
.Refresh(
key = 0,
loadSize = 1,
placeholdersEnabled = false
)
val expected = PagingSource
.LoadResult
.Error<Int, UserDto>(
throwable = RuntimeException("no data for you")
)::class.java
// when
val actual = usersPagingSource.load(params = params)::class.java
// then
assertEquals(expected, actual)
}
@Test
fun `when users are given for next pages then users paging source returns success append load result`() =
testScope.runTest {
// given
val repository = FakeUserRemoteRepositorySuccessImpl()
val usersPagingSource = UsersPagingSource(
repository = repository
)
val params = PagingSource
.LoadParams
.Append(
key = 20,
loadSize = 1,
placeholdersEnabled = false
)
val expected = PagingSource
.LoadResult
.Page(
data = userDtos,
prevKey = null,
nextKey = 40
)
// when
val actual = usersPagingSource.load(params = params)
// then
assertEquals(expected, actual)
}
}