|
| 1 | +package social.androiddev.common.repository.timeline |
| 2 | + |
| 3 | +import kotlinx.coroutines.flow.Flow |
| 4 | +import kotlinx.coroutines.flow.first |
| 5 | +import kotlinx.coroutines.flow.flow |
| 6 | +import kotlinx.coroutines.test.TestResult |
| 7 | +import kotlinx.coroutines.test.runTest |
| 8 | +import org.mobilenativefoundation.store.store5.ExperimentalStoreApi |
| 9 | +import org.mobilenativefoundation.store.store5.ResponseOrigin |
| 10 | +import org.mobilenativefoundation.store.store5.Store |
| 11 | +import org.mobilenativefoundation.store.store5.StoreRequest |
| 12 | +import org.mobilenativefoundation.store.store5.StoreResponse |
| 13 | +import social.androiddev.common.repository.timeline.fixtures.failureResponse |
| 14 | +import social.androiddev.common.repository.timeline.fixtures.fakeLocalStatus |
| 15 | +import social.androiddev.domain.timeline.FeedType |
| 16 | +import social.androiddev.domain.timeline.model.StatusLocal |
| 17 | +import kotlin.test.Test |
| 18 | +import kotlin.test.assertTrue |
| 19 | +import kotlin.test.fail |
| 20 | + |
| 21 | + |
| 22 | +class RealHomeTimelineRepositoryTest{ |
| 23 | + @Test fun sucessTest(): TestResult { |
| 24 | + return runTest{ |
| 25 | + val testRepo = RealHomeTimelineRepository(fakeSuccessStore) |
| 26 | + val result = testRepo.read(FeedType.Home).first() |
| 27 | + assertTrue { result is StoreResponse.Data } |
| 28 | + assertTrue { result.requireData().first() == fakeLocalStatus } |
| 29 | + |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @Test fun failureTest(): TestResult { |
| 34 | + return runTest{ |
| 35 | + val testRepo = RealHomeTimelineRepository(fakeFailureStore) |
| 36 | + val result = testRepo.read(FeedType.Home).first() |
| 37 | + assertTrue { result is StoreResponse.Error.Message } |
| 38 | + assertTrue { result.errorMessageOrNull() == failureResponse.message} |
| 39 | + |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | +val fakeSuccessStore = object : Store<FeedType, List<StatusLocal>> { |
| 44 | + override suspend fun clear(key: FeedType) { |
| 45 | + TODO("Not yet implemented") |
| 46 | + } |
| 47 | + |
| 48 | + @ExperimentalStoreApi |
| 49 | + override suspend fun clearAll() { |
| 50 | + TODO("Not yet implemented") |
| 51 | + } |
| 52 | + |
| 53 | + override fun stream(request: StoreRequest<FeedType>): Flow<StoreResponse<List<StatusLocal>>> { |
| 54 | + return when (request.key.type) { |
| 55 | + FeedType.Home.type -> { |
| 56 | + flow { |
| 57 | + emit( |
| 58 | + StoreResponse.Data( |
| 59 | + listOf(fakeLocalStatus), |
| 60 | + ResponseOrigin.Cache |
| 61 | + ) |
| 62 | + ) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + else -> { |
| 67 | + fail("wrong response") |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +val fakeFailureStore = object : Store<FeedType, List<StatusLocal>> { |
| 74 | + override suspend fun clear(key: FeedType) { |
| 75 | + TODO("Not yet implemented") |
| 76 | + } |
| 77 | + |
| 78 | + @ExperimentalStoreApi |
| 79 | + override suspend fun clearAll() { |
| 80 | + TODO("Not yet implemented") |
| 81 | + } |
| 82 | + |
| 83 | + override fun stream(request: StoreRequest<FeedType>): Flow<StoreResponse<List<StatusLocal>>> { |
| 84 | + return when (request.key.type) { |
| 85 | + FeedType.Home.type -> { |
| 86 | + flow { |
| 87 | + emit(failureResponse) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + else -> { |
| 92 | + fail("wrong response") |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments