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 for GetCurrentCart and GetCategoriesList usecases #195 and #197 #212

Merged
merged 3 commits into from
Oct 7, 2023
Merged
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
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.hieuwu.groceriesstore.domain.usecases.impl

import com.hieuwu.groceriesstore.data.repository.CategoryRepository
import com.hieuwu.groceriesstore.domain.models.CategoryModel
import com.hieuwu.groceriesstore.domain.usecases.GetCategoriesListUseCase
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.whenever

@RunWith(MockitoJUnitRunner::class)

class GetCategoriesListUseCaseImplTest {

@Mock
lateinit var mockedCategoryRepository: CategoryRepository

private lateinit var testee: GetCategoriesListUseCaseImpl

@Before
fun setUp() {
testee = GetCategoriesListUseCaseImpl(
categoryRepository = mockedCategoryRepository
)
}

@Test
fun givenCategoriesAvailable_whenExecute_thenReturnCorrectValue() {
val mockCategories = listOf(
CategoryModel(id = "1", name = "Category 1", image = "image1.jpg"),
CategoryModel(id = "2", name = "Category 2", image = "image2.jpg"),
CategoryModel(id = "3", name = "Category 3", image = "image3.jpg"),
CategoryModel(id = "4", name = "Category 4", image = "image4.jpg")
)
whenever(mockedCategoryRepository.getFromLocal()).thenReturn(flow {
emit(mockCategories)
})
runBlocking {
val result = testee.execute(GetCategoriesListUseCase.Input())

result.result.collect { categories ->
assertEquals(mockCategories[0], categories[0])
assertEquals(mockCategories[1], categories[1])
assertEquals(mockCategories[2], categories[2])
assertEquals(mockCategories[1].name, "Category 2")
assertEquals(mockCategories[3].image, "image4.jpg")
}
}
}

@Test
fun givenCategoriesUnavailable_whenExecute_thenReturnCorrectValue() {
whenever(mockedCategoryRepository.getFromLocal()).thenReturn(flow {
emit(listOf())
})
runBlocking {
val result = testee.execute(GetCategoriesListUseCase.Input())
result.result.collect {
assertEquals(it.isEmpty(), true)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.hieuwu.groceriesstore.domain.usecases.impl

import com.hieuwu.groceriesstore.data.repository.OrderRepository
import com.hieuwu.groceriesstore.domain.models.OrderModel
import com.hieuwu.groceriesstore.domain.usecases.GetCurrentCartUseCase
import com.hieuwu.groceriesstore.utilities.OrderStatus
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNull
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.mockito.kotlin.whenever

@RunWith(MockitoJUnitRunner::class)

class GetCurrentCartUseCaseImplTest {

@Mock
lateinit var mockedOrderRepository: OrderRepository

private lateinit var testee: GetCurrentCartUseCaseImpl

@Before
fun setUp() {
testee = GetCurrentCartUseCaseImpl(
orderRepository = mockedOrderRepository
)
}

@Test
fun givenCartNotEmpty_whenExecute_thenReturnOrdersInCart() {
val mockOrder = OrderModel(
id = "",
status = OrderStatus.IN_CART.value,
address = null,
lineItemList = mutableListOf(),
createdAt = ""
)
whenever(mockedOrderRepository.getOneOrderByStatus(OrderStatus.IN_CART)).thenReturn(flow {
emit(mockOrder)
})
runBlocking {
val result = testee.execute(GetCurrentCartUseCase.Input())

result.result.collect { order ->
assertEquals(order?.id, "")
assertEquals(order?.status, "cart")
assertEquals(order?.address, null)
assertEquals(order?.lineItemList!!.isEmpty(), true)
assertEquals(order.createdAt, "")
}
}
}

@Test
fun givenCartEmpty_whenExecute_thenReturnNull() {
whenever(mockedOrderRepository.getOneOrderByStatus(OrderStatus.IN_CART)).thenReturn(flow {
null
})
runBlocking {
val result = testee.execute(GetCurrentCartUseCase.Input())
result.result.collect {
assertNull(it)
}
}
}
}
Loading