-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(ui-badge): migrate old Badge tests
Closes: INSTUI-3862
- Loading branch information
1 parent
4c3c332
commit d6da3ed
Showing
4 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
131 changes: 131 additions & 0 deletions
131
packages/ui-badge/src/Badge/__new-tests__/Badge.test.tsx
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,131 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015 - present Instructure, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
import React from 'react' | ||
import { render, screen } from '@testing-library/react' | ||
import { runAxeCheck } from '@instructure/ui-axe-check' | ||
|
||
import '@testing-library/jest-dom/extend-expect' | ||
import { Badge } from '../index' | ||
import type { BadgeProps } from '../props' | ||
|
||
const TEST_STRING = 'test' | ||
const renderBadge = (props: Partial<BadgeProps> = { count: 100 }) => { | ||
return render( | ||
<Badge {...props}> | ||
<button type="button">{TEST_STRING}</button> | ||
</Badge> | ||
) | ||
} | ||
|
||
describe('<Badge />', () => { | ||
it('should be accessible', async () => { | ||
const { container } = renderBadge() | ||
const axeCheck = await runAxeCheck(container) | ||
|
||
expect(axeCheck).toBe(true) | ||
}) | ||
|
||
it('should show the count', () => { | ||
const { container } = renderBadge({ count: 100 }) | ||
|
||
expect(container).toHaveTextContent('100') | ||
}) | ||
|
||
it('should truncate the count via countUntil', async () => { | ||
renderBadge({ count: 100, countUntil: 100 }) | ||
|
||
const truncatedCount = await screen.findByText('99 +') | ||
|
||
expect(truncatedCount).toBeVisible() | ||
}) | ||
|
||
it('should change position based on the placement prop', () => { | ||
const countOffset = '5px' | ||
const EXPECTED_PROP_VALUE = 'calc(-1 * 5px)' | ||
|
||
const { container } = renderBadge({ | ||
placement: 'bottom start', | ||
themeOverride: { countOffset } | ||
}) | ||
|
||
const badge = container.querySelector('[class$=-badge]') | ||
const badgeStyle = badge && getComputedStyle(badge) | ||
|
||
expect(badge).not.toBeNull() | ||
expect(badgeStyle).toHaveProperty('bottom', EXPECTED_PROP_VALUE) | ||
expect(badgeStyle).toHaveProperty('inset-inline-start', EXPECTED_PROP_VALUE) | ||
}) | ||
|
||
it('should not render a wrapper for a standalone Badge', () => { | ||
const { container } = renderBadge({ as: 'li', standalone: true }) | ||
const liElement = container.querySelector('li') | ||
|
||
expect(liElement).toBeNull() | ||
}) | ||
|
||
it('should render a wrapper for a NONE standalone Badge', () => { | ||
const { container } = renderBadge({ as: 'li', standalone: false }) | ||
const liElement = container.querySelector('li') | ||
|
||
expect(liElement).not.toBeNull() | ||
}) | ||
|
||
it('should change its output via the formatOutput prop', () => { | ||
const formatOutput = (formattedCount: string) => { | ||
return `${formattedCount}!` | ||
} | ||
|
||
renderBadge({ count: 15, formatOutput }) | ||
const badgeElement = screen.getByText('15!') | ||
|
||
expect(badgeElement).toBeInTheDocument() | ||
}) | ||
|
||
it('should render button child correctly', () => { | ||
const { container } = renderBadge() | ||
const childBtnElement = container.querySelector('button') | ||
|
||
expect(childBtnElement).toBeInTheDocument() | ||
expect(container).toHaveTextContent(TEST_STRING) | ||
}) | ||
|
||
it('should call elementRef function', () => { | ||
const refMock = jest.fn() | ||
const { container } = renderBadge({ elementRef: refMock }) | ||
|
||
expect(refMock).toHaveBeenCalledWith(container.firstChild) | ||
}) | ||
|
||
it('should show the count when type is count', () => { | ||
const { container } = renderBadge({ count: 100, type: 'count' }) | ||
|
||
expect(container).toHaveTextContent('100') | ||
}) | ||
|
||
it('should NOT show the count when type is notification', () => { | ||
const { container } = renderBadge({ count: 100, type: 'notification' }) | ||
|
||
expect(container).not.toHaveTextContent('100') | ||
}) | ||
}) |
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