@carbon/ibm-cloud-cognitive-cdai
Version:
Carbon for Cloud & Cognitive CD&AI UI components
141 lines (139 loc) • 8.75 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
//
// Copyright IBM Corp. 2020, 2020
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//
var _require = require('gherkin-jest'),
cucumber = _require.cucumber;
var _require2 = require('stucumber'),
HookType = _require2.HookType;
jest.mock('../../component_helpers/Clipboard');
import { IdeAPIKeyGeneration } from '.';
import * as props from './test_assets/testProps.js';
import { idAttributeSelector } from '../../component_helpers/IDHelper';
import { copyToClipboard } from '../../component_helpers/Clipboard';
cucumber.defineRule('Setup of the IdeAPIKeyGeneration world', function (world) {
world.componentToRender = IdeAPIKeyGeneration;
});
// common teardown
cucumber.addHook(HookType.AfterScenarios, function () {
copyToClipboard.mockClear();
});
cucumber.addHook(HookType.AfterFeatures, function () {
copyToClipboard.mockRestore();
});
cucumber.defineRule('a core configuration and no steps', function (world) {
world.defaultProps = props.coreConfigFunc();
});
cucumber.defineRule('a core configuration with a descriptive name step', function (world) {
world.defaultProps = _objectSpread(_objectSpread({}, props.coreConfigFunc()), props.descriptiveNameStepConfigFunc());
});
cucumber.defineRule('a core configuration with custom steps', function (world) {
world.defaultProps = _objectSpread(_objectSpread({}, props.coreConfigFunc()), props.customStepsConfigFunc());
});
cucumber.defineRule('a core configuration with a descriptive name step and custom steps', function (world) {
world.defaultProps = _objectSpread(_objectSpread(_objectSpread({}, props.coreConfigFunc()), props.descriptiveNameStepConfigFunc()), props.customStepsConfigFunc());
});
cucumber.defineRule('the callback property {string} is specified', function (world, callbackName) {
world.callbacks = _objectSpread(_objectSpread({}, world.callbacks), {}, _defineProperty({}, callbackName, jest.fn()));
world.props = _objectSpread(_objectSpread({}, world.props), {}, _defineProperty({}, callbackName, world.callbacks[callbackName]));
});
cucumber.defineRule('the descriptiveNameStep property {string} with value {string} is specified', function (world, propName, propValue) {
world.props = _objectSpread(_objectSpread({}, world.props), {}, {
descriptiveNameStep: _objectSpread(_objectSpread({}, world.defaultProps.descriptiveNameStep), {}, _defineProperty({}, propName, propValue))
});
});
cucumber.defineRule('the descriptiveNameStep {string} property changes to the value {string}', function (world, propName, propValue) {
var descriptiveNameStep = _objectSpread(_objectSpread({}, world.defaultProps.descriptiveNameStep), {}, _defineProperty({}, propName, propValue));
world.mountedComponent.setProps({
descriptiveNameStep: descriptiveNameStep
});
});
cucumber.defineRule('the {string} element is rendered with a {string} property set to {string}', function (world, elementName, propertyName, value) {
var element = world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration-".concat(elementName)));
expect(element.exists()).toBe(true);
expect(element.props()[propertyName]).toBe(value);
});
cucumber.defineRule('the {string} element is rendered with a {string} property set to false', function (world, elementName, propertyName) {
var element = world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration-".concat(elementName)));
expect(element.exists()).toBe(true);
expect(element.props()[propertyName]).toBe(false);
});
cucumber.defineRule('the {string} element is rendered with a {string} property set to true', function (world, elementName, propertyName) {
var element = world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration-".concat(elementName)));
expect(element.exists()).toBe(true);
expect(element.props()[propertyName]).toBe(true);
});
cucumber.defineRule('the {string} element is rendered with no {string} property', function (world, elementName, propertyName) {
var element = world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration-".concat(elementName)));
expect(element.exists()).toBe(true);
expect(element.props()[propertyName]).toBeUndefined();
});
cucumber.defineRule('the {string} element is rendered with text {string}', function (world, elementName, value) {
var element = world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration-".concat(elementName)));
expect(element.exists()).toBe(true);
expect(element.text()).toBe(value);
});
cucumber.defineRule('the {string} callback prop on the {string} element is invoked', function (world, callback, elementName) {
var element = world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration-".concat(elementName)));
expect(element.exists()).toBe(true);
element.props()[callback]();
});
cucumber.defineRule('the {string} element is not rendered', function (world, elementName) {
expect(world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration-".concat(elementName))).exists()).toBe(false);
});
cucumber.defineRule('the modal has a {string} button with the label {string}', function (world, buttonOrder, labelText) {
expect(world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration")).props()["".concat(buttonOrder, "ButtonText")]).toBe(labelText);
});
cucumber.defineRule('the modal has a {string} property with the value {string}', function (world, propertyName, value) {
expect(world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration")).props()[propertyName]).toBe(value);
});
cucumber.defineRule('the modal {string} button is {string}', function (world, buttonOrder, state) {
expect(world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration")).props()["".concat(buttonOrder, "ButtonDisabled")]).toBe(state === 'disabled');
});
cucumber.defineRule('I click on the {string} element', function (world, elementName) {
world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration-".concat(elementName))).simulate('click', {
preventDefault: jest.fn()
});
});
var modalButtonCallbacks = {
close: 'onRequestClose',
secondary: 'onSecondarySubmit',
primary: 'onRequestSubmit'
};
cucumber.defineRule('I click on the modal {string} button', function (world, buttonOrder) {
var modalButtonCallback = modalButtonCallbacks[buttonOrder];
world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration")).props()
// prettier clashes with the no-unexpected-multiline es-lint rule here
// eslint-disable-next-line no-unexpected-multiline
[modalButtonCallback]();
});
cucumber.defineRule('the {string} callback is invoked', function (world, callbackName) {
expect(world.callbacks[callbackName]).toHaveBeenCalled();
});
cucumber.defineRule('the {string} callback is invoked with argument {int}', function (world, callbackName, value) {
expect(world.callbacks[callbackName]).toBeCalled();
expect(world.callbacks[callbackName].mock.calls[0][0]).toEqual(value);
world.callbacks[callbackName].mockClear();
});
cucumber.defineRule('the {string} callback is invoked with an object with property {string} and value {string}', function (world, callbackName, property, value) {
expect(world.callbacks[callbackName]).toBeCalled();
expect(world.callbacks[callbackName].mock.calls[0][0][property]).toEqual(value);
});
cucumber.defineRule('the value {string} is copied to the clipboard', function (world, value) {
expect(copyToClipboard).toHaveBeenCalled();
// the cucumber library doesn't support strings with escaped " chars,
// so we use ' chars instead and replace them here
expect(copyToClipboard.mock.calls[0][1]).toEqual(value.replace(/'/g, '"'));
});
cucumber.defineRule('I type the value {string} into the {string} element', function (world, value, elementName) {
world.mountedComponent.find(idAttributeSelector("IdeAPIKeyGeneration-".concat(elementName))).simulate('change', {
target: {
value: value
}
});
});