Skip to content

Commit c07fb21

Browse files
author
Peter Bryant
committed
📝 Add documentation comments to test library
1 parent f621e48 commit c07fb21

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

test/src/main/java/com/ptrbrynt/kotlin_bloc/test/MockBloc.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ import io.mockk.mockk
88
import kotlinx.coroutines.flow.emptyFlow
99
import kotlin.reflect.KClass
1010

11+
/**
12+
* Creates a mocked instance of the given [Cubit] type [C].
13+
*
14+
* Uses [mockk] under the hood.
15+
*
16+
* @param name Mock name
17+
* @param relaxed Allows creation without any explicit behavior
18+
* @param moreInterfaces Additional interfaces for this mock to implement
19+
* @param relaxUnitFun Allows creation with no specific behaviour for Unit function
20+
* @param block Block to execute after the mock has been created, with the mock as the receiver
21+
* @see mockk
22+
*/
1123
inline fun <reified C : Cubit<State>, reified State> mockCubit(
1224
name: String? = null,
1325
relaxed: Boolean = false,
@@ -27,6 +39,18 @@ inline fun <reified C : Cubit<State>, reified State> mockCubit(
2739
}
2840
}
2941

42+
/**
43+
* Creates a mocked instance of the given [Bloc] type [B].
44+
*
45+
* Uses [mockk] under the hood.
46+
*
47+
* @param name Mock name
48+
* @param relaxed Allows creation without any explicit behavior
49+
* @param moreInterfaces Additional interfaces for this mock to implement
50+
* @param relaxUnitFun Allows creation with no specific behaviour for Unit function
51+
* @param block Block to execute after the mock has been created, with the mock as the receiver
52+
* @see mockk
53+
*/
3054
inline fun <reified B : Bloc<Event, State>, reified Event, reified State> mockBloc(
3155
name: String? = null,
3256
relaxed: Boolean = false,

test/src/main/java/com/ptrbrynt/kotlin_bloc/test/WhenListen.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@ package com.ptrbrynt.kotlin_bloc.test
33
import com.ptrbrynt.kotlin_bloc.core.BlocBase
44
import io.mockk.every
55
import kotlinx.coroutines.flow.Flow
6+
import kotlinx.coroutines.flow.onEach
67

8+
/**
9+
* Creates a stub response for the `stateFlow` property of the given [bloc].
10+
*
11+
* Use [whenListen] when you want to return a canned [Flow] of [State]s for a [bloc] instance.
12+
*
13+
* [whenListen] also handles stubbing the `state` of the [bloc] to stay in sync with the emitted
14+
* state.
15+
*/
716
fun <B : BlocBase<State>, State> whenListen(
817
bloc: B,
918
states: Flow<State>,
1019
initialState: State? = null,
1120
) {
12-
every { bloc.stateFlow } answers { states }
21+
every { bloc.stateFlow } answers {
22+
states.onEach {
23+
every { bloc.state } returns it
24+
}
25+
}
1326
if (initialState != null) {
1427
every { bloc.state } returns initialState
1528
}

test/src/test/java/com/ptrbrynt/kotlin_bloc/test/MockBlocTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class MockBlocTest {
3737

3838
whenListen(cubit, flowOf(0, 1, 2), initialState = 1)
3939

40+
assertEquals(1, cubit.state)
41+
4042
testBloc(
4143
build = { cubit },
4244
expected = listOf(
@@ -46,7 +48,7 @@ class MockBlocTest {
4648
),
4749
)
4850

49-
assertEquals(1, cubit.state)
51+
assertEquals(2, cubit.state)
5052
}
5153

5254
@Test
@@ -83,5 +85,7 @@ class MockBlocTest {
8385
{ equals(2) },
8486
),
8587
)
88+
89+
assertEquals(2, bloc.state)
8690
}
8791
}

0 commit comments

Comments
 (0)