-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for Site Monitor mapper and utils
- Loading branch information
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
WordPress/src/test/java/org/wordpress/android/ui/sitemonitor/SiteMonitorMapperTest.kt
This file contains 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,58 @@ | ||
package org.wordpress.android.ui.sitemonitor | ||
|
||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.mock | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import org.mockito.kotlin.whenever | ||
import org.wordpress.android.BaseUnitTest | ||
|
||
@ExperimentalCoroutinesApi | ||
@RunWith(MockitoJUnitRunner::class) | ||
class SiteMonitorMapperTest : BaseUnitTest() { | ||
@Mock | ||
lateinit var siteMonitorUtils: SiteMonitorUtils | ||
|
||
private lateinit var siteMonitorMapper: SiteMonitorMapper | ||
|
||
@Before | ||
fun setup() { | ||
siteMonitorMapper = SiteMonitorMapper(siteMonitorUtils) | ||
} | ||
|
||
@Test | ||
fun `given prepared request, when mapper is called, then site monitor model is created`() { | ||
whenever(siteMonitorUtils.getUserAgent()).thenReturn(USER_AGENT) | ||
|
||
val state = siteMonitorMapper.toPrepared(URL, ADDRESS_TO_LOAD, SiteMonitorType.METRICS) | ||
|
||
assertThat(state.model.siteMonitorType).isEqualTo(SiteMonitorType.METRICS) | ||
assertThat(state.model.url).isEqualTo(URL) | ||
assertThat(state.model.addressToLoad).isEqualTo(ADDRESS_TO_LOAD) | ||
assertThat(state.model.userAgent).isEqualTo(USER_AGENT) | ||
} | ||
|
||
@Test | ||
fun `given network error, when mapper is called, then NoNetwork error is created`() { | ||
val state = siteMonitorMapper.toNoNetworkError(mock()) | ||
|
||
assertThat(state).isInstanceOf(SiteMonitorUiState.NoNetworkError::class.java) | ||
} | ||
|
||
@Test | ||
fun `given generic error error, when mapper is called, then Generic error is created`() { | ||
val state = siteMonitorMapper.toGenericError(mock()) | ||
|
||
assertThat(state).isInstanceOf(SiteMonitorUiState.GenericError::class.java) | ||
} | ||
|
||
companion object { | ||
const val USER_AGENT = "user_agent" | ||
const val URL = "url" | ||
const val ADDRESS_TO_LOAD = "address_to_load" | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
WordPress/src/test/java/org/wordpress/android/ui/sitemonitor/SiteMonitorUtilsTest.kt
This file contains 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,92 @@ | ||
package org.wordpress.android.ui.sitemonitor | ||
|
||
import junit.framework.TestCase.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import org.mockito.kotlin.verify | ||
import org.wordpress.android.analytics.AnalyticsTracker | ||
import org.wordpress.android.util.analytics.AnalyticsTrackerWrapper | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class SiteMonitorUtilsTest { | ||
@Mock | ||
lateinit var analyticsTrackerWrapper: AnalyticsTrackerWrapper | ||
|
||
private lateinit var siteMonitorUtils: SiteMonitorUtils | ||
|
||
@Before | ||
fun setup() { | ||
siteMonitorUtils = SiteMonitorUtils(analyticsTrackerWrapper) | ||
} | ||
|
||
@Test | ||
fun `when activity is launched, then event is tracked`() { | ||
siteMonitorUtils.trackActivityLaunched() | ||
|
||
verify(analyticsTrackerWrapper).track(AnalyticsTracker.Stat.SITE_MONITORING_SCREEN_SHOWN) | ||
} | ||
|
||
@Test | ||
fun `given url matches pattern, when sanitize is requested, then url is sanitized`() { | ||
val result = siteMonitorUtils.sanitizeSiteUrl("http://example.com") | ||
|
||
assertEquals("example.com", result) | ||
} | ||
|
||
@Test | ||
fun `given url is null, when sanitize is requested, then url is empty`() { | ||
val result = siteMonitorUtils.sanitizeSiteUrl(null) | ||
|
||
assertEquals("", result) | ||
} | ||
|
||
@Test | ||
fun `given url does not match pattern, when sanitize is requested, then url is not sanitized`() { | ||
val url = "gibberish" | ||
val result = siteMonitorUtils.sanitizeSiteUrl(url) | ||
|
||
assertEquals(url, result) | ||
} | ||
|
||
@Test | ||
fun `when metrics tab is launched, then event is tracked`() { | ||
siteMonitorUtils.trackTabLoaded(SiteMonitorType.METRICS) | ||
|
||
// Verify that the correct method was called on the analyticsTrackerWrapper | ||
verify(analyticsTrackerWrapper).track( | ||
AnalyticsTracker.Stat.SITE_MONITORING_TAB_SHOWN, | ||
mapOf( | ||
SiteMonitorUtils.TAB_TRACK_KEY to SiteMonitorType.METRICS.analyticsDescription | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `when php logs tab is launched, then event is tracked`() { | ||
siteMonitorUtils.trackTabLoaded(SiteMonitorType.PHP_LOGS) | ||
|
||
// Verify that the correct method was called on the analyticsTrackerWrapper | ||
verify(analyticsTrackerWrapper).track( | ||
AnalyticsTracker.Stat.SITE_MONITORING_TAB_SHOWN, | ||
mapOf( | ||
SiteMonitorUtils.TAB_TRACK_KEY to SiteMonitorType.PHP_LOGS.analyticsDescription | ||
) | ||
) | ||
} | ||
|
||
@Test | ||
fun `when web server logs tab is launched, then event is tracked`() { | ||
siteMonitorUtils.trackTabLoaded(SiteMonitorType.WEB_SERVER_LOGS) | ||
|
||
// Verify that the correct method was called on the analyticsTrackerWrapper | ||
verify(analyticsTrackerWrapper).track( | ||
AnalyticsTracker.Stat.SITE_MONITORING_TAB_SHOWN, | ||
mapOf( | ||
SiteMonitorUtils.TAB_TRACK_KEY to SiteMonitorType.WEB_SERVER_LOGS.analyticsDescription | ||
) | ||
) | ||
} | ||
} |