UNPKG

stitch-ui

Version:

162 lines (148 loc) 4.79 kB
/* 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, noConsoleErrorsAllowed, braceCompatShim } from "../../../testutil"; import { toJSON } from "../../../util"; 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; braceCompatShim(global.window); const testAppName = "my-test-app"; const testSvcName = "http1"; describe("default 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, "http" ) ); await store.dispatch( actions.saveConfig(testApp.groupId, testApp._id, testSvcName, {}) ); await store.dispatch( actions.loadSvc(testApp.groupId, testApp._id, testSvcName) ); svcRulesEditor = mount( <Provider store={store}> <MemoryRouter> <Services svcname={testSvcName} app={testApp} params={{ svcname: testSvcName }} match={{ url: "/", params: { svcname: testSvcName } }} > {servicesByType .get("http") .getRulesEditor({ svcName: testSvcName, app: testApp })} </Services> </MemoryRouter> </Provider> ); }); beforeEach(() => { actionSub.reset(); }); afterEach(() => { actionSub.reset(); }); it("Loads the rules editor", async () => { expect(svcRulesEditor.find("button").at(0).text()).toEqual("Add Rule"); }); it("Adding a rule works", async () => { const defaultHTTPRule = { "%%args.url.host": { "%in": ["google.com"] } }; expect( Object.keys(store.getState().service.base.service.rules) ).toHaveLength(0); await svcRulesEditor.find("button").at(0).props().onClick(); expect( Object.keys(store.getState().service.base.service.rules) ).toHaveLength(1); expect(svcRulesEditor.find("ReactAce")).toHaveLength(1); expect(svcRulesEditor.find("ReactAce").at(0).props().value).toEqual( toJSON(defaultHTTPRule) ); ["Save", "Delete"].forEach(name => expect( svcRulesEditor.find("button").findWhere(x => x.text() === name) ).toHaveLength(1) ); }); it("Setting the rule to invalid JSON triggers error on save", async () => { svcRulesEditor.find("ReactAce").at(0).props().onChange("not json"); svcRulesEditor .find("button") .findWhere(x => x.text() === "Save") .simulate("click"); // Make sure we don't lose the form input expect(svcRulesEditor.find("ReactAce").at(0).props().value).toEqual( "not json" ); expect(svcRulesEditor.find(".banner-error").text()).toEqual("Invalid JSON"); }); it("Setting the rule to JSON saves correctly, removes error, sets input to formatted JSON", async () => { actionSub.reset(); svcRulesEditor .find("ReactAce") .at(0) .props() .onChange('{"a":"this is valid json"}'); await svcRulesEditor .find("button") .findWhere(x => x.text() === "Save") .props() .onClick(); expect(svcRulesEditor.find("ReactAce").at(0).props().value).toEqual( toJSON({ a: "this is valid json" }) ); expect(svcRulesEditor.find(".banner-error")).toHaveLength(0); }); it("Asynchronous errors are picked up and displayed correctly", async () => { actionSub.reset(); svcRulesEditor .find("ReactAce") .at(0) .props() .onChange('{"%%invalidexpansion1":"foo"}'); await svcRulesEditor .find("button") .findWhere(x => x.text() === "Save") .props() .onClick(); expect(svcRulesEditor.find(".banner-error")).toHaveLength(1); expect(svcRulesEditor.find(".banner-error").text()).toMatch( "do not know how to expand" ); }); });