stitch-ui
Version:
125 lines (114 loc) • 3.99 kB
JavaScript
/* global it, describe, beforeEach, beforeAll, afterEach, expect */
import React from "react"; // eslint-disable-line no-unused-vars
import { Provider } from "react-redux";
import { JSDOM } from "jsdom";
import { mount } from "enzyme";
import { MemoryRouter } from "react-router-dom";
import Services from "../../../app/components/Services";
import {
testSetup,
changeInput,
noConsoleErrorsAllowed
} from "../../../testutil";
import * as homeActions from "../../../home/actions";
import * as appActions from "../../../app/actions";
import * as actions from "../../actions";
import { servicesByType } from "../../registry";
global.navigator = {
userAgent: "node.js"
};
const doc = new JSDOM("<!doctype html><html><body></body></html>");
global.document = doc;
global.window = doc.defaultView;
const testAppName = "my-test-app";
const testSvcName = "http1";
describe("mongodb edit rules", () => {
noConsoleErrorsAllowed();
let store;
let testApp;
let svcRulesEditor;
let actionSub;
beforeAll(async () => {
const testHarness = await testSetup();
store = testHarness.store;
actionSub = testHarness.actionSub;
testApp = await store.dispatch(
homeActions.createApp(testHarness.groupId, testAppName)
);
await store.dispatch(appActions.loadApp(testApp.groupId, testApp._id));
await store.dispatch(
appActions.createService(
testApp.groupId,
testApp._id,
testSvcName,
"mongodb"
)
);
await store.dispatch(
actions.saveConfig(testApp.groupId, testApp._id, testSvcName, {
uri: "mongodb://localhost:26000"
})
);
await store.dispatch(
actions.loadSvc(testApp.groupId, testApp._id, testSvcName)
);
await new Promise(resolve => {
svcRulesEditor = mount(
<Provider store={store}>
<MemoryRouter>
<Services
svcname={testSvcName}
app={testApp}
params={{ svcname: testSvcName }}
match={{
path: "/",
params: {
svcname: testSvcName,
appId: "app-id",
groupId: "group-id"
}
}}
>
{servicesByType
.get("mongodb")
.getRulesEditor({ svcName: testSvcName, app: testApp })}
</Services>
</MemoryRouter>
</Provider>
);
actionSub.subscribe(actions.loadSvcActions.rcv.getType(), resolve);
});
});
beforeEach(() => {
actionSub.reset();
});
afterEach(() => {
actionSub.reset();
});
it("Loads the rules editor", async () => {
const leftsideItems = svcRulesEditor.find("div.edit-list-header > button");
expect(leftsideItems.length).toBe(1);
expect(leftsideItems.text()).toEqual("New");
});
it("Can create a new namespace", async () => {
svcRulesEditor.find("div.edit-list-header > button").simulate("click");
// TODO check that creating with an invalid db/collection triggers and displays error.
expect(svcRulesEditor.find("input[name='database']")).toHaveLength(1);
expect(svcRulesEditor.find("input[name='collection']")).toHaveLength(1);
changeInput(svcRulesEditor, "collection", "testcollection");
changeInput(svcRulesEditor, "database", "testdb");
await svcRulesEditor
.find("button")
.findWhere(x => x.text() === "Create")
.props()
.onClick();
const ruleKeys = Object.keys(store.getState().service.base.service.rules);
expect(ruleKeys).toHaveLength(1);
expect(
store.getState().service.base.service.rules[ruleKeys[0]].namespace
).toEqual("testdb.testcollection");
});
// TODO test clicking on an existing field, changing read/write/valid, saving it.
// TODO test adding an invalid json for read/write/valid, error should be displayed.
// TODO test adding a new field, clicking on new field should display permissions editor for it.
});