From 9d851a7a4b423a7bec5c0c321883a276108b3de7 Mon Sep 17 00:00:00 2001 From: Bliss Pisit Wetcha Date: Fri, 18 Nov 2022 13:06:39 +0700 Subject: [PATCH] [#18] Add SurveyUseCase test --- .../domain/usecase/SurveyListUseCaseTest.kt | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/SurveyListUseCaseTest.kt diff --git a/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/SurveyListUseCaseTest.kt b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/SurveyListUseCaseTest.kt new file mode 100644 index 00000000..6ff3d593 --- /dev/null +++ b/shared/src/commonTest/kotlin/co/nimblehq/blisskmmic/domain/usecase/SurveyListUseCaseTest.kt @@ -0,0 +1,61 @@ +package co.nimblehq.blisskmmic.domain.usecase + +import co.nimblehq.blisskmmic.domain.model.PaginationMeta +import co.nimblehq.blisskmmic.domain.model.Survey +import co.nimblehq.blisskmmic.domain.model.fakePaginationMeta +import co.nimblehq.blisskmmic.domain.model.fakeSurvey +import co.nimblehq.blisskmmic.domain.repository.MockSurveyRepository +import co.nimblehq.blisskmmic.domain.repository.SurveyRepository +import io.kotest.matchers.shouldBe +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.test.runTest +import org.kodein.mock.Mocker +import org.kodein.mock.UsesFakes +import org.kodein.mock.UsesMocks +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.fail + +@UsesMocks(SurveyRepository::class) +@UsesFakes(Survey::class, PaginationMeta::class) +class SurveyListUseCaseTest { + + private val mocker = Mocker() + private val surveyRepository = MockSurveyRepository(mocker) + private val survey = fakeSurvey() + private val paginationMeta = fakePaginationMeta() + private val surveyListUseCase = SurveyListUseCaseImpl(surveyRepository) + + @BeforeTest + fun setUp() { + mocker.reset() + } + + @Test + fun `When calling surveyList with a success response, it returns correct object`() = runTest { + mocker.every { + surveyRepository.survey(1) + } returns flow { emit(Pair(listOf(survey), paginationMeta)) } + + surveyListUseCase(1) + .collect{ + it.first().title shouldBe survey.title + } + } + + @Test + fun `When calling surveyList with a failure response, it returns correct error`() = runTest { + mocker.every { + surveyRepository.survey(1) + } returns flow { error("Fail") } + + surveyListUseCase(1) + .catch { + it.message shouldBe "Fail" + } + .collect{ + fail("Should not receive object") + } + } +}