mirror of
https://github.com/bitwarden/android.git
synced 2025-03-15 18:58:59 +03:00
Inject clock into ViewModels (#1112)
This commit is contained in:
parent
932bc3957f
commit
9736aacd53
4 changed files with 24 additions and 36 deletions
|
@ -19,8 +19,8 @@ import kotlinx.coroutines.flow.onEach
|
|||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import java.time.Clock
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.TimeZone
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val KEY_STATE = "state"
|
||||
|
@ -30,6 +30,7 @@ private const val KEY_STATE = "state"
|
|||
*/
|
||||
@HiltViewModel
|
||||
class LoginApprovalViewModel @Inject constructor(
|
||||
private val clock: Clock,
|
||||
private val authRepository: AuthRepository,
|
||||
private val specialCircumstanceManager: SpecialCircumstanceManager,
|
||||
savedStateHandle: SavedStateHandle,
|
||||
|
@ -54,7 +55,7 @@ class LoginApprovalViewModel @Inject constructor(
|
|||
private val dateTimeFormatter
|
||||
get() = DateTimeFormatter
|
||||
.ofPattern("M/d/yy hh:mm a")
|
||||
.withZone(TimeZone.getDefault().toZoneId())
|
||||
.withZone(clock.zone)
|
||||
|
||||
init {
|
||||
state
|
||||
|
|
|
@ -18,8 +18,8 @@ import kotlinx.coroutines.flow.onEach
|
|||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import java.time.Clock
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.TimeZone
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val KEY_STATE = "state"
|
||||
|
@ -29,6 +29,7 @@ private const val KEY_STATE = "state"
|
|||
*/
|
||||
@HiltViewModel
|
||||
class PendingRequestsViewModel @Inject constructor(
|
||||
private val clock: Clock,
|
||||
private val authRepository: AuthRepository,
|
||||
private val settingsRepository: SettingsRepository,
|
||||
savedStateHandle: SavedStateHandle,
|
||||
|
@ -44,7 +45,7 @@ class PendingRequestsViewModel @Inject constructor(
|
|||
private val dateTimeFormatter
|
||||
get() = DateTimeFormatter
|
||||
.ofPattern("M/d/yy hh:mm a")
|
||||
.withZone(TimeZone.getDefault().toZoneId())
|
||||
.withZone(clock.zone)
|
||||
|
||||
init {
|
||||
updateAuthRequestList()
|
||||
|
|
|
@ -21,15 +21,19 @@ import io.mockk.every
|
|||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.Clock
|
||||
import java.time.Instant
|
||||
import java.time.ZoneOffset
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.TimeZone
|
||||
|
||||
class LoginApprovalViewModelTest : BaseViewModelTest() {
|
||||
|
||||
private val fixedClock: Clock = Clock.fixed(
|
||||
Instant.parse("2023-10-27T12:00:00Z"),
|
||||
ZoneOffset.UTC,
|
||||
)
|
||||
private val mockSpecialCircumstanceManager: SpecialCircumstanceManager = mockk {
|
||||
every { specialCircumstance } returns null
|
||||
}
|
||||
|
@ -43,18 +47,6 @@ class LoginApprovalViewModelTest : BaseViewModelTest() {
|
|||
every { userStateFlow } returns mutableUserStateFlow
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
// Setting the timezone so the tests pass consistently no matter the environment.
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun tearDown() {
|
||||
// Clearing the timezone after the test.
|
||||
TimeZone.setDefault(null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `init should call getAuthRequestById when special circumstance is absent`() {
|
||||
createViewModel(state = null)
|
||||
|
@ -252,6 +244,7 @@ class LoginApprovalViewModelTest : BaseViewModelTest() {
|
|||
specialCircumstanceManager: SpecialCircumstanceManager = mockSpecialCircumstanceManager,
|
||||
state: LoginApprovalState? = DEFAULT_STATE,
|
||||
): LoginApprovalViewModel = LoginApprovalViewModel(
|
||||
clock = fixedClock,
|
||||
authRepository = authRepository,
|
||||
specialCircumstanceManager = specialCircumstanceManager,
|
||||
savedStateHandle = SavedStateHandle()
|
||||
|
|
|
@ -16,16 +16,20 @@ import io.mockk.every
|
|||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.Clock
|
||||
import java.time.Instant
|
||||
import java.time.ZoneOffset
|
||||
import java.time.ZonedDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.TimeZone
|
||||
|
||||
class PendingRequestsViewModelTest : BaseViewModelTest() {
|
||||
|
||||
private val fixedClock: Clock = Clock.fixed(
|
||||
Instant.parse("2023-10-27T12:00:00Z"),
|
||||
ZoneOffset.UTC,
|
||||
)
|
||||
private val mutableAuthRequestsWithUpdatesFlow =
|
||||
bufferedMutableSharedFlow<AuthRequestsUpdatesResult>()
|
||||
private val authRepository = mockk<AuthRepository> {
|
||||
|
@ -37,18 +41,6 @@ class PendingRequestsViewModelTest : BaseViewModelTest() {
|
|||
every { getPullToRefreshEnabledFlow() } returns mutablePullToRefreshStateFlow
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
// Setting the timezone so the tests pass consistently no matter the environment.
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun tearDown() {
|
||||
// Clearing the timezone after the test.
|
||||
TimeZone.setDefault(null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `init should call getAuthRequestsWithUpdates`() {
|
||||
createViewModel(state = null)
|
||||
|
@ -65,7 +57,7 @@ class PendingRequestsViewModelTest : BaseViewModelTest() {
|
|||
fun `getPendingResults success with content should update state with some requests filtered`() {
|
||||
val dateTimeFormatter = DateTimeFormatter
|
||||
.ofPattern("M/d/yy hh:mm a")
|
||||
.withZone(TimeZone.getDefault().toZoneId())
|
||||
.withZone(fixedClock.zone)
|
||||
val nowZonedDateTime = ZonedDateTime.now()
|
||||
val requestList = listOf(
|
||||
AuthRequest(
|
||||
|
@ -288,7 +280,7 @@ class PendingRequestsViewModelTest : BaseViewModelTest() {
|
|||
fun `on LifecycleResume should update state`() = runTest {
|
||||
val dateTimeFormatter = DateTimeFormatter
|
||||
.ofPattern("M/d/yy hh:mm a")
|
||||
.withZone(TimeZone.getDefault().toZoneId())
|
||||
.withZone(fixedClock.zone)
|
||||
val nowZonedDateTime = ZonedDateTime.now()
|
||||
val fiveMinZonedDateTime = ZonedDateTime.now().minusMinutes(5)
|
||||
val sixMinZonedDateTime = ZonedDateTime.now().minusMinutes(6)
|
||||
|
@ -373,6 +365,7 @@ class PendingRequestsViewModelTest : BaseViewModelTest() {
|
|||
private fun createViewModel(
|
||||
state: PendingRequestsState? = DEFAULT_STATE,
|
||||
): PendingRequestsViewModel = PendingRequestsViewModel(
|
||||
clock = fixedClock,
|
||||
authRepository = authRepository,
|
||||
settingsRepository = settingsRepository,
|
||||
savedStateHandle = SavedStateHandle().apply { set("state", state) },
|
||||
|
|
Loading…
Add table
Reference in a new issue