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

Disallow usages of innerHTML for no-container rule #889

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
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
36 changes: 30 additions & 6 deletions lib/rules/no-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
}

function isDisallowedContainerProperty(propertyName: string): boolean {
return propertyName === 'innerHTML';
}

return {
CallExpression(node) {
const callExpressionIdentifier = getDeepestIdentifierNode(node);
Expand All @@ -111,6 +115,20 @@ export default createTestingLibraryRule<Options, MessageIds>({
}
},

MemberExpression(node) {
if (
ASTUtils.isIdentifier(node.object) &&
node.object.name === containerName &&
ASTUtils.isIdentifier(node.property) &&
isDisallowedContainerProperty(node.property.name)
) {
context.report({
node,
messageId: 'noContainer',
});
}
},

VariableDeclarator(node) {
if (!node.init) {
return;
Expand Down Expand Up @@ -150,12 +168,18 @@ export default createTestingLibraryRule<Options, MessageIds>({
if (ASTUtils.isIdentifier(nodeValue)) {
containerName = nodeValue.name;
} else if (isObjectPattern(nodeValue)) {
nodeValue.properties.forEach(
(property) =>
isProperty(property) &&
ASTUtils.isIdentifier(property.key) &&
destructuredContainerPropNames.push(property.key.name)
);
nodeValue.properties.forEach((property) => {
if (isProperty(property) && ASTUtils.isIdentifier(property.key)) {
if (isDisallowedContainerProperty(property.key.name)) {
context.report({
node,
messageId: 'noContainer',
});
}

destructuredContainerPropNames.push(property.key.name);
}
});
}
} else if (ASTUtils.isIdentifier(node.id)) {
renderResultVarName = node.id.name;
Expand Down
186 changes: 186 additions & 0 deletions tests/lib/rules/no-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,46 @@ ruleTester.run(RULE_NAME, rule, {
code: `
import { otherRender } from 'somewhere-else'
const { container } = otherRender(<Example />);
container.innerHTML;
const button = container.querySelector('.btn-primary');
`,
},
{
code: `
const { container } = render(<Example />);
const newElement = document.createElement('div');
newElement.innerHTML;
`,
},
{
code: `
const { container } = render(<Example />);
const newElement = document.createElement('div');
newElement.firstChild;
`,
},
{
code: `
const { container } = render(<Example />);
container.firstChild;
`,
},
{
code: `
const { container: { firstChild } } = render(<Example />);
`,
},
...SUPPORTED_TESTING_FRAMEWORKS.map(
(testingFramework) =>
({
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import { render as renamed } from '${testingFramework}'
import { render } from 'somewhere-else'
const { container: { innerHTML } } = render(<Example />);
`,
} as const)
),
],
invalid: [
{
Expand Down Expand Up @@ -107,6 +144,20 @@ ruleTester.run(RULE_NAME, rule, {
},
],
},
{
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import { render } from 'test-utils'
const { container: { innerHTML } } = render(<Example />);
`,
errors: [
{
line: 3,
column: 15,
messageId: 'noContainer',
},
],
},
...SUPPORTED_TESTING_FRAMEWORKS.map(
(testingFramework) =>
({
Expand Down Expand Up @@ -146,6 +197,45 @@ ruleTester.run(RULE_NAME, rule, {
],
} as const)
),
...SUPPORTED_TESTING_FRAMEWORKS.map(
(testingFramework) =>
({
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import { render as testingRender } from '${testingFramework}'
const { container: renamed } = testingRender(<Example />);
renamed.innerHTML;
`,
errors: [
{
line: 4,
column: 9,
messageId: 'noContainer',
},
],
} as const)
),
...SUPPORTED_TESTING_FRAMEWORKS.map(
(testingFramework) =>
({
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import { render } from '${testingFramework}'

const setup = () => render(<Example />)

const { container } = setup()
container.innerHTML;
`,
errors: [
{
line: 7,
column: 9,
messageId: 'noContainer',
},
],
} as const)
),
{
code: `
const { container } = render(<Example />);
Expand Down Expand Up @@ -232,5 +322,101 @@ ruleTester.run(RULE_NAME, rule, {
},
],
},
{
code: `
const { container } = render(<Example />);
container.innerHTML;
`,
errors: [
{
line: 3,
column: 5,
messageId: 'noContainer',
},
],
},
{
code: `
const { container: alias } = render(<Example />);
alias.innerHTML;
`,
errors: [
{
line: 3,
column: 5,
messageId: 'noContainer',
},
],
},
{
code: `
const { container: { innerHTML } } = render(<Example />);
`,
errors: [
{
line: 2,
column: 11,
messageId: 'noContainer',
},
],
},
{
code: `
const { container: { innerHTML: alias } } = render(<Example />);
`,
errors: [
{
line: 2,
column: 11,
messageId: 'noContainer',
},
],
},
...SUPPORTED_TESTING_FRAMEWORKS.map(
(testingFramework) =>
({
settings: { 'testing-library/utils-module': 'test-utils' },
code: `
import { render } from '${testingFramework}'
const { container: { innerHTML } } = render(<Example />);
`,
errors: [
{
line: 3,
column: 15,
messageId: 'noContainer',
},
],
} as const)
),
{
settings: {
'testing-library/custom-renders': ['customRender', 'renderWithRedux'],
},
code: `
const { container } = renderWithRedux(<Example />);
container.innerHTML;
`,
errors: [
{
line: 3,
column: 9,
messageId: 'noContainer',
},
],
},
// {
// code: `
// const view = render(<Example />);
// view.container.innerHTML;
// `,
// errors: [
// {
// line: 3,
// column: 29,
// messageId: 'noContainer',
// },
// ],
// },
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Belco90 This comment-out test is the test that is failing now, that shows us that we are not yet handling the view.container.innerHTML case

Copy link
Member

Choose a reason for hiding this comment

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

I see. I need to go again through the utils for navigating the nodes, I think we had something to fix the problem described in the main description.

],
});