question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attaching data attributes doesn't work in test

See original GitHub issue

As explained here, I’m trying to attach a data attribute to a styled component, but it doesn’t work.

const Progress = styled(CircularProgress).attrs({
  'data-testid': 'progress'
})`
  display: inline-flex;
  vertical-align: middle;
`;
it('shows a circular progress when passing the loading prop', async () => {
  const { getByTestId } = customRender(<SubmitButton loading>Test</SubmitButton>);
  const progress = await waitForElement(() => getByTestId('progress'));
  expect(progress).toBeInTheDocument();
});

Passes the test: <div data-testid="progress"><Progress color={color} /></div> Doesn’t pass the test (cannot find the data attribute): <Progress color={color} />

It doesn’t work either if I pass the attribute inline: <Progress color={color} data-testid="progress" />

Tested with Styled Components v4.0.3

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
probablyupcommented, Nov 15, 2018

Feel free to ask further questions on stack overflow or our spectrum community, Github Issues should just be for bug reports and feature requests. Thanks.

1reaction
probablyupcommented, Nov 15, 2018

@gazpachu you aren’t spreading the props in your Comp component.

const Comp = props => <div {...props}>Hello</div>;

Read more comments on GitHub >

github_iconTop Results From Across the Web

Property injection on attributes not working from a unit test
Filter attribute property injection works as part of the MVC/ASP.NET pipeline, not as something available as a standalone feature.
Read more >
Using data attributes - Learn web development | MDN
JavaScript access​​ To get a data attribute through the dataset object, get the property by the part of the attribute name after data-...
Read more >
Test Data in Attribute - CodeProject
Attached the test data with method for regression testing by declaring ref type in attribute.
Read more >
How to Use HTML Data Attributes - DEV Community ‍ ‍
Any HTML element can have any number of data attributes added to its ... “badges” data attached, we could check that data attribute...
Read more >
Strategies for working with data attributes in vanilla JavaScript
DO NOT implement this!) document.addEventListener('click', function ( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found