Skip to content
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

[core][Android] Make defaultAppContextMock private #28620

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestScope
import java.lang.ref.WeakReference

internal fun defaultAppContextMock(
jniDeallocator: JNIDeallocator = JNIDeallocator(shouldCreateDestructorThread = false),
jsiInterop: JSIContext = JSIContext()
private fun defaultAppContextMock(
jniDeallocator: JNIDeallocator = JNIDeallocator(shouldCreateDestructorThread = false)
): AppContext {
val appContextMock = mockk<AppContext>()
val coreModule = run {
Expand All @@ -40,7 +39,7 @@ internal fun defaultAppContextMock(
every { appContextMock.classRegistry } answers { ClassRegistry() }
every { appContextMock.jniDeallocator } answers { jniDeallocator }
every { appContextMock.findView<View>(capture(slot())) } answers { mockk() }
every { appContextMock.jsiInterop } answers { jsiInterop }

return appContextMock
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
@file:OptIn(ExperimentalCoroutinesApi::class)

package expo.modules.kotlin.jni

import com.google.common.truth.Truth
import org.junit.Before
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Test

class JavaScriptFunctionTest {
private lateinit var jsiInterop: JSIContext

@Before
fun before() {
jsiInterop = JSIContext().apply {
installJSIForTests(defaultAppContextMock(jsiInterop = this))
}
}

@Test
fun should_be_able_to_receive_function_instance() {
val jsValue = jsiInterop.evaluateScript("() => {}")
fun should_be_able_to_receive_function_instance() = withJSIInterop {
val jsValue = evaluateScript("() => {}")
Truth.assertThat(jsValue.isFunction()).isTrue()
val jsFunction = jsValue.getFunction()
Truth.assertThat(jsFunction).isNotNull()
}

@Test
fun should_be_able_to_return_value() {
val function = jsiInterop.evaluateScript("() => { return 21 }").getFunction<Int>()
fun should_be_able_to_return_value() = withJSIInterop {
val function = evaluateScript("() => { return 21 }").getFunction<Int>()
val result = function()
Truth.assertThat(result).isEqualTo(21)
}

@Test
fun should_be_able_to_accept_simple_data() {
val function = jsiInterop.evaluateScript("(a, b) => { return a + b }").getFunction<Int>()
fun should_be_able_to_accept_simple_data() = withJSIInterop {
val function = evaluateScript("(a, b) => { return a + b }").getFunction<Int>()
val result = function(20, 50)
Truth.assertThat(result).isEqualTo(70)
}

@Test
fun should_be_able_to_accept_complex_data() {
val function = jsiInterop.evaluateScript("(object) => { return object.k1 }").getFunction<String>()
fun should_be_able_to_accept_complex_data() = withJSIInterop {
val function = evaluateScript("(object) => { return object.k1 }").getFunction<String>()
val result = function(mapOf("k1" to "expo"))
Truth.assertThat(result).isEqualTo("expo")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,43 @@ package expo.modules.kotlin.jni

import com.google.common.truth.Truth
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Before
import org.junit.Test

class JavaScriptObjectTest {
private lateinit var jsiInterop: JSIContext

private fun emptyObject(): JavaScriptObject {
return jsiInterop.evaluateScript("({ })").getObject()
}

@Before
fun before() {
jsiInterop = JSIContext().apply {
installJSIForTests(defaultAppContextMock(jsiInterop = this))
}
}
fun JSIContext.emptyObject(): JavaScriptObject {
return evaluateScript("({ })").getObject()
}

class JavaScriptObjectTest {
@Test
fun hasProperty_should_return_false_when_the_property_is_missing() {
fun hasProperty_should_return_false_when_the_property_is_missing() = withJSIInterop {
val jsObject = emptyObject()
Truth.assertThat(jsObject.hasProperty("prop")).isFalse()
}

@Test
fun hasProperty_should_return_true_when_the_property_exists() {
val jsObject = jsiInterop.evaluateScript("({ 'prop': 123 })").getObject()
fun hasProperty_should_return_true_when_the_property_exists() = withJSIInterop {
val jsObject = evaluateScript("({ 'prop': 123 })").getObject()
Truth.assertThat(jsObject.hasProperty("prop")).isTrue()
}

@Test
fun getProperty_should_return_correct_value_is_the_property_exists() {
val jsObject = jsiInterop.evaluateScript("({ 'prop': 123 })").getObject()
fun getProperty_should_return_correct_value_is_the_property_exists() = withJSIInterop {
val jsObject = evaluateScript("({ 'prop': 123 })").getObject()
val property = jsObject.getProperty("prop")
Truth.assertThat(property.isNumber()).isTrue()
Truth.assertThat(property.getDouble()).isEqualTo(123)
}

@Test
fun getProperty_should_return_undefined_when_the_property_is_missing() {
val jsObject = jsiInterop.evaluateScript("({ 'prop': 123 })").getObject()
fun getProperty_should_return_undefined_when_the_property_is_missing() = withJSIInterop {
val jsObject = evaluateScript("({ 'prop': 123 })").getObject()
val property = jsObject.getProperty("foo")
Truth.assertThat(property.isUndefined()).isTrue()
}

@Test
fun setProperty_should_work_with_bool() {
val jsObject = jsiInterop.evaluateScript("({ })").getObject()
fun setProperty_should_work_with_bool() = withJSIInterop {
val jsObject = evaluateScript("({ })").getObject()
jsObject.setProperty("foo", true)
jsObject.setProperty("bar", false)

Expand All @@ -62,57 +52,66 @@ class JavaScriptObjectTest {
}

@Test
fun setProperty_should_work_with_int() = with(emptyObject()) {
setProperty("foo", 123)
val foo = getProperty("foo").getDouble()
Truth.assertThat(foo).isEqualTo(123)
fun setProperty_should_work_with_int() = withJSIInterop {
with(emptyObject()) {
setProperty("foo", 123)
val foo = getProperty("foo").getDouble()
Truth.assertThat(foo).isEqualTo(123)
}
}

@Test
fun setProperty_should_work_with_double() = with(emptyObject()) {
setProperty("foo", 20.43)
val foo = getProperty("foo").getDouble()
Truth.assertThat(foo).isEqualTo(20.43)
fun setProperty_should_work_with_double() = withJSIInterop {
with(emptyObject()) {
setProperty("foo", 20.43)
val foo = getProperty("foo").getDouble()
Truth.assertThat(foo).isEqualTo(20.43)
}
}

@Test
fun setProperty_should_work_with_string() = with(emptyObject()) {
setProperty("foo", "bar")
setProperty("bar", null as String?)
fun setProperty_should_work_with_string() = withJSIInterop {
with(emptyObject()) {
setProperty("foo", "bar")
setProperty("bar", null as String?)

val foo = getProperty("foo").getString()
val bar = getProperty("bar")
val foo = getProperty("foo").getString()
val bar = getProperty("bar")

Truth.assertThat(foo).isEqualTo("bar")
Truth.assertThat(bar.isUndefined()).isTrue()
Truth.assertThat(foo).isEqualTo("bar")
Truth.assertThat(bar.isUndefined()).isTrue()
}
}

@Test
fun setProperty_should_work_with_js_value() = with(emptyObject()) {
val jsValue = jsiInterop.evaluateScript("123")

setProperty("foo", jsValue)
fun setProperty_should_work_with_js_value() = withJSIInterop {
with(emptyObject()) {
val jsValue = evaluateScript("123")

val foo = getProperty("foo").getDouble()
setProperty("foo", jsValue)

Truth.assertThat(foo).isEqualTo(123)
val foo = getProperty("foo").getDouble()
Truth.assertThat(foo).isEqualTo(123)
}
}

@Test
fun setProperty_should_work_with_js_object() = with(emptyObject()) {
val jsObject = jsiInterop.evaluateScript("({ 'bar': 10 })").getObject()
fun setProperty_should_work_with_js_object() = withJSIInterop {
with(emptyObject()) {
val jsObject = evaluateScript("({ 'bar': 10 })").getObject()

setProperty("foo", jsObject)
setProperty("foo", jsObject)

val foo = getProperty("foo").getObject()
val bar = foo.getProperty("bar").getDouble()
val foo = getProperty("foo").getObject()
val bar = foo.getProperty("bar").getDouble()

Truth.assertThat(bar).isEqualTo(10)
Truth.assertThat(bar).isEqualTo(10)
}
}

@Test
fun setProperty_should_work_with_untyped_null() {
val jsObject = jsiInterop.evaluateScript("({ 'foo': 10 })").getObject()
fun setProperty_should_work_with_untyped_null() = withJSIInterop {
val jsObject = evaluateScript("({ 'foo': 10 })").getObject()

jsObject.setProperty("foo", null)
val foo = jsObject.getProperty("foo")
Expand All @@ -121,40 +120,48 @@ class JavaScriptObjectTest {
}

@Test
fun defineProperty_defines_non_enumerable_property() = with(emptyObject()) {
defineProperty("expo", 10)
fun defineProperty_defines_non_enumerable_property() = withJSIInterop {
with(emptyObject()) {
defineProperty("expo", 10)

Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(10)
Truth.assertThat(getPropertyNames().toList()).doesNotContain("expo")
Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(10)
Truth.assertThat(getPropertyNames().toList()).doesNotContain("expo")
}
}

@Test
fun defineProperty_defines_enumerable_property() = with(emptyObject()) {
// When the property is enumerable, it is listed in the property names
defineProperty("expo", 10, listOf(JavaScriptObject.PropertyDescriptor.Enumerable))
fun defineProperty_defines_enumerable_property() = withJSIInterop {
with(emptyObject()) {
// When the property is enumerable, it is listed in the property names
defineProperty("expo", 10, listOf(JavaScriptObject.PropertyDescriptor.Enumerable))

Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(10)
Truth.assertThat(getPropertyNames().toList()).contains("expo")
Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(10)
Truth.assertThat(getPropertyNames().toList()).contains("expo")
}
}

@Test
fun defineProperty_defines_configurable_property() = with(emptyObject()) {
// Configurable allows to redefine the property
defineProperty("expo", 10, listOf(JavaScriptObject.PropertyDescriptor.Configurable))
Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(10)
fun defineProperty_defines_configurable_property() = withJSIInterop {
with(emptyObject()) {
// Configurable allows to redefine the property
defineProperty("expo", 10, listOf(JavaScriptObject.PropertyDescriptor.Configurable))
Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(10)

defineProperty("expo", 123)
Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(123)
defineProperty("expo", 123)
Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(123)
}
}

@Test
fun defineProperty_defines_writable_property() = with(emptyObject()) {
// Writable allows changing the property
defineProperty("expo", 10, listOf(JavaScriptObject.PropertyDescriptor.Writable))
Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(10)
fun defineProperty_defines_writable_property() = withJSIInterop {
with(emptyObject()) {
// Writable allows changing the property
defineProperty("expo", 10, listOf(JavaScriptObject.PropertyDescriptor.Writable))
Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(10)

setProperty("expo", 123)
Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(123)
setProperty("expo", 123)
Truth.assertThat(getProperty("expo").getDouble()).isEqualTo(123)
}
}

@Test
Expand Down
Loading
Loading