Skip to content

Commit

Permalink
chore(chat-button-toggle-skeleton): test updates (#17440)
Browse files Browse the repository at this point in the history
  • Loading branch information
ariellalgilmore authored Oct 9, 2024
1 parent f10cb92 commit c5ce4eb
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* 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 { render, screen } from '@testing-library/react';
import React from 'react';
import ChatButton from '../ChatButton';

describe('ChatButton', () => {
it('should support rendering elements within the ChatButton through the `children` prop', () => {
render(
<ChatButton>
<span>child</span>
</ChatButton>
);
expect(screen.getByText('child')).toBeInTheDocument();
});

it('should support a custom className on the outermost element', () => {
const { container } = render(
<ChatButton className="custom-class">test</ChatButton>
);
expect(container.firstChild).toHaveClass('custom-class');
});

it('should use the disabled prop to set disabled on the <ChatButton>', () => {
const { rerender } = render(<ChatButton>test</ChatButton>);
expect(screen.getByRole('button')).toBeEnabled();

rerender(<ChatButton disabled>test</ChatButton>);
expect(screen.getByRole('button')).toBeDisabled();
});

it.each([
['primary', 'cds--btn'],
['secondary', 'cds--btn--secondary'],
['ghost', 'cds--btn--ghost'],
['danger', 'cds--btn--danger'],
['tertiary', 'cds--btn--tertiary'],
])(
'should set the expected classes for the ChatButton of kind: `%s`',
(kind, className) => {
render(
<ChatButton className={className} kind={kind}>
test
</ChatButton>
);
expect(screen.getByText('test')).toHaveClass(className);
}
);

it.each([
['sm', 'cds--btn--sm'],
['md', 'cds--btn--md'],
['lg', 'cds--btn--lg'],
])(
'should set the expected classes for the ChatButton of size: `%s`',
(size, className) => {
render(
<ChatButton className={className} size={size}>
test
</ChatButton>
);
expect(screen.getByText('test')).toHaveClass(className);
}
);

it('should set kind=ghost and size=sm if isQuickAction is set on the <ChatButton>', () => {
render(<ChatButton isQuickAction>test</ChatButton>);
expect(screen.getByText('test')).toHaveClass('cds--btn--ghost');
expect(screen.getByText('test')).toHaveClass('cds--btn--sm');
});

it('should not allow sizes larger than lg on the <ChatButton>', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});

render(<ChatButton size="xl" />);

try {
expect(spy).toHaveBeenCalled();
} finally {
spy.mockRestore();
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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 { render, screen } from '@testing-library/react';
import React from 'react';
import ChatButtonSkeleton from '../ChatButton.Skeleton';

describe('ButtonSkeleton', () => {
it.each([
['sm', 'cds--layout--size-sm'],
['md', 'cds--layout--size-md'],
['lg', 'cds--layout--size-lg'],
])(
'should set the expected classes for the size: `%s`',
(size, className) => {
render(<ChatButtonSkeleton data-testid="test" size={size} />);
expect(screen.getByTestId('test')).toHaveClass(className);
}
);

it('should support a custom className on the outermost element', () => {
const { container } = render(<ChatButtonSkeleton className="test" />);
expect(container.firstChild).toHaveClass('test');
});

it('should spread props onto the outermost element', () => {
const { container } = render(<ChatButtonSkeleton data-testid="test" />);
expect(container.firstChild).toHaveAttribute('data-testid', 'test');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import React from 'react';
import Toggle from './Toggle';
import Toggle from '../Toggle';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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 { render } from '@testing-library/react';
import React from 'react';
import ToggleSkeleton from '../Toggle.Skeleton';

describe('ToggleSkeleton', () => {
it('should support a custom className on the outermost element', () => {
const { container } = render(<ToggleSkeleton className="test" />);
expect(container.firstChild).toHaveClass('test');
});

it('should spread props onto the outermost element', () => {
const { container } = render(<ToggleSkeleton data-testid="test" />);
expect(container.firstChild).toHaveAttribute('data-testid', 'test');
});
});

0 comments on commit c5ce4eb

Please sign in to comment.