Coding task · JavaScript and React course · Module 13: Testing React

Practice: FuelGauge Component

React Testing Library looks for elements by role, just like assistive technologies. A bar without a role is a plain div for the test, and getByRole will not find it. role="progressbar" together with aria-valuenow, aria-valuemin and aria-valuemax tells what the level is, and role="alert" marks a message that has to be announced immediately. The test checks the thresholds 19 and 20, zero, refuelling up to 100% and burning fuel.

Task

Finish the fuel gauge so that the test finds it the way a screen reader user does. ___BLANK1___: isLow is true when the fuel level is lower than 20. ___BLANK2___: the bar has the progressbar role, so the test finds it with getByRole('progressbar', { name: 'Fuel level' }) (aria-label gives the name and aria-valuenow the level). ___BLANK3___: the low fuel warning has the alert role, which a screen reader announces at once. ___BLANK4___: the Refuel button adds 25 percentage points, but the level must not go above 100.