-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into null-check-getinteractivecontent
- Loading branch information
Showing
3 changed files
with
42 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,7 +91,7 @@ | |
to-fast-properties "^2.0.0" | ||
|
||
"@carbon/colors@file:../..": | ||
version "10.35.0" | ||
version "11.9.0" | ||
|
||
"@hapi/[email protected]": | ||
version "5.0.1" | ||
|
@@ -1195,9 +1195,9 @@ debug@^2.2.0, debug@^2.3.3: | |
ms "2.0.0" | ||
|
||
decode-uri-component@^0.2.0: | ||
version "0.2.0" | ||
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545" | ||
integrity sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU= | ||
version "0.2.2" | ||
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.2.tgz#e69dbe25d37941171dd540e024c444cd5188e1e9" | ||
integrity sha512-FqUYQ+8o158GyGTrMFJms9qh3CqTKvAqgqsTnkLI8sKu0028orqBhxNMFkFen0zGyg6epACD32pjVk58ngIErQ== | ||
|
||
decompress-response@^4.2.0: | ||
version "4.2.1" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,66 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* Copyright IBM Corp. 2022 | ||
* | ||
* 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 React from 'react'; | ||
import Switch from '../Switch'; | ||
import { shallow } from 'enzyme'; | ||
|
||
const prefix = 'cds'; | ||
import Switch from './Switch'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
describe('Switch', () => { | ||
describe('component rendering', () => { | ||
const buttonWrapper = shallow( | ||
<Switch kind="button" icon={<svg />} text="test" /> | ||
); | ||
describe('renders as expected - Component API', () => { | ||
it('should spread extra props onto outermost element', () => { | ||
const { container } = render(<Switch data-testid="test-id" />); | ||
|
||
it('should render a button when kind is button', () => { | ||
expect(buttonWrapper.is('button')).toEqual(true); | ||
expect(container.firstChild).toHaveAttribute('data-testid', 'test-id'); | ||
}); | ||
|
||
it('should have the expected text', () => { | ||
expect(buttonWrapper.text()).toEqual('test'); | ||
}); | ||
it('should support a custom `className` prop on the outermost element', () => { | ||
const { container } = render(<Switch className="custom-class" />); | ||
|
||
it('label should have the expected class', () => { | ||
const className = `${prefix}--content-switcher__label`; | ||
expect(buttonWrapper.find('span').hasClass(className)).toEqual(true); | ||
expect(container.firstChild).toHaveClass('custom-class'); | ||
}); | ||
|
||
it('should have the expected class', () => { | ||
const cls = `${prefix}--content-switcher-btn`; | ||
it('should respect disabled prop', () => { | ||
render(<Switch disabled />); | ||
|
||
expect(buttonWrapper.hasClass(cls)).toEqual(true); | ||
expect(screen.getByRole('tab')).toHaveAttribute('disabled'); | ||
}); | ||
|
||
it('should not have selected class', () => { | ||
const selectedClass = `${prefix}--content-switcher--selected`; | ||
it('should call onClick when expected', () => { | ||
const onClick = jest.fn(); | ||
render(<Switch text="First section" onClick={onClick} />); | ||
|
||
userEvent.click(screen.getByText('First section')); | ||
|
||
expect(buttonWrapper.hasClass(selectedClass)).toEqual(false); | ||
expect(onClick).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should have a selected class when selected is set to true', () => { | ||
const selected = true; | ||
it('should call onKeyDown when expected', () => { | ||
const onKeyDown = jest.fn(); | ||
render(<Switch text="First section" onKeyDown={onKeyDown} />); | ||
|
||
buttonWrapper.setProps({ selected }); | ||
userEvent.type(screen.getByText('First section'), 'enter'); | ||
|
||
expect( | ||
buttonWrapper.hasClass(`${prefix}--content-switcher--selected`) | ||
).toEqual(true); | ||
expect(onKeyDown).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('events', () => { | ||
const buttonOnClick = jest.fn(); | ||
const linkOnClick = jest.fn(); | ||
const buttonOnKey = jest.fn(); | ||
const linkOnKey = jest.fn(); | ||
const index = 1; | ||
const name = 'first'; | ||
const text = 'test'; | ||
|
||
const buttonWrapper = shallow( | ||
<Switch | ||
index={index} | ||
name={name} | ||
kind="button" | ||
onClick={buttonOnClick} | ||
onKeyDown={buttonOnKey} | ||
text={text} | ||
/> | ||
); | ||
|
||
const linkWrapper = shallow( | ||
<Switch | ||
index={index} | ||
name={name} | ||
kind="anchor" | ||
onClick={linkOnClick} | ||
onKeyDown={linkOnKey} | ||
text={text} | ||
/> | ||
); | ||
|
||
it('should invoke button onClick handler', () => { | ||
buttonWrapper.simulate('click', { preventDefault() {} }); | ||
expect(buttonOnClick).toHaveBeenCalledWith({ index, name, text }); | ||
it('should respect selected prop', () => { | ||
render(<Switch selected />); | ||
|
||
expect(screen.getByRole('tab')).toHaveClass( | ||
'cds--content-switcher--selected' | ||
); | ||
expect(screen.getByRole('tab')).toHaveAttribute('aria-selected', 'true'); | ||
}); | ||
|
||
it('should invoke link onClick handler', () => { | ||
linkWrapper.simulate('click', { preventDefault() {} }); | ||
expect(buttonOnClick).toHaveBeenCalledWith({ index, name, text }); | ||
it('should respect text prop', () => { | ||
render(<Switch text="First section" />); | ||
|
||
expect(screen.getByText('First section')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |