ContactsViewModelTest.kt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Nextcloud Talk - Android Client
  3. *
  4. * SPDX-FileCopyrightText: 2024 Sowjanya Kota <sowjanya.kch@gmail.com>
  5. * SPDX-License-Identifier: GPL-3.0-or-later
  6. */
  7. package com.nextcloud.talk.contacts
  8. import com.nextcloud.talk.contacts.apiService.FakeItem
  9. import com.nextcloud.talk.contacts.repository.FakeRepositoryError
  10. import com.nextcloud.talk.contacts.repository.FakeRepositorySuccess
  11. import kotlinx.coroutines.Dispatchers
  12. import kotlinx.coroutines.ExperimentalCoroutinesApi
  13. import kotlinx.coroutines.test.TestDispatcher
  14. import kotlinx.coroutines.test.UnconfinedTestDispatcher
  15. import kotlinx.coroutines.test.resetMain
  16. import kotlinx.coroutines.test.runTest
  17. import kotlinx.coroutines.test.setMain
  18. import org.junit.After
  19. import org.junit.Before
  20. import org.junit.Test
  21. @OptIn(ExperimentalCoroutinesApi::class)
  22. class ContactsViewModelTest {
  23. private lateinit var viewModel: ContactsViewModel
  24. private val repository: ContactsRepository = FakeRepositorySuccess()
  25. val dispatcher: TestDispatcher = UnconfinedTestDispatcher()
  26. @Before
  27. fun setup() {
  28. Dispatchers.setMain(dispatcher)
  29. }
  30. @After
  31. fun tearDown() {
  32. Dispatchers.resetMain()
  33. }
  34. @Before
  35. fun setUp() {
  36. viewModel = ContactsViewModel(repository)
  37. }
  38. @Test
  39. fun `fetch contacts`() =
  40. runTest {
  41. viewModel = ContactsViewModel(repository)
  42. viewModel.getContactsFromSearchParams()
  43. assert(viewModel.contactsViewState.value is ContactsUiState.Success)
  44. val successState = viewModel.contactsViewState.value as ContactsUiState.Success
  45. assert(successState.contacts == FakeItem.contacts)
  46. }
  47. @Test
  48. fun `test error contacts state`() =
  49. runTest {
  50. viewModel = ContactsViewModel(FakeRepositoryError())
  51. assert(viewModel.contactsViewState.value is ContactsUiState.Error)
  52. val errorState = viewModel.contactsViewState.value as ContactsUiState.Error
  53. assert(errorState.message == "unable to fetch contacts")
  54. }
  55. @Test
  56. fun `update search query`() {
  57. viewModel.updateSearchQuery("Ma")
  58. assert(viewModel.searchQuery.value == "Ma")
  59. }
  60. @Test
  61. fun `initial search query is empty string`() {
  62. viewModel.updateSearchQuery("")
  63. assert(viewModel.searchQuery.value == "")
  64. }
  65. @Test
  66. fun `initial shareType is User`() {
  67. assert(viewModel.shareTypeList.contains(ShareType.User.shareType))
  68. }
  69. @Test
  70. fun `update shareTypes`() {
  71. viewModel.updateShareTypes(listOf(ShareType.Group.shareType))
  72. assert(viewModel.shareTypeList.contains(ShareType.Group.shareType))
  73. }
  74. @Test
  75. fun `initial room state is none`() =
  76. runTest {
  77. assert(viewModel.roomViewState.value is RoomUiState.None)
  78. }
  79. @Test
  80. fun `test success room state`() =
  81. runTest {
  82. viewModel.createRoom("1", "users", "s@gmail.com", null)
  83. assert(viewModel.roomViewState.value is RoomUiState.Success)
  84. val successState = viewModel.roomViewState.value as RoomUiState.Success
  85. assert(successState.conversation == FakeItem.roomOverall.ocs!!.data)
  86. }
  87. @Test
  88. fun `test failure room state`() =
  89. runTest {
  90. viewModel = ContactsViewModel(FakeRepositoryError())
  91. viewModel.createRoom("1", "users", "s@gmail.com", null)
  92. assert(viewModel.roomViewState.value is RoomUiState.Error)
  93. val errorState = viewModel.roomViewState.value as RoomUiState.Error
  94. assert(errorState.message == "unable to create room")
  95. }
  96. @Test
  97. fun `test image uri`() {
  98. val expectedImageUri = "https://mydomain.com/index.php/avatar/vidya/512"
  99. val imageUri = viewModel.getImageUri("vidya", false)
  100. assert(imageUri == expectedImageUri)
  101. }
  102. @Test
  103. fun `test error image uri`() {
  104. val expectedImageUri = "https://mydoman.com/index.php/avatar/vidya/512"
  105. val imageUri = viewModel.getImageUri("vidya", false)
  106. assert(imageUri != expectedImageUri)
  107. }
  108. }