Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: update Code tests to use react testing library #1352

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 0 additions & 86 deletions packages/react/__tests__/src/components/Code/index.js

This file was deleted.

87 changes: 87 additions & 0 deletions packages/react/src/components/Code/Code.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React from 'react';
import { render } from '@testing-library/react';
import { axe } from 'jest-axe';
import { createSandbox } from 'sinon';
import Code from './';

const sandbox = createSandbox();

beforeEach(() => {
global.ResizeObserver = global.ResizeObserver || (() => null);
sandbox.stub(global, 'ResizeObserver').callsFake((listener) => {
listener();
return {
observe: sandbox.stub(),
disconnect: sandbox.stub()
};
});
});

afterEach(() => {
sandbox.restore();
});

test('should render Code block', () => {
const { container } = render(
<Code language="javascript">{`var some = "javascript"`}</Code>
);
expect(container.querySelector('pre')).toHaveClass('Code');
expect(container.querySelector('pre')).not.toHaveClass('Code--scrollable');
expect(container.querySelector('pre')).toHaveTextContent(
'var some = "javascript"'
);
});

test('should set tabIndex when element is scrollable', () => {
// Mock the specific state expected when the tab should be scrollable
sandbox.stub(global.HTMLPreElement.prototype, 'clientWidth').value(123);
sandbox.stub(global.HTMLPreElement.prototype, 'scrollWidth').value(456);

const { container } = render(
<Code language="javascript" scrollable>{`var some = "javascript"`}</Code>
);
expect(container.querySelector('pre')).toHaveClass('Code--scrollable');
expect(container.querySelector('pre')).toHaveAttribute('tabIndex', '0');
});

test('should not set tabIndex when element is not scrollable', () => {
// Mock the specific state expected when the tab should not be scrollable
sandbox.stub(global.HTMLPreElement.prototype, 'clientWidth').value(123);
sandbox.stub(global.HTMLPreElement.prototype, 'scrollWidth').value(123);

const { container } = render(
<Code language="javascript" scrollable>{`var some = "javascript"`}</Code>
);
expect(container.querySelector('pre')).toHaveClass('Code--scrollable');
expect(container.querySelector('pre')).not.toHaveAttribute('tabIndex');
});

test('should support className prop', () => {
const { container } = render(
<Code
language="javascript"
className="bananas"
>{`var some = "javascript"`}</Code>
);
expect(container.querySelector('pre')).toHaveClass('Code', 'bananas');
});

test('should return no axe violations', async () => {
const { container } = render(
<Code language="javascript">{`var some = "javascript"`}</Code>
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});

test('should return no axe violations when scrollable', async () => {
// Mock the specific state expected when the tab should be scrollable
sandbox.stub(global.HTMLPreElement.prototype, 'clientWidth').value(123);
sandbox.stub(global.HTMLPreElement.prototype, 'scrollWidth').value(456);

const { container } = render(
<Code language="javascript" scrollable>{`var some = "javascript"`}</Code>
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
Loading