stitch-ui
Version:
113 lines (101 loc) • 3.85 kB
JavaScript
/* global it, describe, beforeAll, afterEach, expect, jasmine */
import React from "react"; // eslint-disable-line no-unused-vars
import { Provider } from "react-redux";
import { JSDOM } from "jsdom";
import { mount } from "enzyme";
import {
testSetup,
changeTextArea,
noConsoleErrorsAllowed
} from "../../../testutil";
import EditConfig from "../components/EditConfig";
import * as homeActions from "../../../home/actions";
import * as appActions from "../../../app/actions";
import * as actions from "../../actions";
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 = "mdb1";
describe("mongodb config", () => {
noConsoleErrorsAllowed();
let store;
let testApp;
let svcConfigEditor;
let actionSub;
beforeAll(async () => {
// Need to override the timeout interval, since the default (2 sec)
// may be shorter than the server needs to actually try connecting to an invalid URI.
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 10;
const testHarness = await testSetup();
store = testHarness.store;
actionSub = testHarness.actionSub;
testApp = await store.dispatch(
homeActions.createApp(testHarness.groupId, testAppName)
);
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)
);
svcConfigEditor = mount(
<Provider store={store}>
<EditConfig svcname={testSvcName} app={testApp} />
</Provider>
);
});
afterEach(() => {
actionSub.reset();
});
it("Loads the config editor with the correct data", async () => {
expect(svcConfigEditor.find("textarea").at(0).props().value).toBe(
"mongodb://localhost:26000"
);
});
it("gets an error if uri is missing", async () => {
changeTextArea(svcConfigEditor, "uri", "");
await svcConfigEditor.find("button").props().onClick();
expect(store.getState().service.base.savingConfig).toBe(false);
expect(store.getState().service.base.configSaveError).toBeTruthy();
expect(svcConfigEditor.find(".banner-error").text()).toMatch(
"expected 'uri' to be a 'string'"
);
});
it("gets an error if uri is invalid", async () => {
changeTextArea(svcConfigEditor, "uri", "mongodb://bogus:27017");
await svcConfigEditor.find("button").props().onClick();
expect(store.getState().service.base.savingConfig).toBe(false);
expect(store.getState().service.base.configSaveError).toBeTruthy();
expect(svcConfigEditor.find(".banner-error").text()).toMatch(
"error connecting to MongoDB"
);
});
it("saves successfully if valid data is provided", async () => {
changeTextArea(svcConfigEditor, "uri", "mongodb://localhost:26000");
await svcConfigEditor.find("button").props().onClick();
expect(store.getState().service.base.savingConfig).toBe(false);
expect(store.getState().service.base.configSaveError).toBeNull();
expect(svcConfigEditor.find(".banner-error")).toHaveLength(0);
});
it("saves successfully if there is leading/trailing whitespace", async () => {
changeTextArea(svcConfigEditor, "uri", " mongodb://localhost:26000 ");
await svcConfigEditor.find("button").props().onClick();
expect(store.getState().service.base.savingConfig).toBe(false);
expect(store.getState().service.base.configSaveError).toBeNull();
expect(svcConfigEditor.find(".banner-error")).toHaveLength(0);
});
});