-
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.
test: increase coverage containedlist, layout and textinputskeleton (#…
- Loading branch information
1 parent
0557883
commit b6c9566
Showing
3 changed files
with
222 additions
and
0 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
103 changes: 103 additions & 0 deletions
103
packages/react/src/components/Layout/__tests__/Layout-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,103 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2024 | ||
* | ||
* 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 { render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { Layout, LayoutConstraint } from '../'; | ||
|
||
const prefix = 'cds'; | ||
|
||
describe('Layout', () => { | ||
it('should render a custom element when "as" prop is provided', () => { | ||
render(<Layout as="section">Content inside section</Layout>); | ||
const sectionElement = screen.getByText('Content inside section'); | ||
expect(sectionElement.tagName).toBe('SECTION'); | ||
}); | ||
|
||
it('should apply the correct size class for Layout', () => { | ||
const { container } = render(<Layout size="lg">Content</Layout>); | ||
expect(container.firstChild).toHaveClass(`${prefix}--layout--size-lg`); | ||
}); | ||
|
||
it('should apply the correct density class for Layout', () => { | ||
const { container } = render(<Layout density="condensed">Content</Layout>); | ||
expect(container.firstChild).toHaveClass( | ||
`${prefix}--layout--density-condensed` | ||
); | ||
}); | ||
|
||
it('should apply custom class name to Layout', () => { | ||
const { container } = render( | ||
<Layout className="custom-class">Content</Layout> | ||
); | ||
expect(container.firstChild).toHaveClass('custom-class'); | ||
}); | ||
|
||
it('should render children inside Layout', () => { | ||
render(<Layout>Child Content</Layout>); | ||
const child = screen.getByText('Child Content'); | ||
expect(child).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe('LayoutConstraint', () => { | ||
it('should render a custom element when "as" prop is provided for LayoutConstraint', () => { | ||
render( | ||
<LayoutConstraint as="article">Content inside article</LayoutConstraint> | ||
); | ||
const articleElement = screen.getByText('Content inside article'); | ||
expect(articleElement.tagName).toBe('ARTICLE'); | ||
}); | ||
|
||
it('should apply correct size constraints for LayoutConstraint', () => { | ||
const { container } = render( | ||
<LayoutConstraint size={{ min: 'sm', default: 'md', max: 'xl' }}> | ||
Content | ||
</LayoutConstraint> | ||
); | ||
expect(container.firstChild).toHaveClass( | ||
`${prefix}--layout-constraint--size__default-md` | ||
); | ||
expect(container.firstChild).toHaveClass( | ||
`${prefix}--layout-constraint--size__min-sm` | ||
); | ||
expect(container.firstChild).toHaveClass( | ||
`${prefix}--layout-constraint--size__max-xl` | ||
); | ||
}); | ||
|
||
it('should apply correct density constraints for LayoutConstraint', () => { | ||
const { container } = render( | ||
<LayoutConstraint | ||
density={{ min: 'normal', default: 'condensed', max: 'normal' }}> | ||
Content | ||
</LayoutConstraint> | ||
); | ||
expect(container.firstChild).toHaveClass( | ||
`${prefix}--layout-constraint--density__default-condensed` | ||
); | ||
expect(container.firstChild).toHaveClass( | ||
`${prefix}--layout-constraint--density__min-normal` | ||
); | ||
expect(container.firstChild).toHaveClass( | ||
`${prefix}--layout-constraint--density__max-normal` | ||
); | ||
}); | ||
|
||
it('should apply custom class name to LayoutConstraint', () => { | ||
const { container } = render( | ||
<LayoutConstraint className="custom-class">Content</LayoutConstraint> | ||
); | ||
expect(container.firstChild).toHaveClass('custom-class'); | ||
}); | ||
|
||
it('should render children inside LayoutConstraint', () => { | ||
render(<LayoutConstraint>Constraint Child Content</LayoutConstraint>); | ||
const child = screen.getByText('Constraint Child Content'); | ||
expect(child).toBeInTheDocument(); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
packages/react/src/components/TextInput/__tests__/TextInput.Skeleton-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,62 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2023 | ||
* | ||
* 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 TextInputSkeleton from '../TextInput.Skeleton'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
const prefix = 'cds'; | ||
|
||
describe('TextInputSkeleton', () => { | ||
it('should render the skeleton input with the default classes', () => { | ||
const { container } = render(<TextInputSkeleton />); | ||
const formItem = container.firstChild; | ||
|
||
expect(formItem).toHaveClass(`${prefix}--form-item`); | ||
expect( | ||
formItem.querySelector(`.${prefix}--skeleton.${prefix}--text-input`) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render the label skeleton by default', () => { | ||
const { container } = render(<TextInputSkeleton />); | ||
const labelSkeleton = container.querySelector( | ||
`.${prefix}--label.${prefix}--skeleton` | ||
); | ||
|
||
expect(labelSkeleton).toBeInTheDocument(); | ||
}); | ||
|
||
it('should not render the label skeleton if hideLabel is true', () => { | ||
const { container } = render(<TextInputSkeleton hideLabel />); | ||
const labelSkeleton = container.querySelector( | ||
`.${prefix}--label.${prefix}--skeleton` | ||
); | ||
|
||
expect(labelSkeleton).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should apply custom class names to the form item wrapper', () => { | ||
const { container } = render( | ||
<TextInputSkeleton className="custom-class" /> | ||
); | ||
const formItem = container.firstChild; | ||
|
||
expect(formItem).toHaveClass('custom-class'); | ||
expect(formItem).toHaveClass(`${prefix}--form-item`); | ||
}); | ||
|
||
it('should spread additional props onto the root element', () => { | ||
const { container } = render( | ||
<TextInputSkeleton data-testid="skeleton-input" /> | ||
); | ||
const formItem = container.firstChild; | ||
|
||
expect(formItem).toHaveAttribute('data-testid', 'skeleton-input'); | ||
}); | ||
}); |