UNPKG

@carbon/ibm-cloud-cognitive-cdai

Version:
134 lines (130 loc) 5.68 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; // // 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. // import { IdeHTTPErrors } from '.'; import * as jth from '../../component_helpers/jest_test_helper_functions.js'; 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, _defineProperty({}, 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, _defineProperty({}, 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, _defineProperty({}, 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); }); }); });