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

unit-tests #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ dependencies {

// Testing
testImplementation 'junit:junit:4.13.2'
testImplementation "org.robolectric:robolectric:4.10.3"
testImplementation 'com.android.support.test:rules:1.0.2'
testImplementation 'com.android.support.test:runner:1.0.2'
testImplementation "androidx.test:core:1.5.0"
testImplementation "androidx.test:core-ktx:1.5.0"
testImplementation 'org.assertj:assertj-core:3.24.2'
testImplementation "io.mockk:mockk:1.13.7"
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.hifeful.mealmania.presentation

import android.net.Uri
import android.support.test.runner.AndroidJUnit4
import com.hifeful.mealmania.presentation.util.DeeplinkResult
import org.assertj.core.api.Assertions
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4::class)
class MealManiaViewModelTest {
private val viewModel = MealManiaViewModel()

@Test
fun `with mymeals page deeplink - on handleDeeplink - it should return ShowMyMealsPage DeeplinkResult`() {
val uri = Uri.parse("mealmania://mymeals")

val actual = viewModel.handleDeeplink(uri)

Assertions.assertThat(actual).isInstanceOf(DeeplinkResult.ShowMyMealsPage::class.java)
}

@Test
fun `with meals search deeplink - on handleDeeplink - it should return ApplyMealsSearch DeeplinkResult`() {
val uri = Uri.parse("mealmania://search?q=soup")

val actual = viewModel.handleDeeplink(uri)

Assertions.assertThat(actual)
.isEqualTo(DeeplinkResult.ApplyMealsSearch("soup"))
}

@Test
fun `with meals details page deeplink - on handleDeeplink - it should return ShowMealsPage DeeplinkResult`() {
val uri = Uri.parse("mealmania://meal?id=123")

val actual = viewModel.handleDeeplink(uri)

Assertions.assertThat(actual)
.isEqualTo(DeeplinkResult.ShowMealsPage("123"))
}

@Test
fun `with unsupported deeplink - on handleDeeplink - it should return no deeplink`() {
val uri = Uri.parse("mealmania://receipt?id=123&name=potato")

val actual = viewModel.handleDeeplink(uri)

Assertions.assertThat(actual).isNull()
}

@Test
fun `with unknown deeplink - on handleDeeplink - it should return null`() {
val uri = Uri.parse("unknown")

val actual = viewModel.handleDeeplink(uri)

Assertions.assertThat(actual).isNull()
}

@Test
fun `with mymeals page firebase dynamic link - on handleFirebaseDynamicLink - it should return ShowMyMealsPage DeeplinkResult`() {
val uri = Uri.parse("https://mealmania.page.link/mymeals")

val actual = viewModel.handleFirebaseDynamicLink(uri)

Assertions.assertThat(actual).isInstanceOf(DeeplinkResult.ShowMyMealsPage::class.java)
}

@Test
fun `with meals search firebase dynamic link - on handleFirebaseDynamicLink - it should return ApplyMealsSearch DeeplinkResult`() {
val uri = Uri.parse("https://mealmania.link/search?q=soup")

val actual = viewModel.handleFirebaseDynamicLink(uri)

Assertions.assertThat(actual)
.isEqualTo(DeeplinkResult.ApplyMealsSearch("soup"))
}

@Test
fun `with meals details page firebase dynamic link - on handleFirebaseDynamicLink - it should return ShowMealsPage DeeplinkResult`() {
val uri = Uri.parse("https://mealmania.link/meal?id=123")

val actual = viewModel.handleFirebaseDynamicLink(uri)

Assertions.assertThat(actual)
.isEqualTo(DeeplinkResult.ShowMealsPage("123"))
}

@Test
fun `with unsupported firebase dynamic link - on handleFirebaseDynamicLink - it should return null`() {
val uri = Uri.parse("https://mealmania.link/receipt?id=123&name=potato")

val actual = viewModel.handleFirebaseDynamicLink(uri)

Assertions.assertThat(actual).isNull()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.hifeful.mealmania.presentation.details

import org.junit.Test


class MealDetailsReducerTest {

private val reducer = MealDetailsFeature.ReducerImpl()

@Test
fun `Given not favorite meal, When invoke is called, Then the state should be favorite`() {
// Given
val previousState = MealDetailsFeature.State(isFavourite = false)
val effect = MealDetailsFeature.Effect.FavouriteClicked(1)

// When
val actual = reducer.invoke(previousState, effect)

// Then
assert(actual.isFavourite == true)
}

@Test
fun `Given favorite meal, When invoke is called, Then the state should be not favorite`() {
// Given
val previousState = MealDetailsFeature.State(isFavourite = true)
val effect = MealDetailsFeature.Effect.FavouriteClicked(1)

// When
val actual = reducer.invoke(previousState, effect)

// Then
assert(actual.isFavourite == false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.hifeful.mealmania.presentation.details

import android.content.Context
import android.content.res.ColorStateList
import android.support.test.runner.AndroidJUnit4
import androidx.core.content.ContextCompat
import androidx.test.core.app.ApplicationProvider
import io.mockk.mockk
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4::class)
class MealDetailsViewStateTest {

private val context: Context = ApplicationProvider.getApplicationContext()

@Test
fun `Given favorite meal, When getFavouriteIcon is called, Then holo red dark should be returned`() {
// Given
val expectedColorStateList = ColorStateList.valueOf(
ContextCompat.getColor(context, android.R.color.holo_red_dark)
)
val viewState = MealDetailsViewState(
meal = mockk(),
isFavourite = true
)

// When
val actual = viewState.getFavouriteIcon(context)

// Then
assert(actual == expectedColorStateList)
}

@Test
fun `Given not favorite meal, When getFavouriteIcon is called, Then while color should be returned`() {
// Given
val expectedColorStateList = ColorStateList.valueOf(
ContextCompat.getColor(context, android.R.color.white)
)
val viewState = MealDetailsViewState(
meal = mockk(),
)

// When
val actual = viewState.getFavouriteIcon(context)

// Then
assert(actual == expectedColorStateList)
}
}