////////////////////////////////////////////////////////////////////////////
//
// Copyright 2021 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
import React, { useRef, useState } from "react";
import Realm from "realm";
import { Button, Text, View } from "react-native";
import { act, fireEvent, render, renderHook, waitFor } from "@testing-library/react-native";
import { RealmProvider, createRealmContext } from "..";
import {
RealmProviderFallback,
RealmProviderFromRealm,
areConfigurationsIdentical,
mergeRealmConfiguration,
} from "../RealmProvider";
import { randomRealmPath } from "./helpers";
import { RealmContext } from "../RealmContext";
import { MockedProgressRealmPromiseWithDelay, mockRealmOpen } from "./mocks";
const dogSchema: Realm.ObjectSchema = {
name: "dog",
primaryKey: "_id",
properties: {
_id: "int",
name: "string",
},
};
const catSchema: Realm.ObjectSchema = {
name: "cat",
primaryKey: "_id",
properties: {
_id: "int",
name: "string",
},
};
const withConfigRealmContext = createRealmContext({
schema: [dogSchema],
inMemory: true,
path: randomRealmPath(),
});
describe("RealmProvider", () => {
afterEach(() => {
Realm.clearTestState();
});
describe("with a Realm Configuration", () => {
const { RealmProvider, useRealm } = withConfigRealmContext;
it("returns the configured realm with useRealm", async () => {
const wrapper = ({ children }: { children: React.ReactNode }) => {children};
const { result } = renderHook(() => useRealm(), { wrapper });
await waitFor(() => expect(result.current).not.toBe(null));
const realm = result.current;
expect(realm).toBeInstanceOf(Realm);
expect(realm.schema[0].name).toBe("dog");
});
it("closes realm on unmount by default", async () => {
const wrapper = ({ children }: { children: React.ReactNode }) => {children};
const { result, unmount } = renderHook(() => useRealm(), { wrapper });
await waitFor(() => expect(result.current).not.toBe(null));
const realm = result.current;
unmount();
expect(realm.isClosed).toBe(true);
});
it("does not close realm on unmount if closeOnUnmount is false", async () => {
const wrapper = ({ children }: { children: React.ReactNode }) => (
{children}
);
const { result, unmount } = renderHook(() => useRealm(), { wrapper });
await waitFor(() => expect(result.current).not.toBe(null));
const realm = result.current;
unmount();
expect(realm.isClosed).toBe(false);
});
it("will override the configuration provided in createRealmContext", async () => {
const wrapper = ({ children }: { children: React.ReactNode }) => (
{children}
);
const { result } = renderHook(() => useRealm(), { wrapper });
await waitFor(() => expect(result.current).not.toBe(null));
const realm = result.current;
expect(realm).toBeInstanceOf(Realm);
expect(realm.schema[0].name).toBe("cat");
});
it("can be provided in multiple parts of an application", async () => {
const RealmComponent = () => {
const realm = useRealm();
return (