-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Add unit tests for FindByViewModel. #14145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sagar0-0
wants to merge
2
commits into
signalapp:main
Choose a base branch
from
Sagar0-0:sagar/test-findbyvm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
277 changes: 277 additions & 0 deletions
277
app/src/test/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByViewModelTest.kt
This file contains hidden or 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,277 @@ | ||
/* | ||
* Copyright 2025 Signal Messenger, LLC | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
package org.thoughtcrime.securesms.recipients.ui.findby | ||
|
||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.mockkObject | ||
import io.mockk.mockkStatic | ||
import io.mockk.unmockkObject | ||
import io.mockk.unmockkStatic | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.thoughtcrime.securesms.dependencies.AppDependencies | ||
import org.thoughtcrime.securesms.profiles.manage.UsernameRepository | ||
import org.thoughtcrime.securesms.recipients.LiveRecipientCache | ||
import org.thoughtcrime.securesms.recipients.Recipient | ||
import org.thoughtcrime.securesms.recipients.RecipientId | ||
import org.thoughtcrime.securesms.recipients.RecipientRepository | ||
import org.thoughtcrime.securesms.registration.ui.countrycode.Country | ||
import org.thoughtcrime.securesms.registration.ui.countrycode.CountryUtils | ||
import org.whispersystems.signalservice.api.push.ServiceId | ||
import java.util.Optional | ||
|
||
class FindByViewModelTest { | ||
|
||
private val liveRecipientCache = mockk<LiveRecipientCache>(relaxed = true) | ||
private lateinit var viewModel: FindByViewModel | ||
|
||
@Before | ||
fun setup() { | ||
mockkStatic(AppDependencies::class) | ||
every { AppDependencies.recipientCache } returns liveRecipientCache | ||
every { Recipient.self() } returns mockk { | ||
every { e164 } returns Optional.of("+15551234") | ||
} | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
unmockkStatic(AppDependencies::class) | ||
} | ||
|
||
@Test | ||
fun `Given phone number mode, when I change user entry, then I expect digits only`() { | ||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER) | ||
|
||
viewModel.onUserEntryChanged("123abc456") | ||
val result = viewModel.state.value.userEntry | ||
|
||
assertEquals("123456", result) | ||
} | ||
|
||
@Test | ||
fun `Given username mode, when I change user entry, then I expect unaltered value`() { | ||
viewModel = FindByViewModel(FindByMode.USERNAME) | ||
|
||
viewModel.onUserEntryChanged("username123") | ||
val result = viewModel.state.value.userEntry | ||
|
||
assertEquals("username123", result) | ||
} | ||
|
||
@Test | ||
fun `Given a selected country, when I update it, then I expect the state to reflect it`() { | ||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER) | ||
val country = Country(emoji = "", name = "United States", countryCode = 1, regionCode = "US") | ||
|
||
viewModel.onCountrySelected(country) | ||
val result = viewModel.state.value.selectedCountry | ||
|
||
assertEquals(country, result) | ||
} | ||
|
||
@Test | ||
fun `Given invalid username, when I click next, then I expect InvalidEntry`() = runTest { | ||
viewModel = FindByViewModel(FindByMode.USERNAME) | ||
|
||
viewModel.onUserEntryChanged("invalid username") | ||
val result = viewModel.onNextClicked() | ||
|
||
assertTrue(result is FindByResult.InvalidEntry) | ||
} | ||
|
||
@Test | ||
fun `Given empty phone number, when I click next, then I expect InvalidEntry`() = runTest { | ||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER) | ||
|
||
viewModel.onUserEntryChanged("") | ||
mockkStatic(RecipientRepository::class).apply { | ||
every { RecipientRepository.lookupNewE164(any()) } returns RecipientRepository.LookupResult.InvalidEntry | ||
} | ||
|
||
val result = viewModel.onNextClicked() | ||
|
||
assertTrue(result is FindByResult.InvalidEntry) | ||
|
||
unmockkObject(RecipientRepository) | ||
} | ||
|
||
@Test | ||
fun `Given valid phone lookup, when I click next, then I expect Success`() = runTest { | ||
val recipientId = RecipientId.from(123L) | ||
|
||
mockkStatic(RecipientRepository::class).apply { | ||
every { RecipientRepository.lookupNewE164("+15551234") } returns | ||
RecipientRepository.LookupResult.Success(recipientId) | ||
} | ||
|
||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER) | ||
val country = Country(emoji = "🇺🇸", name = "United States", countryCode = 1, regionCode = "US") | ||
|
||
viewModel.onCountrySelected(country) | ||
viewModel.onUserEntryChanged("5551234") | ||
|
||
val result = viewModel.onNextClicked() | ||
|
||
assertTrue(result is FindByResult.Success) | ||
assertEquals((result as FindByResult.Success).recipientId, recipientId) | ||
|
||
unmockkObject(RecipientRepository) | ||
} | ||
|
||
@Test | ||
fun `Given unknown phone lookup, when I click next, then I expect NotFound`() = runTest { | ||
val recipientId = RecipientId.from(123L) | ||
|
||
mockkStatic(RecipientRepository::class).apply { | ||
every { RecipientRepository.lookupNewE164(any()) } returns RecipientRepository.LookupResult.NotFound(recipientId) | ||
} | ||
|
||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER) | ||
viewModel.onUserEntryChanged("0000000000") | ||
|
||
val result = viewModel.onNextClicked() | ||
|
||
assertTrue(result is FindByResult.NotFound) | ||
assertEquals((result as FindByResult.NotFound).recipientId, recipientId) | ||
|
||
unmockkObject(RecipientRepository) | ||
} | ||
|
||
@Test | ||
fun `Given matching country name, when I filter countries, then I expect matched countries`() { | ||
val countries = listOf( | ||
Country(emoji = "🇺🇸", name = "United States", countryCode = 1, regionCode = "US"), | ||
Country(emoji = "🇨🇦", name = "Canada", countryCode = 1, regionCode = "CA"), | ||
Country(emoji = "🇬🇧", name = "United Kingdom", countryCode = 44, regionCode = "GB") | ||
) | ||
|
||
mockkObject(CountryUtils).apply { | ||
every { CountryUtils.getCountries() } returns countries | ||
} | ||
|
||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER) | ||
|
||
viewModel.filterCountries("United") | ||
val result = viewModel.state.value.filteredCountries | ||
|
||
assertEquals(2, result.size) | ||
assertTrue(result.any { it.name == "United States" }) | ||
assertTrue(result.any { it.name == "United Kingdom" }) | ||
|
||
unmockkObject(CountryUtils) | ||
} | ||
|
||
@Test | ||
fun `Given matching country code, when I filter countries, then I expect matched countries`() { | ||
val countries = listOf( | ||
Country(emoji = "🇺🇸", name = "United States", countryCode = 1, regionCode = "US"), | ||
Country(emoji = "🇨🇦", name = "Canada", countryCode = 1, regionCode = "CA"), | ||
Country(emoji = "🇬🇧", name = "United Kingdom", countryCode = 44, regionCode = "GB") | ||
) | ||
|
||
mockkObject(CountryUtils).apply { | ||
every { CountryUtils.getCountries() } returns countries | ||
} | ||
|
||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER) | ||
|
||
viewModel.filterCountries("1") | ||
val result = viewModel.state.value.filteredCountries | ||
|
||
assertEquals(2, result.size) | ||
assertTrue(result.any { it.name == "United States" }) | ||
assertTrue(result.any { it.name == "Canada" }) | ||
|
||
unmockkObject(CountryUtils) | ||
} | ||
|
||
@Test | ||
fun `Given empty country filter, when I filter countries, then I expect empty countries list`() { | ||
val countries = listOf( | ||
Country(emoji = "🇺🇸", name = "United States", countryCode = 1, regionCode = "US"), | ||
Country(emoji = "🇨🇦", name = "Canada", countryCode = 1, regionCode = "CA"), | ||
Country(emoji = "🇬🇧", name = "United Kingdom", countryCode = 44, regionCode = "GB") | ||
) | ||
|
||
mockkObject(CountryUtils).apply { | ||
every { CountryUtils.getCountries() } returns countries | ||
} | ||
|
||
viewModel = FindByViewModel(FindByMode.PHONE_NUMBER) | ||
|
||
viewModel.filterCountries("") | ||
val result = viewModel.state.value.filteredCountries | ||
|
||
assertEquals(0, result.size) | ||
|
||
unmockkObject(CountryUtils) | ||
} | ||
|
||
@Test | ||
fun `Given username not found, when I click next, then I expect NotFound`() = runTest { | ||
mockkStatic(UsernameRepository::class) | ||
every { UsernameRepository.fetchAciForUsername("john") } returns UsernameRepository.UsernameAciFetchResult.NotFound | ||
|
||
viewModel = FindByViewModel(FindByMode.USERNAME) | ||
viewModel.onUserEntryChanged("@john") | ||
|
||
val result = viewModel.onNextClicked() | ||
|
||
assertTrue(result is FindByResult.NotFound) | ||
|
||
unmockkObject(UsernameRepository) | ||
} | ||
|
||
@Test | ||
fun `Given username fetch network error, when I click next, then I expect NetworkError`() = runTest { | ||
mockkStatic(UsernameRepository::class) | ||
every { UsernameRepository.fetchAciForUsername("jane") } returns UsernameRepository.UsernameAciFetchResult.NetworkError | ||
|
||
viewModel = FindByViewModel(FindByMode.USERNAME) | ||
viewModel.onUserEntryChanged("@jane") | ||
|
||
val result = viewModel.onNextClicked() | ||
|
||
assertTrue(result is FindByResult.NetworkError) | ||
|
||
unmockkObject(UsernameRepository) | ||
} | ||
|
||
@Test | ||
fun `Given valid username, when I click next, then I expect Success`() = runTest { | ||
val aci: ServiceId.ACI = mockk(relaxed = true) | ||
val username = "@doe" | ||
val recipientId = RecipientId.from(456L) | ||
|
||
mockkStatic(UsernameRepository::class).apply { | ||
every { | ||
UsernameRepository.fetchAciForUsername("doe") // stripped @ | ||
} returns UsernameRepository.UsernameAciFetchResult.Success(aci) | ||
} | ||
|
||
mockkObject(Recipient) | ||
val mockRecipient = mockk<Recipient>() | ||
every { mockRecipient.id } returns recipientId | ||
every { Recipient.externalUsername(aci, username) } returns mockRecipient | ||
|
||
viewModel = FindByViewModel(FindByMode.USERNAME) | ||
viewModel.onUserEntryChanged(username) | ||
|
||
val result = viewModel.onNextClicked() | ||
|
||
assertTrue(result is FindByResult.Success) | ||
assertEquals(recipientId, (result as FindByResult.Success).recipientId) | ||
|
||
unmockkStatic(UsernameRepository::class) | ||
unmockkObject(Recipient) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want to keep this as the user provided input instead of the formatted input so things match what the user sees/entered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we can also remove the formattedNumber.