Proposal: rspec helpers for GOV.UK services #1
Replies: 3 comments 10 replies
-
@peteryates @paulrobertlloyd @nataliecarey @carlosmartinez any thoughts on this? ⬆️ |
Beta Was this translation helpful? Give feedback.
-
I love the idea of this. It's something I've considered a few times too.
I don't know if it needs to be tied to govuk-components because some of these would probably apply to the form builder too (like the error summary) and I'm sure we'd want to make other form-based assertions like checking fields have the right label, hint, legend etc. One huge strength of RSpec is that it allows for a slightly more conversational approach. Without too much additional work we could do something like this expect(page).to summarise(key: "Name", value: "Jo Bloggs")
# could be expressed as the equivalent of:
expect(page).to summarise("Name").with("Jo Bloggs")
# or sticking to the class names in the design system:
expect(page).to summarise_key("Name").with_value("Jo Bloggs")
# and possibly, by wrapping `#within` from our matcher:
expect(page).to have_a_summary_card_titled("Lead tenant").with_items do
expect(page).to summarise_key("Name").with_value("Jo Bloggs")
expect(page).to summarise_key("Email").with_value("[email protected]")
end |
Beta Was this translation helpful? Give feedback.
-
Great idea @frankieroberto. Will take a closer look at the initial implementation this afternoon. |
Beta Was this translation helpful? Give feedback.
-
Hi all,
This repo (and the initial code) is exploring an idea for a set of helpers that make it easier to write automated tests for services using rspec, whilst also helping to improve accessibility and usability.
Rationale
Generally, automated tests for GOV.UK services don't need any special helpers, as most components are just regular HTML features, so you can use all the standard rspec helpers like:
However, there are some GOV.UK components which are more custom, and so require a bit more work to test:
These can end up with tests that are harder to read:
Much nicer is to have a helper which lets the test focus on the content and not the markup:
In addition to making tests easier to read and write, the tests could also help to ensure that some common accessibility and usability issues don’t get missed, such as making sure that "Change" links have visually-hidden (or visible) disambiguation words.
Rather than exhaustively testing for every possible implementation error, the tests would focus on the mistakes that are most common in the real world, from our own experience and things like external accessibility audits.
Initial sketch
I’ve started by sketching out 3 helpers:
summarise
This is intended for checking summary lists and is extracted from some code I originally wrote for Apply.
It supports keys and values, plus an optional single action per row (in theory you can have multiple actions per row, but I've never seen this done in practice):
Currently you'd use a single assertion per row, and there’s no way to assert a particular order - but I’m not sure that order is something useful to test anyway?
summarise_errors
This checks the Error summary component, and also that it’s implemented correctly.
This tests the follow:
Error:
prefix in the title taghave_descriptive_links
This is a more speculative one that I’m less sure about, as it’s so general and therefore less likely that you'd remember to test it for every page. But perhaps it's still useful?
This tests that:
Possibly it could also test that there aren’t 2 links to the same page, which is ideally avoided, but I’m less sure about that as it's sometimes inevitable (eg a "Back" link going to the same page as a link in a navigation bar).
Open questions
Beta Was this translation helpful? Give feedback.
All reactions