Skip to content

Commit

Permalink
Fix up network module tests (facebook#48160)
Browse files Browse the repository at this point in the history
Summary:

Shift to mockito-kotlin and fix ReactCookieJarContainerTest so it's not just testing a mock.

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D66898956
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Dec 12, 2024
1 parent 9f6f1f5 commit ea481da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,49 @@ import okhttp3.HttpUrl
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.any
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when` as whenever
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner

/**
* Returns Mockito.any() as nullable type to avoid java.lang.IllegalStateException when null is
* returned.
*/
private fun <T> nonNullAny(type: Class<T>): T = any(type)

/** Tests for {@link NetworkingModule}. */
@RunWith(RobolectricTestRunner::class)
class ReactCookieJarContainerTest {
private val httpUrl: HttpUrl = HttpUrl.Builder().host("example.com").scheme("http").build()

@Test
fun testMissingJar() {
val jarContainer: ReactCookieJarContainer = mock(ReactCookieJarContainer::class.java)
val jarContainer = ReactCookieJarContainer()
assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(0)
}

@Test
fun testEmptyCookies() {
val jarContainer: ReactCookieJarContainer = mock(ReactCookieJarContainer::class.java)
val cookies: List<Cookie> = emptyList()
whenever(jarContainer.loadForRequest(nonNullAny(HttpUrl::class.java))).thenReturn(cookies)
val jarContainer = ReactCookieJarContainer()
val cookieJar = mock<CookieJar>()
jarContainer.setCookieJar(cookieJar)
whenever(cookieJar.loadForRequest(any())).thenReturn(emptyList())
assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(0)
}

@Test
fun testValidCookies() {
val jarContainer = ReactCookieJarContainer()
val cookieJar: CookieJar = mock(CookieJar::class.java)
val cookieJar = mock<CookieJar>()
jarContainer.setCookieJar(cookieJar)
val cookies: MutableList<Cookie> = mutableListOf()
cookies.add(Cookie.Builder().name("valid").value("valid value").domain("domain").build())
val cookies =
listOf(Cookie.Builder().name("valid").value("valid value").domain("domain").build())
whenever(cookieJar.loadForRequest(httpUrl)).thenReturn(cookies)
assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(1)
}

@Test
fun testInvalidCookies() {
val jarContainer = ReactCookieJarContainer()
val cookieJar: CookieJar = mock(CookieJar::class.java)
val cookieJar = mock<CookieJar>()
jarContainer.setCookieJar(cookieJar)
val cookies: MutableList<Cookie> = mutableListOf()
cookies.add(Cookie.Builder().name("valid").value("înválíd välūė").domain("domain").build())
val cookies =
listOf(Cookie.Builder().name("valid").value("înválíd välūė").domain("domain").build())
whenever(cookieJar.loadForRequest(httpUrl)).thenReturn(cookies)
assertThat(jarContainer.loadForRequest(httpUrl).size).isEqualTo(0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentCaptor
import org.mockito.MockedStatic
import org.mockito.Mockito.*
import org.mockito.Mockito.mockStatic
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify

class ResponseUtilTest {
private lateinit var reactContext: ReactApplicationContext
private lateinit var arguments: MockedStatic<Arguments>

@Before
fun setUp() {
reactContext = mock(ReactApplicationContext::class.java)
reactContext = mock()

arguments = mockStatic(Arguments::class.java)
arguments.`when`<WritableArray>(Arguments::createArray).thenAnswer { JavaOnlyArray() }
Expand Down Expand Up @@ -247,6 +250,6 @@ class ResponseUtilTest {
ResponseUtil.onRequestSuccess(null, 1)
ResponseUtil.onResponseReceived(null, 1, 200, Arguments.createMap(), "http://example.com")

verify(reactContext, never()).emitDeviceEvent(anyString(), any())
verify(reactContext, never()).emitDeviceEvent(any<String>(), any())
}
}

0 comments on commit ea481da

Please sign in to comment.