UNPKG

@carbon/ibm-cloud-cognitive-cdai

Version:
64 lines (63 loc) 3.65 kB
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. // import IdeNavigation from './IdeNavigation'; import { navigationCoreProps } from './test_assets/testProps.js'; import { idAttributeSelector } from '../../../component_helpers/IDHelper'; import * as jth from '../../../component_helpers/jest_test_helper_functions.js'; describe('IdeNavigation unit tests', 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(IdeNavigation, defaultProps, props)); component = test.component; unmount = test.unmount; }; afterEach(function () { // if unmount defined, call it to clear up any mounted component unmount && unmount(); }); describe('view tests', function () { it('renders the expected structure with no props provided', function () { mountTestComponent(); expect(component.getElements()).toMatchSnapshot(); }); it('renders the expected structure with all provided', function () { mountTestComponent(_objectSpread({}, navigationCoreProps)); expect(component.getElements()).toMatchSnapshot(); }); it('adds the expected classes when the side nav is hidden via prop', function () { mountTestComponent({ showFirstLevel: false }); expect(component.find(idAttributeSelector('IdeNavigation--left-nav')).hasClass('ide-navigation__hide-nav')).toBe(true); expect(component.find(idAttributeSelector('IdeNavigation--content')).hasClass('ide-navigation--content__hide-nav')).toBe(true); }); }); describe('function tests', function () { it('toggle() when invoked flips the expanded state', function () { mountTestComponent(); var componentInstance = component.instance(); var startingExpandedState = componentInstance.state.expanded; componentInstance.toggle(); expect(componentInstance.state.expanded).toBe(!startingExpandedState); }); it('if controlled, the parent component dictates expanded state, and gets called on navigation interaction', function () { var mockToggle = jest.fn(); mountTestComponent({ expanded: true, onToggle: mockToggle }); expect(component.find(idAttributeSelector('IdeNavigation--left-nav')).props().expanded).toBe(true); // as we have provided it component.find(idAttributeSelector('IdeNavigation--left-nav-toggle')).props().onToggle(); // trigger the callback expect(mockToggle).toHaveBeenCalled(); // and confirm our callback is invoked on toggle }); }); });