use getBy query instead of queryBy when asserting for elements present in document (#2951)

* replace queryByText with getByText when asserting for presence of element

* use string instead of regex for line numbers

* cleanup test case of CodeSnippet
This commit is contained in:
Abhishek Jakhar
2020-10-19 14:40:56 +05:30
committed by GitHub
parent 782f3b3541
commit ac8d5d5c74
2 changed files with 15 additions and 10 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/core': patch
---
update the test cases of CodeSnippet component
@@ -20,10 +20,10 @@ import { wrapInTestApp } from '@backstage/test-utils';
import { CodeSnippet } from './CodeSnippet';
const JAVASCRIPT = `const greeting = "Hello";
const world = "World";
const greet = person => gretting + " " + person + "!";
const JAVASCRIPT = `
const greeting = "Hello";
const world = "World";
const greet = person => gretting + " " + person + "!";
`;
const minProps = {
@@ -48,20 +48,20 @@ describe('<CodeSnippet />', () => {
});
it('renders with line numbers', () => {
const { queryByText } = render(
const { getByText } = render(
wrapInTestApp(<CodeSnippet {...minProps} showLineNumbers />),
);
expect(queryByText(/1/)).toBeInTheDocument();
expect(queryByText(/2/)).toBeInTheDocument();
expect(queryByText(/3/)).toBeInTheDocument();
expect(getByText('1')).toBeInTheDocument();
expect(getByText('2')).toBeInTheDocument();
expect(getByText('3')).toBeInTheDocument();
});
it('copy code using button', async () => {
document.execCommand = jest.fn();
const rendered = render(
const { getByTitle } = render(
wrapInTestApp(<CodeSnippet {...minProps} showCopyCodeButton />),
);
const button = rendered.getByTitle('Text copied to clipboard');
const button = getByTitle('Text copied to clipboard');
fireEvent.click(button);
expect(document.execCommand).toHaveBeenCalled();
});