Skip to content

fix: not crash if getSuggestedQuery throw an exception #215

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

Closed
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
6 changes: 5 additions & 1 deletion src/lib/queryAdvise.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ export function getAllPossibileQueries(element) {
.filter((query) => query.type !== 'GENERIC')
.map((query) => {
const method = getFieldName(query.method);
return getSuggestedQuery(element, 'get', method);
try {
return getSuggestedQuery(element, 'get', method);
} catch (e) {
return undefined;
}
Comment on lines +114 to +118
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of having a try/catch inside a loop. Also, shouldn't this be fixed upstream?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if other queries don't throw an exception I want to show them

Copy link
Member

@smeijer smeijer Jun 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. And isn't this fixable in @testing-library/dom?

Copy link
Member Author

@marcosvega91 marcosvega91 Jun 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This problem is that it is a new usecase.

We can solve the problem anyway but I think is better to maintain this code to avoid other problem of this kind.

})
.filter((suggestedQuery) => suggestedQuery !== undefined)
.reduce((obj, suggestedQuery) => {
Expand Down
54 changes: 32 additions & 22 deletions src/lib/queryAdvise.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getQueryAdvise, getAllPossibileQueries } from './queryAdvise';

import { render } from '../../tests/test-utils';
const emptyObject = `
Object {
"data": Object {},
Expand Down Expand Up @@ -43,16 +43,27 @@ it('should return an empty object if element node is a malformed object', () =>
});

it('should add `screen.` on suggested query returned by getSuggestedQuery', () => {
const rootNode = document.createElement('div');
const element = document.createElement('button');
const result = getQueryAdvise({ rootNode, element });
const { container } = render(`<button />`);
const element = container.querySelector('button');
const result = getQueryAdvise({ rootNode: container, element });
expect(result.suggestion.expression).toStartWith('screen.');
});

it('should not throw error if something is wrong in @testing-library/dom', () => {
const { container } = render(`
<div id="a">One</div>
<div id="b">Two</div>
<input
type="text"
aria-labelledby="a b"
/>`);
const input = container.querySelector('input');
expect(() => getAllPossibileQueries(input)).not.toThrow();
});

it('[getAllPossibileQueries] should return an object with all possibile queries', () => {
const rootNode = document.createElement('div');
const element = document.createElement('button');
rootNode.appendChild(element);
const { container, rerender } = render(`<button />`);
const element = container.firstChild;
let suggestedQueries = getAllPossibileQueries(element);

expect(suggestedQueries).toMatchInlineSnapshot(`
Expand All @@ -68,21 +79,20 @@ it('[getAllPossibileQueries] should return an object with all possibile queries'
},
}
`);

rootNode.innerHTML = `
<label for="username">Username</label>
<input
id="username"
name="username"
placeholder="how should I call you?"
data-testid="uname"
title="enter your username"
alt="enter your username"
value="john-doe"
type="text"
/>
`;
const input = rootNode.querySelector('input');
rerender(`
<label for="username">Username</label>
<input
id="username"
name="username"
placeholder="how should I call you?"
data-testid="uname"
title="enter your username"
alt="enter your username"
value="john-doe"
type="text"
/>
`);
const input = container.querySelector('input');
suggestedQueries = getAllPossibileQueries(input);

expect(suggestedQueries).toMatchInlineSnapshot(`
Expand Down
19 changes: 19 additions & 0 deletions tests/test-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function render(html, { container = document.createElement('div') } = {}) {
container.innerHTML = html;
function rerender(newHtml) {
return render(newHtml, { container });
}
return { container, rerender };
}

function renderIntoDocument(html) {
return render(html, { container: document.body });
}

function cleanup() {
document.body.innerHTML = '';
}

afterEach(cleanup);

export { render, renderIntoDocument, cleanup };