UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

7 lines 3.33 kB
import React from'react';import{shallow}from'enzyme';import Checkbox from"./index";/** * Checked Icon * @returns {JSX} */var Checked=function Checked(){return React.createElement("div",null);};/** * Unchecked Icon * @returns {JSX} */var Unchecked=function Unchecked(){return React.createElement("div",null);};describe('<Checkbox />',function(){it('should render the checkbox with the label before the icon',function(){var wrapper=shallow(React.createElement(Checkbox,{label:React.createElement("span",null,"Test Label Deluxe"),labelPosition:"left",checkedIcon:React.createElement(Checked,null),uncheckedIcon:React.createElement(Unchecked,null),checked:false}));var expected=React.createElement("div",null,React.createElement("span",null,"Test Label Deluxe"),React.createElement(Unchecked,null));expect(wrapper).toMatchSnapshot();expect(wrapper.matchesElement(expected)).toBeTruthy();});it('should render the checkbox with the label after the icon',function(){var wrapper=shallow(React.createElement(Checkbox,{label:React.createElement("span",null,"Test Label Deluxe"),labelPosition:"right",checkedIcon:React.createElement(Checked,null),uncheckedIcon:React.createElement(Unchecked,null),checked:false}));var expected=React.createElement("div",null,React.createElement(Unchecked,null),React.createElement("span",null,"Test Label Deluxe"));expect(wrapper).toMatchSnapshot();expect(wrapper.matchesElement(expected)).toBeTruthy();});it('should render the unchecked icon if "checked" is false',function(){var wrapper=shallow(React.createElement(Checkbox,{checked:false,label:"Test Label Deluxe",checkedIcon:React.createElement(Checked,null),uncheckedIcon:React.createElement(Unchecked,null)}));expect(wrapper).toMatchSnapshot();expect(wrapper.find(Checked).length).toBe(0);expect(wrapper.find(Unchecked).length).toBe(1);});it('should render the unchecked icon if "checked" is false',function(){var wrapper=shallow(React.createElement(Checkbox,{checked:true,label:"Test Label Deluxe",checkedIcon:React.createElement(Checked,null),uncheckedIcon:React.createElement(Unchecked,null)}));expect(wrapper).toMatchSnapshot();expect(wrapper.find(Checked).length).toBe(1);expect(wrapper.find(Unchecked).length).toBe(0);});it('should call the callback with the inverted value',function(){var spy=jest.fn();var wrapper=shallow(React.createElement(Checkbox,{label:"Test Label Deluxe",checkedIcon:React.createElement(Checked,null),uncheckedIcon:React.createElement(Unchecked,null),checked:false,onCheck:spy}));wrapper.simulate('click');expect(spy).toHaveBeenCalledWith(true);});it('should render an <input> element if a name prop is provided',function(){var wrapper=shallow(React.createElement(Checkbox,{label:"Test Label Deluxe",checkedIcon:React.createElement(Checked,null),uncheckedIcon:React.createElement(Unchecked,null),defaultChecked:false,name:"myCheckbox"}));var input=wrapper.find('input');expect(input.length).toBe(1);expect(input.prop('name')).toEqual('myCheckbox');expect(input.prop('value')).toEqual(0);});it('should work as an uncontrolled input',function(){var wrapper=shallow(React.createElement(Checkbox,{label:"Test Label Deluxe",checkedIcon:React.createElement(Checked,null),uncheckedIcon:React.createElement(Unchecked,null),defaultChecked:false}));wrapper.simulate('click');expect(wrapper.state('checked')).toBe(true);});});