stitch-ui
Version:
115 lines (102 loc) • 3.39 kB
JavaScript
/* global it, describe, 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 ProfileKeys from "../components/ProfileKeys";
import {
testSetup,
changeInput,
noConsoleErrorsAllowed,
stubConfirmation
} from "../../testutil";
import * as homeActions from "../../home/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 reloadedKeys = actions.profileKeyActions.loadAPIKeysActions.rcv.getType();
const checked = c => ({ target: { checked: c } });
const testAppName = "my-test-app";
describe("profile", () => {
noConsoleErrorsAllowed();
let store;
let profileDisplay;
let actionSub;
beforeAll(async () => {
const testHarness = await testSetup();
store = testHarness.store;
actionSub = testHarness.actionSub;
await store.dispatch(
homeActions.createApp(testHarness.groupId, testAppName)
);
await new Promise(resolve => {
actionSub.subscribe(reloadedKeys, resolve);
profileDisplay = mount(
<Provider store={store}>
<ProfileKeys />
</Provider>
);
});
});
afterEach(() => {
actionSub.reset();
});
const findButtonByName = name =>
profileDisplay.find("button").findWhere(x => x.text() === name);
it("Loads the profile keys display", async () => {
expect(findButtonByName("Create API Key")).toHaveLength(1);
});
it("Can create a new profile key", async () => {
findButtonByName("Create API Key").simulate("click");
changeInput(profileDisplay, "newKeyName", "testkey");
await new Promise(resolve => {
actionSub.subscribe(reloadedKeys, resolve);
findButtonByName("Save").simulate("click");
});
expect(Object.keys(store.getState().profile.apiKeys.toJS())).toHaveLength(
1
);
expect(profileDisplay.find(".api-key")).toHaveLength(1);
});
it("Can disable the key", async () => {
const profileKey = Object.keys(store.getState().profile.apiKeys.toJS())[0];
expect(store.getState().profile.apiKeys.get(profileKey).disabled).toBe(
false
);
await new Promise(resolve => {
actionSub.subscribe(reloadedKeys, resolve);
profileDisplay
.find("input[type='checkbox']")
.simulate("change", checked(false));
});
expect(store.getState().profile.apiKeys.get(profileKey).disabled).toBe(
true
);
// re-enable the key
actionSub.reset();
await new Promise(resolve => {
actionSub.subscribe(reloadedKeys, resolve);
profileDisplay
.find("input[type='checkbox']")
.simulate("change", checked(true));
});
expect(store.getState().profile.apiKeys.get(profileKey).disabled).toBe(
false
);
});
it("Can delete the key", async () => {
stubConfirmation(true);
await new Promise(resolve => {
actionSub.subscribe(reloadedKeys, resolve);
profileDisplay.find("button[name='deleteAPIKey']").simulate("click");
});
expect(Object.keys(store.getState().profile.apiKeys.toJS())).toHaveLength(
0
);
expect(profileDisplay.find(".api-key")).toHaveLength(0);
});
});