-
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.
feat(react): refactor composed modal to functional component (#10060)
* feat(react): update composed modal to functional component * feat(react): refactor ComposedModal to functional component * chore(react): remove console.logs * fix(react): export ComposedModal under feature flag * Update packages/react/src/components/ComposedModal/next/ComposedModal.js Co-authored-by: Scott Strubberg <[email protected]> * Update packages/react/src/components/ComposedModal/next/ComposedModal.js Co-authored-by: Scott Strubberg <[email protected]> Co-authored-by: Scott Strubberg <[email protected]>
- Loading branch information
1 parent
3bd0462
commit 40f3768
Showing
5 changed files
with
764 additions
and
2 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
118 changes: 118 additions & 0 deletions
118
packages/react/src/components/ComposedModal/next/ComposedModal-test.js
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* 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 { mount } from 'enzyme'; | ||
import ComposedModal from './ComposedModal'; | ||
import { ModalHeader } from './ModalHeader'; | ||
import { ModalFooter } from '../ComposedModal'; | ||
import { settings } from 'carbon-components'; | ||
|
||
const { prefix } = settings; | ||
|
||
describe('<ComposedModal />', () => { | ||
let container; | ||
|
||
afterEach(() => { | ||
if (container && container.parentNode) { | ||
container.parentNode.removeChild(container); | ||
} | ||
container = null; | ||
}); | ||
|
||
it('renders', () => { | ||
const wrapper = mount(<ComposedModal open />); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
it('changes the open state upon change in props', () => { | ||
const wrapper = mount(<ComposedModal open />); | ||
|
||
expect( | ||
document.body.classList.contains('bx--body--with-modal-open') | ||
).toEqual(true); | ||
wrapper.setProps({ open: false }); | ||
expect( | ||
document.body.classList.contains('bx--body--with-modal-open') | ||
).toEqual(false); | ||
}); | ||
|
||
it('should change class of <body> upon open state', () => { | ||
const wrapper = mount(<ComposedModal open />); | ||
expect( | ||
document.body.classList.contains('bx--body--with-modal-open') | ||
).toEqual(true); | ||
wrapper.unmount(); | ||
expect( | ||
document.body.classList.contains('bx--body--with-modal-open') | ||
).toEqual(false); | ||
mount(<ComposedModal open={false} />); | ||
expect( | ||
document.body.classList.contains('bx--body--with-modal-open') | ||
).toEqual(false); | ||
}); | ||
|
||
it('calls onClick upon user-initiated closing', () => { | ||
const onClose = jest.fn(); | ||
const wrapper = mount( | ||
<ComposedModal open onClose={onClose}> | ||
<ModalHeader /> | ||
</ComposedModal> | ||
); | ||
const button = wrapper.find(`.${prefix}--modal-close`).first(); | ||
button.simulate('click'); | ||
expect( | ||
document.body.classList.contains('bx--body--with-modal-open') | ||
).toEqual(false); | ||
expect(onClose.mock.calls.length).toBe(1); | ||
}); | ||
|
||
it('provides a way to prevent upon user-initiated closing', () => { | ||
const onClose = jest.fn(() => false); | ||
const wrapper = mount( | ||
<ComposedModal open onClose={onClose}> | ||
<ModalHeader /> | ||
</ComposedModal> | ||
); | ||
const button = wrapper.find(`.${prefix}--modal-close`).first(); | ||
button.simulate('click'); | ||
expect( | ||
document.body.classList.contains('bx--body--with-modal-open') | ||
).toEqual(true); | ||
}); | ||
|
||
it('should focus on the primary actionable button in ModalFooter by default', () => { | ||
container = document.createElement('div'); | ||
container.id = 'container'; | ||
document.body.appendChild(container); | ||
mount( | ||
<ComposedModal open> | ||
<ModalFooter primaryButtonText="Save" /> | ||
</ComposedModal>, | ||
{ attachTo: document.querySelector('#container') } | ||
); | ||
expect( | ||
document.activeElement.classList.contains(`${prefix}--btn--primary`) | ||
).toEqual(true); | ||
}); | ||
|
||
it('should focus on the element that matches selectorPrimaryFocus', () => { | ||
container = document.createElement('div'); | ||
container.id = 'container'; | ||
document.body.appendChild(container); | ||
mount( | ||
<ComposedModal open selectorPrimaryFocus={`.${prefix}--modal-close`}> | ||
<ModalHeader label="Optional Label" title="Example" /> | ||
<ModalFooter primaryButtonText="Save" /> | ||
</ComposedModal>, | ||
{ attachTo: document.querySelector('#container') } | ||
); | ||
expect( | ||
document.activeElement.classList.contains(`${prefix}--modal-close`) | ||
).toEqual(true); | ||
}); | ||
}); |
Oops, something went wrong.