Adding dynamic data-testid attributes for easier testing
Nikolay Advolodkin
Founder @ UltimateQA | 150K engineers trained in 184 countries
?? Hey #Developers, let's solve a common challenge when testing components like grids! ??
?? The Challenge:?
You've got a dynamic grid of components, and you need a clean way to generate unique `data-testid` attributes for each element, making it easier to locate them in your tests.
? The Solution:?
Create a reusable custom hook that generates unique `data-testid` attributes for your components based on a prefix and an index.
?? Code Examples:
1. Create a custom hook `useTestIdGenerator.ts`:
2. Use the custom hook in your grid component:
Now, you've got unique `data-testid` attributes generated for each element in the grid! ?? This approach makes it super easy to test dynamic components and keep your codebase clean and maintainable. ??
??If you're not a developer, you can make this request to your developer. They will be happy to help.
Happy testing! ???? #DevTips #DeveloperAdvocate
Hardware / Software diagnostics | QA/QC | Budōka
6 个月Thanks for this, much appreciated