Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for dialog fragment #60

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.rubensousa.carioca.hilt

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
Expand Down Expand Up @@ -48,6 +53,21 @@ class HiltFragmentScenarioTest {
assertThat(testDependency).isSameInstanceAs(providedDependency)
}

@Test
fun testViewModelOfDialogFragmentReceivesInjectedDependency() {
// given
val scenario = launchHiltFragment<TestDialogFragment>()

// when
var testDependency: TestDependency? = null
scenario.onFragment { fragment ->
testDependency = fragment.getTestDependency()
}

// then
assertThat(testDependency).isSameInstanceAs(providedDependency)
}

@Test
fun testFragmentMovesToDifferentLifecycleStates() {
// given
Expand All @@ -74,7 +94,7 @@ class HiltFragmentScenarioTest {
fun testActivityRemovesFragmentWhenItGetsDestroyed() {
// given
val scenario = launchHiltFragment<TestFragment>()
val activity = scenario.withFragment { requireActivity() }
val activity = scenario.withFragment { requireActivity() }

// when
scenario.moveToState(Lifecycle.State.DESTROYED)
Expand Down Expand Up @@ -141,6 +161,29 @@ class HiltFragmentScenarioTest {
return currentState
}

@AndroidEntryPoint
class TestDialogFragment : DialogFragment() {

private val viewModel by viewModels<TestViewModel>()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
return View(inflater.context).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
setBackgroundColor(Color.YELLOW)
}
}

fun getTestDependency() = viewModel.getTestDependency()

}

@AndroidEntryPoint
class TestFragment : Fragment() {

Expand Down
Loading