-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for LifeCycleTest:hookSnackBar (#2754)
* Add LifeCycleExtension:HookSnackBar test * Add LifeCycleExtension:HookSnackBar test * Test HookSnackBar * Check whether showSnackBar is executed Co-authored-by: Maryann Mweru <[email protected]> --------- Co-authored-by: Hilary Baraka Egesa <[email protected]>
- Loading branch information
1 parent
80c606b
commit fbfa0a4
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
.../src/test/java/org/smartregister/fhircore/quest/util/extensions/LifeCycleExtensionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2021-2023 Ona Systems, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.smartregister.fhircore.quest.util.extensions | ||
|
||
import android.content.Context | ||
import androidx.compose.material.ScaffoldState | ||
import androidx.compose.material.SnackbarDuration | ||
import androidx.navigation.NavController | ||
import io.mockk.coEvery | ||
import io.mockk.coVerify | ||
import io.mockk.every | ||
import io.mockk.just | ||
import io.mockk.mockk | ||
import io.mockk.runs | ||
import kotlin.test.assertNotNull | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.smartregister.fhircore.engine.domain.model.ActionConfig | ||
import org.smartregister.fhircore.engine.domain.model.ResourceData | ||
import org.smartregister.fhircore.engine.domain.model.SnackBarMessageConfig | ||
import org.smartregister.fhircore.quest.robolectric.RobolectricTest | ||
|
||
class LifeCycleExtensionTest : RobolectricTest() { | ||
private val navController = mockk<NavController>(relaxUnitFun = true) | ||
private val context = mockk<Context>(relaxUnitFun = true, relaxed = true) | ||
|
||
@Before | ||
fun setUp() { | ||
every { navController.context } returns context | ||
} | ||
|
||
@Test | ||
fun testHookSnackBarShowsSnackBar() = runTest { | ||
val messageConfig = mockk<SnackBarMessageConfig>() | ||
every { messageConfig.message } returns "This is a message" | ||
|
||
val mockedSharedFlow = mockk<SharedFlow<SnackBarMessageConfig>>() | ||
|
||
val scaffoldState = mockk<ScaffoldState>() | ||
val resourceData = mockk<ResourceData>() | ||
val listActionConfig = mockk<List<ActionConfig>>() | ||
every { scaffoldState.snackbarHostState } returns mockk() | ||
coEvery { | ||
scaffoldState.snackbarHostState.showSnackbar("message", "action", SnackbarDuration.Long) | ||
} returns mockk() | ||
val snackBarResult = runBlocking { | ||
scaffoldState.snackbarHostState.showSnackbar("message", "action", SnackbarDuration.Long) | ||
} | ||
|
||
assertNotNull(snackBarResult) | ||
every { listActionConfig.handleClickEvent(navController, resourceData) } just runs | ||
coEvery { | ||
mockedSharedFlow.hookSnackBar(scaffoldState, resourceData = resourceData, navController) | ||
} just runs | ||
coVerify { scaffoldState.snackbarHostState.showSnackbar(any(), any(), any()) } | ||
} | ||
} |