-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
125 additions
and
34 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -53,7 +53,7 @@ class FragmentLifecycleTest { | |
} | ||
} | ||
bind(featureFactory) | ||
} | ||
}, | ||
) | ||
} | ||
} | ||
|
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
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
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
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
79 changes: 79 additions & 0 deletions
79
...id/src/test/java/com/instacart/formula/android/internal/FormulaFragmentViewFactoryTest.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,79 @@ | ||
package com.instacart.formula.android.internal | ||
|
||
import com.google.common.truth.Truth | ||
import com.instacart.formula.android.Feature | ||
import com.instacart.formula.android.FeatureEvent | ||
import com.instacart.formula.android.FragmentEnvironment | ||
import com.instacart.formula.android.FragmentId | ||
import com.instacart.formula.android.ViewFactory | ||
import io.reactivex.rxjava3.core.Observable | ||
import org.junit.Test | ||
import java.lang.RuntimeException | ||
|
||
class FormulaFragmentViewFactoryTest { | ||
|
||
@Test fun `throws an exception if feature provider returns null`() { | ||
val viewFactory = viewFactory { null } | ||
val result = runCatching { viewFactory.viewFactory() } | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Could not find feature for " | ||
) | ||
} | ||
|
||
@Test fun `throws an exception if feature is not registered`() { | ||
val viewFactory = viewFactory { | ||
FeatureEvent.MissingBinding(it) | ||
} | ||
val result = runCatching { viewFactory.viewFactory() } | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Missing feature factory or integration for" | ||
) | ||
} | ||
|
||
@Test fun `throws an exception if feature failed to initialize`() { | ||
val viewFactory = viewFactory { | ||
FeatureEvent.Failure(it, RuntimeException("Something went wrong")) | ||
} | ||
val result = runCatching { viewFactory.viewFactory() } | ||
Truth.assertThat(result.exceptionOrNull()).hasMessageThat().contains( | ||
"Feature failed to initialize:" | ||
) | ||
} | ||
|
||
@Test fun `only initializes the view factory once`() { | ||
var timesCalled = 0 | ||
val viewFactory = viewFactory { | ||
timesCalled += 1 | ||
val feature = Feature( | ||
state = Observable.empty(), | ||
viewFactory = ViewFactory { _, _ -> | ||
error("should not be called") | ||
} | ||
) | ||
FeatureEvent.Init(it, feature) | ||
} | ||
|
||
viewFactory.viewFactory() | ||
viewFactory.viewFactory() | ||
viewFactory.viewFactory() | ||
viewFactory.viewFactory() | ||
Truth.assertThat(timesCalled).isEqualTo(1) | ||
} | ||
|
||
private fun viewFactory( | ||
delegateGetFeature: (FragmentId) -> FeatureEvent?, | ||
): FormulaFragmentViewFactory { | ||
return FormulaFragmentViewFactory( | ||
environment = FragmentEnvironment(), | ||
fragmentId = FragmentId( | ||
instanceId = "instanceId", | ||
key = EmptyFragmentKey(tag = "tag") | ||
), | ||
featureProvider = object : FeatureProvider { | ||
override fun getFeature(id: FragmentId): FeatureEvent? { | ||
return delegateGetFeature(id) | ||
} | ||
} | ||
) | ||
} | ||
} |