|
@@ -3,9 +3,23 @@ import { ActionController } from './ActionController';
|
|
|
|
|
|
describe('ActionController', () => {
|
|
|
let app;
|
|
|
+ const oldWindowLocation = window.location;
|
|
|
+
|
|
|
+ beforeAll(() => {
|
|
|
+ delete window.location;
|
|
|
+
|
|
|
+ window.location = Object.defineProperties(
|
|
|
+ {},
|
|
|
+ {
|
|
|
+ ...Object.getOwnPropertyDescriptors(oldWindowLocation),
|
|
|
+ assign: { configurable: true, value: jest.fn() },
|
|
|
+ },
|
|
|
+ );
|
|
|
+ });
|
|
|
|
|
|
afterEach(() => {
|
|
|
app?.stop();
|
|
|
+ jest.clearAllMocks();
|
|
|
});
|
|
|
|
|
|
describe('post method', () => {
|
|
@@ -70,4 +84,62 @@ describe('ActionController', () => {
|
|
|
expect(clickMock).toHaveBeenCalled();
|
|
|
});
|
|
|
});
|
|
|
+
|
|
|
+ describe('redirect method', () => {
|
|
|
+ beforeEach(() => {
|
|
|
+ document.body.innerHTML = `
|
|
|
+ <select name="url" data-controller="w-action" data-action="change->w-action#redirect">
|
|
|
+ <option value="http://localhost/place?option=1">1</option>
|
|
|
+ <option value="http://localhost/place?option=2" selected>2</option>
|
|
|
+ </select>
|
|
|
+ `;
|
|
|
+
|
|
|
+ app = Application.start();
|
|
|
+ app.register('w-action', ActionController);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should have a redirect method that falls back to any element value', () => {
|
|
|
+ const select = document.querySelector('select');
|
|
|
+
|
|
|
+ expect(window.location.href).toEqual('http://localhost/');
|
|
|
+ expect(window.location.assign).not.toHaveBeenCalled();
|
|
|
+
|
|
|
+ select.dispatchEvent(new CustomEvent('change'));
|
|
|
+
|
|
|
+ expect(window.location.assign).toHaveBeenCalledWith(
|
|
|
+ 'http://localhost/place?option=2',
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should allow redirection via the custom event detail', () => {
|
|
|
+ const select = document.querySelector('select');
|
|
|
+
|
|
|
+ expect(window.location.href).toEqual('http://localhost/');
|
|
|
+ expect(window.location.assign).not.toHaveBeenCalled();
|
|
|
+
|
|
|
+ select.dispatchEvent(
|
|
|
+ new CustomEvent('change', { detail: { url: '/its/in/the/detail/' } }),
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(window.location.assign).toHaveBeenCalledWith(
|
|
|
+ '/its/in/the/detail/',
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should allow redirection via the Stimulus param approach', () => {
|
|
|
+ const select = document.querySelector('select');
|
|
|
+
|
|
|
+ expect(window.location.href).toEqual('http://localhost/');
|
|
|
+ expect(window.location.assign).not.toHaveBeenCalled();
|
|
|
+
|
|
|
+ select.dataset.wActionUrlParam = '/check/out/the/param/';
|
|
|
+
|
|
|
+ select.dispatchEvent(
|
|
|
+ new CustomEvent('change', { detail: { url: '/its/in/the/detail/' } }),
|
|
|
+ );
|
|
|
+ expect(window.location.assign).toHaveBeenCalledWith(
|
|
|
+ '/check/out/the/param/',
|
|
|
+ );
|
|
|
+ });
|
|
|
+ });
|
|
|
});
|