Skip to content

Instantly share code, notes, and snippets.

View ferPrieto's full-sized avatar
💪
Attitute beats talent

Fernando Prieto Moyano ferPrieto

💪
Attitute beats talent
View GitHub Profile
// Nested conditions (BAD IDEA)
fun logTeamMembers(teamMembers: List<String>) {
if (teamMembers.isEmpty()) {
Log.d("TAG", "It's empty")
} else if (teamMembers.size == 1) {
if (teamMembers.first() == "foo") {
Log.d("TAG", "The only member is foo")
} else {
Log.d("TAG", "The only member is not foo")
}
@ferPrieto
ferPrieto / FilterIcon.kt
Created January 4, 2022 12:03
This is an example of a Composable function to define an Icon with a Content description
@Composable
private fun FilterIcon(modifier: Modifier, onClick: () -> Unit) {
Box(modifier) {
IconButton(
onClick = onClick
) {
Image(
painter = painterResource(R.drawable.ic_filter),
contentDescription = "Filter Button"
)
@ferPrieto
ferPrieto / MainScreenTest.kt
Created January 4, 2022 12:13
This is an example of how to identify a node by Semantics: ContentDescription
private val dialogFilterButton by lazy {
composeTestRule.onNodeWithContentDescription("Filter Button")
}
@Test
fun elementsVisibilityAfterOpeningTheMainScreen() {
setMainContent()
dialogFilterButton.assertIsDisplayed()
}
android {
defaultConfig {
applicationId = "prieto.fernando.sample"
testInstrumentationRunner = "prieto.fernando.sample.webmock.MockTestRunner"
}
buildFeatures {
compose = true
viewBinding = true
}
// rest of configuration
class MockTestRunner : AndroidJUnitRunner() {
override fun onCreate(arguments: Bundle?) {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitAll().build())
super.onCreate(arguments)
}
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
@ferPrieto
ferPrieto / FakeNetworkModule.kt
Created January 4, 2022 16:50
This is the FakeNetworkModule class used for testing purpose that replaces the original one (NetworkModule) defined in production
@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [NetworkModule::class]
)
class FakeNetworkModule : NetworkModule() {
override fun getBaseUrl() = "http://127.0.0.1:${BuildConfig.PORT}"
}
@ferPrieto
ferPrieto / NetworkModule.kt
Created January 4, 2022 16:53
This is the simplified version of the NetworkModule, which will be replaced by the FakeNetworkModule in the Tests
@InstallIn(SingletonComponent::class)
@Module
open class NetworkModule {
open fun getBaseUrl () ="https://api.spacexdata.com/v3/"
@Provides
@BaseUrl
fun provideBaseUrl() = getBaseUrl ()
@ferPrieto
ferPrieto / BaseTest.kt
Created January 4, 2022 17:11
These are the minimum initialisation needed in order to use MockWebServer in your tests
val mockWebServer by lazy { MockWebServer() }
@Before
fun setUp() {
mockWebServer.start(BuildConfig.PORT)
}
@After
fun teardown() {
mockWebServer.shutdown()
@ferPrieto
ferPrieto / LaunchesScreenKtTest.kt
Last active January 5, 2022 00:18
This is the simplified test using MockWebServer
@ExperimentalMaterialApi
@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class LaunchesScreenKtTest : BaseScreenTest() {
@Test
@InternalCoroutinesApi
fun visibleItemsCountAfterOpeningTheScreen() {
mockWebServer.dispatcher = SuccessDispatcher()
setMainContent()
@ferPrieto
ferPrieto / LaunchesScreenTest.kt
Last active January 5, 2022 00:20
This is a simplified demonstration of a UI Test in Isolation where only one of the tabs is initialised with the desired use case
@Test
@InternalCoroutinesApi
fun elementsVisibilityAfterTwoItemsRetrieved() {
composeTestRule.apply {
setContent {
SpaceXTheme {
LaunchesScreen(
state = LaunchesContract.State(
listOf(
LaunchUiModel(