Add reload() method to ActionController

pull/12185/head
Sage Abdullah 2024-07-01 14:12:47 +01:00 zatwierdzone przez Thibaud Colas
rodzic 0cf68697c0
commit 1c2616623d
2 zmienionych plików z 27 dodań i 4 usunięć

Wyświetl plik

@ -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(`
<button
id="button"
data-controller="w-action"
data-action="click->w-action#reload"
>
Reload
</button>`);
});
it('should reload the page', () => {
document.getElementById('button').click();
expect(window.location.reload).toHaveBeenCalledTimes(1);
});
});
describe('redirect method', () => {
beforeEach(async () => {
await setup(`

Wyświetl plik

@ -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.