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

Better Component testing helpers #150

Closed
dsingleton opened this issue Jul 19, 2016 · 3 comments
Closed

Better Component testing helpers #150

dsingleton opened this issue Jul 19, 2016 · 3 comments

Comments

@dsingleton
Copy link
Contributor

Applications often want to write tests that assert against something in a component, in test mode a component will render a test version, containing the arguments passed to it as JSON (not fetching anything from static).

Currently apps tend to do a substring match on the JSON within the component wrapper, which is a bit hacky. Ideally there would be test helpers that can assert a specific argument was passed to the component, without having to parse the JSON out of the markup in each application.

eg,

assert_component('title', 'context', 'foo')

This requires writing helpers for a set of testing frameworks, but as we standardised our rails frontends that should be easier, and at the least we could add a helper for the most common one.

Heres an example for an application implementing these helpers itself: https://github.com/alphagov/government-frontend/blob/master/test/test_helper.rb#L24-L83

It would be nice if this was re-usable in other apps.

@tijmenb
Copy link
Contributor

tijmenb commented Sep 15, 2016

Frontend uses:

assert page.has_selector?(shared_component_selector('beta_label'))

@dsingleton
Copy link
Contributor Author

government-frontend has a selection of component asserts now: https://github.com/alphagov/government-frontend/blob/master/test/test_helper.rb#L39-L121

If this does get standardised that would be a good place to pick from.

@fofr
Copy link
Contributor

fofr commented Sep 27, 2017

This has been helpful in collections for testing arguments to components:

def within_static_component(component)
    within(shared_component_selector(component)) do
      component_args = JSON.parse(page.text).with_indifferent_access
      yield component_args
    end
  end

https://github.com/alphagov/collections/blob/61c18b335770a93b154686addca27313aea5737c/test/integration_test_helper.rb#L19-L24

Based on:
https://github.com/alphagov/government-frontend/blob/master/test/test_helper.rb#L91-L96

@tijmenb tijmenb closed this as completed Jun 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants