Add test for WritableNativeMap

Summary: A simple test for WritableNativeMap. Also includes a test to verify that the key name exists in the error message if a dynamic cast exception occurs (this will be implemented in the future)

Reviewed By: mdvacca

Differential Revision: D12914375

fbshipit-source-id: 654674b1b32e7b3f38cc1364a2302d1ce08ec33e
This commit is contained in:
Andrew Chen (Eng) 2018-11-16 17:19:17 -08:00 committed by Facebook Github Bot
parent 6f34bc4016
commit 17ced5701f
1 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,104 @@
package com.facebook.react.tests.core;
import static org.fest.assertions.api.Assertions.assertThat;
import android.support.test.runner.AndroidJUnit4;
import com.facebook.react.bridge.UnexpectedNativeTypeException;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class WritableNativeMapTest {
private WritableNativeMap mMap;
@Before
public void setup() {
mMap = new WritableNativeMap();
mMap.putBoolean("boolean", true);
mMap.putDouble("double", 1.2);
mMap.putInt("int", 1);
mMap.putString("string", "abc");
mMap.putMap("map", new WritableNativeMap());
mMap.putArray("array", new WritableNativeArray());
mMap.putBoolean("dvacca", true);
mMap.setUseNativeAccessor(true);
}
@Test
public void testBoolean() {
assertThat(mMap.getBoolean("boolean")).isEqualTo(true);
}
@Test(expected = UnexpectedNativeTypeException.class)
public void testBooleanInvalidType() {
mMap.getBoolean("string");
}
@Test
public void testDouble() {
assertThat(mMap.getDouble("double")).isEqualTo(1.2);
}
@Test(expected = UnexpectedNativeTypeException.class)
public void testDoubleInvalidType() {
mMap.getDouble("string");
}
@Test
public void testInt() {
assertThat(mMap.getInt("int")).isEqualTo(1);
}
@Test(expected = UnexpectedNativeTypeException.class)
public void testIntInvalidType() {
mMap.getInt("string");
}
@Test
public void testString() {
assertThat(mMap.getString("string")).isEqualTo("abc");
}
@Test(expected = UnexpectedNativeTypeException.class)
public void testStringInvalidType() {
mMap.getString("int");
}
@Test
public void testMap() {
assertThat(mMap.getMap("map")).isNotNull();
}
@Test(expected = UnexpectedNativeTypeException.class)
public void testMapInvalidType() {
mMap.getMap("string");
}
@Test
public void testArray() {
assertThat(mMap.getArray("array")).isNotNull();
}
@Test(expected = UnexpectedNativeTypeException.class)
public void testArrayInvalidType() {
mMap.getArray("string");
}
@Ignore("Needs to be implemented")
@Test
public void testErrorMessageContainsKey() {
String key = "fkg";
try {
mMap.getString(key);
Assert.fail("Expected an UnexpectedNativeTypeException to be thrown");
} catch (UnexpectedNativeTypeException e) {
assertThat(e.getMessage()).contains(key);
}
}
}