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

Test Hx-Trigger header #161

Open
yagodaoud opened this issue Jan 6, 2025 · 1 comment
Open

Test Hx-Trigger header #161

yagodaoud opened this issue Jan 6, 2025 · 1 comment

Comments

@yagodaoud
Copy link

yagodaoud commented Jan 6, 2025

I've got this simple controller route that returns a fragment with a trigger so my front-end can handle the outcome of the request:

    @HxRequest
    @HxTrigger("historyCreated")
    @PostMapping
    public ModelAndView createHistory(@ModelAttribute EmployeeHistoryDTO employeeHistoryDTO, Model model) {
        employeeHistoryService.create(employeeHistoryDTO);

        Employee employee = (Employee) employeeService.getById(employeeHistoryDTO.getEmployee().getId());
        model.addAttribute("employee", employee);

        ModelAndView modelAndView = new ModelAndView( "employee-detail :: employeeHistory");
        modelAndView.addAllObjects(model.asMap());

        return modelAndView;
    }

How can i test if it's sending the header correctly with junit? I tried using a response object to see its headers but there's nothing there.

@checketts
Copy link
Collaborator

checketts commented Jan 6, 2025

Are you using Spring and MockMVC in your test? There are tests in this codebase that validates headers. I would recommend that approach: https://github.com/wimdeblauwe/htmx-spring-boot/blob/main/htmx-spring-boot/src/test/java/io/github/wimdeblauwe/htmx/spring/boot/mvc/HtmxResponseHandlerMethodArgumentResolverTest.java#L32

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

2 participants