diff --git a/client/src/controllers/ActionController.test.js b/client/src/controllers/ActionController.test.js index c4aa634d5e..d559896ace 100644 --- a/client/src/controllers/ActionController.test.js +++ b/client/src/controllers/ActionController.test.js @@ -22,6 +22,7 @@ describe('ActionController', () => { { ...Object.getOwnPropertyDescriptors(oldWindowLocation), assign: { configurable: true, value: jest.fn() }, + reload: { configurable: true, value: jest.fn() }, }, ); }); @@ -112,10 +113,7 @@ describe('ActionController', () => { it('should call click method when button is clicked via Stimulus action', () => { const btn = document.getElementById('button'); - const clickMock = jest.fn(); - HTMLButtonElement.prototype.click = clickMock; - - btn.addEventListener('some-event', btn.click()); + const clickMock = jest.spyOn(HTMLButtonElement.prototype, 'click'); const event = new CustomEvent('some-event'); btn.dispatchEvent(event); @@ -124,6 +122,24 @@ describe('ActionController', () => { }); }); + describe('reload method', () => { + beforeEach(async () => { + await setup(` + `); + }); + + it('should reload the page', () => { + document.getElementById('button').click(); + expect(window.location.reload).toHaveBeenCalledTimes(1); + }); + }); + describe('redirect method', () => { beforeEach(async () => { await setup(` diff --git a/client/src/controllers/ActionController.ts b/client/src/controllers/ActionController.ts index a0b8822131..290af5a612 100644 --- a/client/src/controllers/ActionController.ts +++ b/client/src/controllers/ActionController.ts @@ -119,6 +119,13 @@ export class ActionController extends Controller< navigator.sendBeacon(this.urlValue, new FormData(this.createFormElement())); } + /** + * Reload the browser. + */ + reload() { + window.location.reload(); + } + /** * Trigger a redirect based on the custom event's detail, the Stimulus param * or finally check the controlled element for a value to use.