wimdeblauwe / htmx-spring-boot

Spring Boot and Thymeleaf helpers for working with htmx
Apache License 2.0
497 stars 48 forks source link

Expose HtmxResponse internals for testing verification #30

Closed checketts closed 11 months ago

checketts commented 1 year ago

I'm adding MockMvc tests for some of my Htmx based endpoints and it currently isn't possible to write an 'expects' block against HtmxResponse templates since all the getters are package private.

We could: 1- Make the getters public so callers could pull out internals to assert against them 2- Add a testing support library and matcher that is in the same package as HtmxResponse so it has access to the internals,

I favor option #2 but since the repo isn't setup for multiple modules, it seems like it would be a large refactor to add it in. Would you like me to attempt that?

wimdeblauwe commented 1 year ago

Can you elaborate how you see this working? I would personally use Cypress or Playwright for testing the HTML, I believe it is a lot more closer to testing how things actually work without having to know anything about the internals.

checketts commented 1 year ago

My testers have been using Cypress for testing the HTML. However, the logic they are trying to verify is around forms. For example form validation and enabling/disabling parts.

One validation approach we are considering would be with OOB templates to show validation outside the form (via a toast popup or something)

Cypress testing makes it impossible to mock out the data used in the validation (it hooks into the response, too late in the call, so it would just verify HTMX and not the application logic).

By hooking into the MockMVC mechanism, we've been able to add tests ensuring the field are enabled/disabled properly based on the inputs results (since that logic is in Thymeleaf) and validation. (For example a validation check for if a username already exists in the system) Note we've done all that via the xpath and html response. No change is required for those.

However, the points we can't do are like: expects {view { name("myTemplate") } } (since the HtmxReponse doesn't populate the view name), nor can we detect the multiple templates in the OOB response.

wimdeblauwe commented 1 year ago

Cypress testing makes it impossible to mock out the data

If you use Cypress for end-to-end testing, then mocking is not needed I think?

checketts commented 1 year ago

So previously with our Angular apps, there was lots of logic in the frontend app so we would use Cypress to mock out a server's returned JSON to the front end. That allowed us to populate and simulate complex forms/data/validation.

Thankfully that is no longer needed as much with HTMX. So it appears we'll use Cypress for full end-2-end testing.

Mocking is still needed in cases where validation in checking if a selected user has certain attributes (age, gender, married, etc) before proceeding further in the form. Instead of hard coding users that change (married users become single, age changes over time, etc) we use mocking.

xhaggi commented 12 months ago

This is fixed by #73

xhaggi commented 11 months ago

@wimdeblauwe if I am correct you can close this.

wimdeblauwe commented 11 months ago

@checketts ok for you?

checketts commented 11 months ago

Yup