@carbon/ibm-cloud-cognitive-cdai
Version:
Carbon for Cloud & Cognitive CD&AI UI components
140 lines (135 loc) • 6.73 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _ = require(".");
var jth = _interopRequireWildcard(require("../../component_helpers/jest_test_helper_functions.js"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
//
// 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.
//
describe('IdeHTTPErrors', function () {
var component, unmount;
var mountTestComponent = function mountTestComponent() {
var defaultProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var test = jth.mountComponent(jth.getJSXForComponent(_.IdeHTTPErrors, defaultProps, props), false);
component = test.component;
unmount = test.unmount;
};
afterEach(function () {
return unmount && unmount();
});
// Prop based rendering tests
it('renders when no props are defined', function () {
mountTestComponent();
expect(component.getElements()).toMatchSnapshot();
});
function rendersWhenPropIsDefined(propName) {
var propValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propName;
var testNameSuffix = arguments.length > 2 ? arguments[2] : undefined;
var testName = "renders when the ".concat(propName, " prop is defined");
if (testNameSuffix) {
testName += " as ".concat(testNameSuffix);
}
it(testName, function () {
mountTestComponent(undefined, (0, _defineProperty2.default)({}, propName, propValue));
expect(component.instance().props[propName]).toBe(propValue);
expect(component.getElements()).toMatchSnapshot();
});
}
rendersWhenPropIsDefined('background', 403, '403');
rendersWhenPropIsDefined('background', 404, '404');
rendersWhenPropIsDefined('title');
rendersWhenPropIsDefined('description');
rendersWhenPropIsDefined('details');
rendersWhenPropIsDefined('links', {
text: 'text',
url: 'url'
}, 'an object');
rendersWhenPropIsDefined('links', [{
text: 'text1',
url: 'url1'
}, {
text: 'text2',
url: 'url2'
}], 'an array');
// Validation tests
beforeEach(function () {
return jest.spyOn(console, 'error').mockImplementation(function () {});
});
afterEach(function () {
return console.error.mockRestore();
});
function throwsWithInvalidProp(propName) {
var propValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propName;
var testNameSuffix = arguments.length > 2 ? arguments[2] : undefined;
var errorMessageSuffix = arguments.length > 3 ? arguments[3] : undefined;
var testName = "throws when ".concat(propName, " prop ").concat(testNameSuffix);
it(testName, function () {
expect(function () {
return mountTestComponent(undefined, (0, _defineProperty2.default)({}, propName, propValue));
}).toThrowError("IdeHTTPErrors: ".concat(propName, " prop ").concat(errorMessageSuffix));
});
}
throwsWithInvalidProp('background', 1, 'is not equal to 403 or 404', 'must be 403 or 404, got 1');
throwsWithInvalidProp('title', 1, 'is not type string', 'must be type string, got number');
throwsWithInvalidProp('description', 1, 'is not type string', 'must be type string, got number');
throwsWithInvalidProp('details', 1, 'is not type string', 'must be type string, got number');
describe('links prop', function () {
throwsWithInvalidProp('links', 1, 'is not an object or array', 'must be an object or array of objects with text+url props, got 1');
function throwsWithInvalidNestedProp(propName, propValue, nestedPropName, testNameSuffix, errorMessageSuffix) {
var propNameSuffix = '.';
if (Array.isArray(propValue)) {
propNameSuffix = '[n].';
}
var testName = "throws when ".concat(propName).concat(propNameSuffix).concat(nestedPropName, " prop is ").concat(testNameSuffix);
it(testName, function () {
expect(function () {
return mountTestComponent(undefined, (0, _defineProperty2.default)({}, propName, propValue));
}).toThrowError("IdeHTTPErrors: ".concat(propName, ".").concat(nestedPropName, " prop ").concat(errorMessageSuffix));
});
}
describe('defined as an object', function () {
throwsWithInvalidNestedProp('links', {
text: 1,
url: ''
}, 'text', 'is not type string', 'must be type string, got number');
throwsWithInvalidNestedProp('links', {
text: '',
url: 1
}, 'url', 'is not type string', 'must be type string, got number');
});
describe('defined as an array', function () {
throwsWithInvalidProp('links', [], 'defined as an array does not contain any items', 'defined as an array must contain at least 1 item');
throwsWithInvalidNestedProp('links', [{
text: 1,
url: ''
}], 'text', 'is not type string', 'must be type string, got number');
throwsWithInvalidNestedProp('links', [{
text: '',
url: 1
}], 'url', 'is not type string', 'must be type string, got number');
});
});
// Coverage completion tests
describe('renderBackground method', function () {
it('returns null when inline and viewport width is greater than or equal to medium', function () {
mountTestComponent();
component.setState({
width: 672
});
expect(component.instance().renderBackground(true)).toEqual(null);
});
});
describe('renderSection method', function () {
it('returns null when sectionClassName is undefined', function () {
mountTestComponent();
expect(component.instance().renderSection()).toEqual(null);
});
});
});