NotificationsViewModelTestFilter.kt 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright 2023 Tusky Contributors
  3. *
  4. * This file is a part of Tusky.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under the terms of the
  7. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  11. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. * Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  15. * see <http://www.gnu.org/licenses>.
  16. */
  17. package com.keylesspalace.tusky.components.notifications
  18. import app.cash.turbine.test
  19. import com.google.common.truth.Truth.assertThat
  20. import com.keylesspalace.tusky.db.AccountEntity
  21. import com.keylesspalace.tusky.entity.Notification
  22. import kotlinx.coroutines.ExperimentalCoroutinesApi
  23. import kotlinx.coroutines.test.runTest
  24. import org.junit.Test
  25. import org.mockito.kotlin.argumentCaptor
  26. import org.mockito.kotlin.verify
  27. /**
  28. * Verify that [ApplyFilter] is handled correctly on receipt:
  29. *
  30. * - Is the [UiState] updated correctly?
  31. * - Are the correct [AccountManager] functions called, with the correct arguments?
  32. */
  33. @OptIn(ExperimentalCoroutinesApi::class)
  34. class NotificationsViewModelTestFilter : NotificationsViewModelTestBase() {
  35. @Test
  36. fun `should load initial filter from active account`() = runTest {
  37. viewModel.uiState.test {
  38. assertThat(awaitItem().activeFilter)
  39. .containsExactlyElementsIn(setOf(Notification.Type.FOLLOW))
  40. }
  41. }
  42. @Test
  43. fun `should save filter to active account && update state`() = runTest {
  44. viewModel.uiState.test {
  45. // When
  46. viewModel.accept(InfallibleUiAction.ApplyFilter(setOf(Notification.Type.REBLOG)))
  47. // Then
  48. // - filter saved to active account
  49. argumentCaptor<AccountEntity>().apply {
  50. verify(accountManager).saveAccount(capture())
  51. assertThat(this.lastValue.notificationsFilter)
  52. .isEqualTo("[\"reblog\"]")
  53. }
  54. // - filter updated in uiState
  55. assertThat(expectMostRecentItem().activeFilter)
  56. .containsExactlyElementsIn(setOf(Notification.Type.REBLOG))
  57. }
  58. }
  59. }