@applicaster/zapp-react-dom-app
Version:
Zapp App Component for Applicaster's Quick Brick React Native App
123 lines (94 loc) • 3.09 kB
JavaScript
import { loadDomPlugins } from "../loadDomPlugins";
const plugin = {
module: {},
identifier: "plugin",
name: "Plugin",
};
const pluginWithDomNode = {
module: {
requiresAppendingDomNode: true,
init: jest.fn(() => Promise.resolve()),
},
name: "Plugin with Dom Node",
configuration: { foo: "baz" },
};
const pluginWithDomNodeAndNoInit = {
module: {
requiresAppendingDomNode: true,
},
name: "Plugin with Dom node & no init method",
configuration: { foo: "bar" },
};
const resolver = jest.fn();
const rejecter = jest.fn();
function resetMocks() {
resolver.mockClear();
rejecter.mockClear();
}
describe("loadDomPlugins", () => {
beforeAll(() => {
global.document = {
body: {},
};
});
afterAll(() => {
delete global.document;
});
beforeEach(resetMocks);
describe("when there are no plugins requiring to write to the dom", () => {
it("resolves", async () => {
const plugins = [plugin];
await loadDomPlugins(plugins).then(resolver).catch(rejecter);
expect(resolver).toHaveBeenCalled();
expect(rejecter).not.toHaveBeenCalled();
});
});
describe("when there are plugins requiring to write to the dom", () => {
const consoleSpy = jest
.spyOn(console, "warn")
.mockImplementation(() => jest.fn());
beforeEach(() => {
resetMocks();
consoleSpy.mockClear();
});
afterAll(() => {
consoleSpy.mockRestore();
});
it("resolves", async () => {
const plugins = [plugin, pluginWithDomNode];
await loadDomPlugins(plugins).then(resolver).catch(rejecter);
expect(resolver).toHaveBeenCalled();
expect(rejecter).not.toHaveBeenCalled();
});
it("calls the init method of that plugin", async () => {
const plugins = [plugin, pluginWithDomNode];
await loadDomPlugins(plugins).then(resolver).catch(rejecter);
expect(resolver).toHaveBeenCalled();
expect(rejecter).not.toHaveBeenCalled();
expect(pluginWithDomNode.module.init).toHaveBeenCalledWith(
expect.objectContaining({
body: global.document.body,
configuration: pluginWithDomNode.configuration,
})
);
});
it("skips plugins without init method", async () => {
const plugins = [plugin, pluginWithDomNode, pluginWithDomNodeAndNoInit];
await loadDomPlugins(plugins).then(resolver).catch(rejecter);
expect(resolver).toHaveBeenCalled();
expect(rejecter).not.toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalled();
expect(consoleSpy.mock.calls).toMatchSnapshot();
});
it("skips the warning when not in __DEV__", async () => {
const devFlag = global.__DEV__;
global.__DEV__ = false;
const plugins = [plugin, pluginWithDomNode, pluginWithDomNodeAndNoInit];
await loadDomPlugins(plugins).then(resolver).catch(rejecter);
expect(resolver).toHaveBeenCalled();
expect(rejecter).not.toHaveBeenCalled();
expect(consoleSpy).not.toHaveBeenCalled();
global.__DEV__ = devFlag;
});
});
});