stitch-ui
Version:
205 lines (179 loc) • 6.48 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 {
testSetup,
noConsoleErrorsAllowed,
stubConfirmation
} from "../../testutil";
import Services from "../../app/components/Services";
import EditIncomingWebhooks from "../components/EditIncomingWebhooks";
import * as homeActions from "../../home/actions";
import * as appActions from "../../app/actions";
import * as svcActions from "../../services/actions";
import * as actions from "../actions";
import { servicesByType } from "../../services/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("default edit webhooks", () => {
noConsoleErrorsAllowed();
let store;
let testApp;
let webhooksEditor;
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,
"http"
)
);
await store.dispatch(
svcActions.saveConfig(testApp.groupId, testApp._id, testSvcName, {})
);
await store.dispatch(appActions.loadServices(testApp.groupId, testApp._id));
await new Promise(resolve => {
actionSub.subscribe(
actions.loadIncomingWebhooksActions.rcv.getType(),
resolve
);
webhooksEditor = mount(
<Provider store={store}>
<MemoryRouter>
<Services
svcname={testSvcName}
app={testApp}
params={{ svcname: testSvcName }}
match={{ path: "/", params: { svcname: testSvcName } }}
>
<EditIncomingWebhooks
svcname={testSvcName}
services={store.getState().app.root.services}
app={testApp}
params={{ svcname: testSvcName }}
>
{servicesByType
.get("http")
.getRulesEditor({ svcName: testSvcName, app: testApp })}
</EditIncomingWebhooks>
</Services>
</MemoryRouter>
</Provider>
);
});
});
beforeEach(() => {
actionSub.reset();
});
afterEach(() => {
actionSub.reset();
});
const newWebhookButton = () =>
webhooksEditor
.find("button")
.findWhere(x => x.text().trim() === "New Webhook");
it("Loads the webhooks editor for test service", async () => {
expect(newWebhookButton(webhooksEditor)).toHaveLength(1);
expect(webhooksEditor.find("div.splitpanel-rightside")).toHaveLength(0);
});
it("Renders the webhooks editor with 'create' and 'cancel' button but no 'delete' button when creating a new webhook", async () => {
await newWebhookButton(webhooksEditor).props().onClick();
const headerButtons = webhooksEditor
.find("div.panel-header")
.find("button");
expect(headerButtons).toHaveLength(2);
expect(headerButtons.at(0).text().trim()).toEqual("Create");
expect(headerButtons.at(1).text().trim()).toEqual("Cancel");
const primaryButtons = webhooksEditor
.find("div.panel-header")
.find("button.button-is-primary");
expect(primaryButtons).toHaveLength(1);
expect(primaryButtons.at(0).text()).toEqual("Create");
});
it("Renders the webhooks editor with no webhook url when creating a new webhook", async () => {
await newWebhookButton(webhooksEditor).at(0).props().onClick();
expect(webhooksEditor.find("label[htmlFor='webhook-url']")).toHaveLength(0);
});
it("Can render webhook editor", async () => {
await newWebhookButton(webhooksEditor).at(0).props().onClick();
expect(webhooksEditor.find(".splitpanel-is-right")).toHaveLength(1);
expect(webhooksEditor.find(".edit-list-item-info")).toHaveLength(0);
});
it("Can change webhook options", async () => {
webhooksEditor
.find("input[name='secret']")
.props()
.onChange({ target: { value: "hello" } });
await webhooksEditor
.find("button")
.findWhere(x => x.text().trim() === "Create")
.at(0)
.props()
.onClick();
expect(webhooksEditor.find("input[name='secret']").props().value).toEqual(
"hello"
);
});
const createWebhook = async () => {
await newWebhookButton(webhooksEditor).props().onClick();
webhooksEditor
.find("input[name='secret']")
.props()
.onChange({ target: { value: "superSecret" } });
await webhooksEditor
.find("button")
.findWhere(x => x.text().trim() === "Create")
.at(0)
.props()
.onClick();
};
it("Renders with 'save', 'cancel', and 'delete' buttons when opening an existing webhook", async () => {
await createWebhook();
await webhooksEditor.find("EditListItem").at(0).props().onClick();
const headerButtons = webhooksEditor
.find("div.panel-header")
.find("button");
expect(headerButtons).toHaveLength(3);
expect(headerButtons.at(0).text().trim()).toEqual("Save");
expect(headerButtons.at(1).text().trim()).toEqual("Cancel");
expect(headerButtons.at(2).text().trim()).toEqual("Delete");
const primaryButtons = webhooksEditor
.find("div.panel-header")
.find("button.button-is-primary");
expect(primaryButtons).toHaveLength(1);
expect(primaryButtons.at(0).text()).toEqual("Save");
});
it("Can delete a webhook", async () => {
await createWebhook();
const listItems = webhooksEditor.find("EditListItem");
expect(listItems).toHaveLength(1);
await listItems.at(0).props().onClick();
stubConfirmation(true);
await webhooksEditor
.find("button")
.findWhere(x => x.text().trim() === "Delete")
.at(0)
.props()
.onClick();
expect(webhooksEditor.find("div.splitpanel-rightside")).toHaveLength(0);
expect(listItems).toHaveLength(1);
});
});