Add unit test on AnnouncementPresenter

This commit is contained in:
Benoit Marty
2025-10-03 09:40:38 +02:00
parent a4a6cb81ba
commit 6caf4b64db
3 changed files with 79 additions and 18 deletions

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.features.announcement.impl
import com.google.common.truth.Truth.assertThat
import io.element.android.features.announcement.impl.store.AnnouncementStore
import io.element.android.features.announcement.impl.store.InMemoryAnnouncementStore
import io.element.android.tests.testutils.test
import kotlinx.coroutines.test.runTest
import org.junit.Test
class AnnouncementPresenterTest {
@Test
fun `present - initial state`() = runTest {
val presenter = createAnnouncementPresenter()
presenter.test {
val state = awaitItem()
assertThat(state.showSpaceAnnouncement).isFalse()
}
}
@Test
fun `present - showSpaceAnnouncement value depends on the value in the store`() = runTest {
val store = InMemoryAnnouncementStore()
val presenter = createAnnouncementPresenter(
announcementStore = store,
)
presenter.test {
val state = awaitItem()
assertThat(state.showSpaceAnnouncement).isFalse()
store.setSpaceAnnouncementValue(AnnouncementStore.SpaceAnnouncement.Show)
val updatedState = awaitItem()
assertThat(updatedState.showSpaceAnnouncement).isTrue()
store.setSpaceAnnouncementValue(AnnouncementStore.SpaceAnnouncement.Shown)
val finalState = awaitItem()
assertThat(finalState.showSpaceAnnouncement).isFalse()
}
}
}
private fun createAnnouncementPresenter(
announcementStore: AnnouncementStore = InMemoryAnnouncementStore(),
) = AnnouncementPresenter(
announcementStore = announcementStore,
)

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.features.announcement.impl.store
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
class InMemoryAnnouncementStore(
initialSpaceAnnouncement: AnnouncementStore.SpaceAnnouncement = AnnouncementStore.SpaceAnnouncement.NeverShown,
) : AnnouncementStore {
private val spaceAnnouncement = MutableStateFlow(initialSpaceAnnouncement)
override suspend fun setSpaceAnnouncementValue(value: AnnouncementStore.SpaceAnnouncement) {
spaceAnnouncement.value = value
}
override fun spaceAnnouncementFlow(): Flow<AnnouncementStore.SpaceAnnouncement> {
return spaceAnnouncement.asStateFlow()
}
override suspend fun reset() {
spaceAnnouncement.value = AnnouncementStore.SpaceAnnouncement.NeverShown
}
}

View File

@@ -1,18 +0,0 @@
/*
* Copyright 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.features.rageshake.impl
import kotlinx.coroutines.test.runTest
import org.junit.Test
class AnnouncementPresenterTest {
@Test
fun `present - initial test`() = runTest {
// TODO
}
}