databasereact-nativerealm

React Native Realm testing


How can I test a Realm database made using React Native?

I have made the following table using Realm:

const exercises = {
    "name": "table",
    "properties": {
        "id": "int",
        "name": { "type": "string", "maxLength": 50 },
        "type": "string",
        "notes": { "type": "string", "maxLength": 500 },
        "video": "binary",
    },
    "primaryKey": "id",
};
const addRecord() => {}
const editRecord() => {}
const deleteRecord() => {}

I want to write tests to ensure the functions addRecord(), editRecord() and deleteRecord() work as expected.

Currently, I am using Jest to test the code. If possible, I would like to use Jest to test the table functions, although I am able to use other libraries.

I have tried making the table again in the realm.test.js file. However, I encounter the error:

SyntaxError: Unexpected token 'export'
> 1 | import Realm from "realm";

I have the following code in my package.json:

    "jest": {
        "preset": "jest-expo",
        "transformIgnorePatterns": [
            "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@sentry/react-native|native-base|react-native-svg)"
        ]
    },

I have attempted to add the realm directory in the node_modules folder to the transformIgnorePatterns array. This has not worked


Solution

  • Add @realm to the end of the transformIgnorePatterns Array.

    The new object should be:

        "jest": {
            "preset": "jest-expo",
            "transformIgnorePatterns": [
                "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@sentry/react-native|native-base|react-native-svg|@realm)"
            ]
        },
    

    Run the tests like this:

    describe("exercises table", () => {
    const exercises = {
        "name": "table",
        "properties": {
            "id": "int",
            "name": { "type": "string", "maxLength": 50 },
            "type": "string",
            "notes": { "type": "string", "maxLength": 500 },
            "video": "binary",
        },
        "primaryKey": "id",
    };
    test("should write to the table", () => {
        const realm = new Realm({ "schema": [exercises] });
        realm.write(() => { realm.deleteAll(); });
        realm.write(() => {
            realm.create("exercises", { id: 1, name: "Exercise 1", type: "type 1", notes: "Note 1", "video": "Binary" });
        });
        const exercise = realm.objects("exercises")[0];
        expect(exercise.name).toBe("Exercise 1");
        expect(exercise.type).toBe("Type 1");
        expect(exercise.notes).toBe("Note 1");
        expect(exercise.video).toBe("Binary");
    });
    });
    

    Define the table inside of the describe block. Write all of the tests inside of the same describe block