@carbon/ibm-cloud-cognitive-cdai
Version:
Carbon for Cloud & Cognitive CD&AI UI components
169 lines (168 loc) • 6.43 kB
JavaScript
//
// 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 { IdeCard } from '.';
import { mount } from 'enzyme';
import React from 'react';
import { idAttributeSelector } from '../../component_helpers/IDHelper';
import * as jth from '../../component_helpers/jest_test_helper_functions.js';
import { defaultProps, disabledCard, externalLinkCard, largeCard, linkCard, nodeContentCard, nodeContentCardNoContent, customCard, customCardNoRender, noCardType } from './test_assets/testProps.js';
describe('IdeCard 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(IdeCard, defaultProps, props));
component = test.component;
unmount = test.unmount;
};
afterEach(function () {
// if unmount defined, call it to clear up any mounted component
unmount && unmount();
});
it('Renders as expected with With basic properties provided', function () {
mountTestComponent(defaultProps);
expect(component.exists('ClickableTile')).toBe(true);
expect(component.exists(idAttributeSelector("ide-card-".concat(defaultProps.cardId)))).toBe(true);
expect(component.exists(idAttributeSelector("ide-card-".concat(defaultProps.cardId, "-card")))).toBe(true);
});
it('Renders a disabled card', function () {
mountTestComponent(null, disabledCard);
expect(component.exists('ide-card__disabled'));
});
it('Renders a large card', function () {
mountTestComponent(null, largeCard);
expect(component.exists('ide-card__large'));
});
it('Renders an external link card', function () {
mountTestComponent(null, externalLinkCard);
expect(component.exists('ide-card-container__standard'));
expect(component.find('.ide-card--card-overides').props().href).toBe(externalLinkCard.cardHref);
expect(component.find('.ide-card--card-overides').props().target).toBe(externalLinkCard.cardLinkTarget);
});
it('Renders a links card', function () {
mountTestComponent(null, linkCard);
expect(component.exists('ide-card-foobar-#started-Link'));
});
it('Renders a custom content card', function () {
mountTestComponent(null, customCard);
expect(component.exists('ide-card__standard'));
});
it('Renders a custom content card with a missing renderFunc', function () {
mountTestComponent(null, customCardNoRender);
expect(component.exists('ide-card__standard'));
});
it('Renders a node content card', function () {
mountTestComponent(null, nodeContentCard);
expect(component.exists('ide-card-heading-container'));
expect(component.exists('ide-card__standard'));
expect(component.exists('test--idecard-nodeContentCard'));
});
it('Renders a node content card without content', function () {
mountTestComponent(null, nodeContentCardNoContent);
expect(component.exists('ide-card-heading-container'));
expect(component.exists('ide-card__standard'));
});
it('Renders a card when no cardType is provided', function () {
mountTestComponent(null, noCardType);
expect(component.exists('ide-card--tile-text'));
});
it('Sets the onHover state to true onMouseEnter and false onMouseLeave', function () {
var card = mount(/*#__PURE__*/React.createElement(IdeCard, {
cardId: "id",
cardType: "text",
cardDisabled: true,
disabledText: "You don't have access to this instance",
cardContent: {
text: 'testText'
}
}));
expect(card.state('onHover')).toBe(false);
card.simulate('mouseenter');
expect(card.state('onHover')).toBe(true);
card.simulate('mouseleave');
expect(card.state('onHover')).toBe(false);
card.setProps({
cardDisabled: false
});
card.simulate('mouseenter');
expect(card.state('onHover')).toBe(false);
card.simulate('mouseleave');
expect(card.state('onHover')).toBe(false);
});
it('Calls `onInteract` when clicked', function () {
var onInteract = jest.fn();
var preventDefault = jest.fn();
var card = mount(/*#__PURE__*/React.createElement(IdeCard, {
cardId: "id",
cardType: "text",
cardHref: "/hello",
onInteract: onInteract,
cardContent: {
text: 'testText'
}
}));
card.find('a').simulate('click', {
preventDefault: preventDefault
});
expect(onInteract).toHaveBeenCalled();
expect(preventDefault).not.toHaveBeenCalled();
});
it('Does not call `onInteract` when clicked if the card is disabled', function () {
var onInteract = jest.fn();
var preventDefault = jest.fn();
var card = mount(/*#__PURE__*/React.createElement(IdeCard, {
cardDisabled: true,
cardId: "id",
cardType: "text",
cardHref: "/hello",
onInteract: onInteract,
cardContent: {
text: 'testText'
}
}));
card.find('a').simulate('click', {
preventDefault: preventDefault
});
expect(onInteract).not.toHaveBeenCalled();
expect(preventDefault).toHaveBeenCalled();
});
it('Prevents the default event when clicked with no href specified', function () {
var onInteract = jest.fn();
var preventDefault = jest.fn();
var card = mount(/*#__PURE__*/React.createElement(IdeCard, {
cardId: "id",
cardType: "text",
onInteract: onInteract,
cardContent: {
text: 'testText'
}
}));
card.find('a').simulate('click', {
preventDefault: preventDefault
});
expect(onInteract).toHaveBeenCalled();
expect(preventDefault).toHaveBeenCalled();
});
it('Prevents the default event if the card is disabled', function () {
var onInteract = jest.fn();
var preventDefault = jest.fn();
var card = mount(/*#__PURE__*/React.createElement(IdeCard, {
cardDisabled: true,
cardId: "id",
cardType: "text",
onInteract: onInteract,
cardContent: {
text: 'testText'
}
}));
card.find('a').simulate('click', {
preventDefault: preventDefault
});
expect(onInteract).not.toHaveBeenCalled();
expect(preventDefault).toHaveBeenCalled();
});
});