forked from thomaskioko/tv-maniac
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6841784
commit 750895e
Showing
5 changed files
with
175 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
...ommonTest/kotlin/com/thomaskioko/tvmaniac/presentation/profile/ProfileStateMachineTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.thomaskioko.tvmaniac.presentation.profile | ||
|
||
import app.cash.turbine.test | ||
import com.thomaskioko.tvmaniac.datastore.testing.FakeDatastoreRepository | ||
import com.thomaskioko.tvmaniac.datastore.testing.authenticatedAuthState | ||
import com.thomaskioko.tvmaniac.trakt.profile.testing.FakeProfileRepository | ||
import com.thomaskioko.tvmaniac.trakt.profile.testing.user | ||
import com.thomaskioko.tvmaniac.traktauth.api.TraktAuthState | ||
import com.thomaskioko.tvmaniac.traktauth.testing.FakeTraktAuthRepository | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.test.runTest | ||
import org.mobilenativefoundation.store.store5.StoreReadResponse | ||
import org.mobilenativefoundation.store.store5.StoreReadResponseOrigin | ||
import kotlin.test.Test | ||
|
||
class ProfileStateMachineTest { | ||
|
||
private val datastoreRepository = FakeDatastoreRepository() | ||
private val profileRepository = FakeProfileRepository() | ||
private val traktAuthRepository = FakeTraktAuthRepository() | ||
|
||
private val stateMachine = ProfileStateMachine( | ||
datastoreRepository = datastoreRepository, | ||
profileRepository = profileRepository, | ||
traktAuthRepository = traktAuthRepository, | ||
) | ||
|
||
@Test | ||
fun initial_state_emits_expected_result() = runTest { | ||
stateMachine.state.test { | ||
awaitItem() shouldBe LoggedOutContent() | ||
} | ||
} | ||
|
||
@Test | ||
fun given_ShowTraktDialog_andUserIsAuthenticated_expectedResultIsEmitted() = runTest { | ||
stateMachine.state.test { | ||
awaitItem() shouldBe LoggedOutContent.DEFAULT_STATE // Initial State | ||
|
||
stateMachine.dispatch(ShowTraktDialog) | ||
|
||
awaitItem() shouldBe LoggedOutContent.DEFAULT_STATE | ||
.copy( | ||
showTraktDialog = true, | ||
) | ||
|
||
stateMachine.dispatch(TraktLoginClicked) | ||
|
||
awaitItem() shouldBe LoggedOutContent.DEFAULT_STATE | ||
.copy( | ||
showTraktDialog = false, | ||
) | ||
|
||
traktAuthRepository.setAuthState(TraktAuthState.LOGGED_IN) | ||
datastoreRepository.setAuthState(authenticatedAuthState) | ||
profileRepository.setUserData( | ||
StoreReadResponse.Data( | ||
value = user, | ||
origin = StoreReadResponseOrigin.Cache, | ||
), | ||
) | ||
|
||
awaitItem() shouldBe LoggedInContent() | ||
awaitItem() shouldBe LoggedInContent() | ||
.copy( | ||
errorMessage = null, | ||
userInfo = UserInfo( | ||
slug = user.slug, | ||
userName = user.user_name, | ||
fullName = user.full_name, | ||
userPicUrl = user.profile_picture, | ||
), | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun given_TraktLoginClicked_andErrorOccurs_expectedResultIsEmitted() = runTest { | ||
stateMachine.state.test { | ||
val errorMessage = "Something happened" | ||
|
||
awaitItem() shouldBe LoggedOutContent.DEFAULT_STATE | ||
|
||
stateMachine.dispatch(ShowTraktDialog) | ||
|
||
awaitItem() shouldBe LoggedOutContent.DEFAULT_STATE.copy( | ||
showTraktDialog = true, | ||
) | ||
|
||
stateMachine.dispatch(TraktLoginClicked) | ||
|
||
awaitItem() shouldBe LoggedOutContent.DEFAULT_STATE.copy( | ||
showTraktDialog = false, | ||
) | ||
|
||
traktAuthRepository.setAuthState(TraktAuthState.LOGGED_IN) | ||
datastoreRepository.setAuthState(authenticatedAuthState) | ||
profileRepository.setUserData( | ||
StoreReadResponse.Error.Exception( | ||
error = Throwable(errorMessage), | ||
origin = StoreReadResponseOrigin.Cache, | ||
), | ||
) | ||
|
||
awaitItem() shouldBe LoggedInContent() | ||
awaitItem() shouldBe LoggedInContent() | ||
.copy( | ||
errorMessage = errorMessage, | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun given_TraktLogoutClicked_expectedResultIsEmitted() = runTest { | ||
stateMachine.state.test { | ||
awaitItem() shouldBe LoggedOutContent.DEFAULT_STATE | ||
|
||
traktAuthRepository.setAuthState(TraktAuthState.LOGGED_IN) | ||
datastoreRepository.setAuthState(authenticatedAuthState) | ||
profileRepository.setUserData( | ||
StoreReadResponse.Data( | ||
value = user, | ||
origin = StoreReadResponseOrigin.Cache, | ||
), | ||
) | ||
|
||
awaitItem() shouldBe LoggedInContent() | ||
awaitItem() shouldBe LoggedInContent() | ||
.copy( | ||
errorMessage = null, | ||
userInfo = UserInfo( | ||
slug = user.slug, | ||
userName = user.user_name, | ||
fullName = user.full_name, | ||
userPicUrl = user.profile_picture, | ||
), | ||
) | ||
|
||
stateMachine.dispatch(TraktLogoutClicked) | ||
|
||
awaitItem() shouldBe LoggedOutContent.DEFAULT_STATE | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters