Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Apr 11, 2024
1 parent 39efe67 commit 35fc2f5
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 17 deletions.
104 changes: 104 additions & 0 deletions assets/js/__tests__/ujs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import fetchMock from 'jest-fetch-mock';
import '../ujs';

describe('Remote utils', () => {
const mockEndpoint = 'http://localhost/endpoint';

beforeAll(() => {
fetchMock.enableMocks();
});

afterAll(() => {
fetchMock.disableMocks();
});

beforeEach(() => {
window.booru.csrfToken = Math.random().toString();
fetchMock.resetMocks();
});

function addOneShotEventListener(name: string, cb: (e: Event) => void) {
const handler = (event: Event) => {
cb(event);
document.removeEventListener(name, handler);
};
document.addEventListener(name, handler);
}

describe('a[data-remote]', () => {
const mockVerb = 'POST';
const submitA = () => {
const a = document.createElement('a');
a.href = mockEndpoint;
a.dataset.remote = 'remote';
a.dataset.method = mockVerb;

document.documentElement.insertAdjacentElement('beforeend', a);
a.click();

return a;
};

it('should call native fetch with the correct parameters (without body)', () => {
submitA();
expect(fetch).toHaveBeenNthCalledWith(1, mockEndpoint, {
method: mockVerb,
credentials: 'same-origin',
headers: {
'x-csrf-token': window.booru.csrfToken,
'x-requested-with': 'XMLHttpRequest'
}
});
});

it('should emit fetchcomplete response', () => new Promise<void>((resolve, reject) => {

Check failure on line 54 in assets/js/__tests__/ujs.spec.ts

View workflow job for this annotation

GitHub Actions / JavaScript Linting and Unit Tests

'reject' is defined but never used
let a: HTMLAnchorElement | null = null;

addOneShotEventListener('fetchcomplete', event => {
expect(event.target).toBe(a);
resolve();
});

a = submitA();
}));
});

describe('form[data-remote]', () => {
const mockVerb = 'POST';
const submitForm = () => {
const form = document.createElement('form');
form.action = mockEndpoint;
form.method = mockVerb;
form.dataset.remote = 'remote';

document.documentElement.insertAdjacentElement('beforeend', form);
form.submit();

return form;
};

it('should call native fetch with the correct parameters (with body)', () => {
submitForm();
expect(fetch).toHaveBeenNthCalledWith(1, mockEndpoint, {
method: mockVerb,
credentials: 'same-origin',
headers: {
'x-csrf-token': window.booru.csrfToken,
'x-requested-with': 'XMLHttpRequest'
},
body: new FormData(),
});
});

it('should emit fetchcomplete response', () => new Promise<void>((resolve, reject) => {

Check failure on line 93 in assets/js/__tests__/ujs.spec.ts

View workflow job for this annotation

GitHub Actions / JavaScript Linting and Unit Tests

'reject' is defined but never used
let form: HTMLFormElement | null = null;

addOneShotEventListener('fetchcomplete', event => {
expect(event.target).toBe(form);
resolve();
});

form = submitForm();
}));
});
});
35 changes: 18 additions & 17 deletions assets/js/ujs.js → assets/js/ujs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ const headers = () => ({
'x-requested-with': 'XMLHttpRequest'
});

function confirm(event, target) {
function confirm(event: Event, target: HTMLElement) {
if (!window.confirm(target.dataset.confirm)) {
event.preventDefault();
event.stopImmediatePropagation();
return false;
}
}

function disable(event, target) {
function disable(event: Event, target: HTMLAnchorElement | HTMLButtonElement | HTMLInputElement) {
// failed validations prevent the form from being submitted;
// stop here or the form will be permanently locked
if (target.type === 'submit' && target.closest(':invalid') !== null) return;

// Store what's already there so we don't lose it
const label = findFirstTextNode(target);
const label = findFirstTextNode<Text>(target);
if (label) {
target.dataset.enableWith = label.nodeValue;
target.dataset.enableWith = label.nodeValue || '';
label.nodeValue = ` ${target.dataset.disableWith}`;
}
else {
target.dataset.enableWith = target.innerHTML;
target.innerHTML = target.dataset.disableWith;
target.innerHTML = target.dataset.disableWith || '';
}

// delay is needed because Safari stops the submit if the button is immediately disabled
requestAnimationFrame(() => target.disabled = 'disabled');
requestAnimationFrame(() => target.setAttribute('disabled', 'disabled'));
}

// you should use button_to instead of link_to[method]!
function linkMethod(event, target) {
function linkMethod(event: Event, target: HTMLAnchorElement) {
event.preventDefault();

const form = makeEl('form', { action: target.href, method: 'POST' });
Expand All @@ -49,7 +49,7 @@ function linkMethod(event, target) {
form.submit();
}

function formRemote(event, target) {
function formRemote(event: Event, target: HTMLFormElement) {
event.preventDefault();

fetch(target.action, {
Expand All @@ -58,32 +58,31 @@ function formRemote(event, target) {
headers: headers(),
body: new FormData(target)
}).then(response => {
fire(target, 'fetchcomplete', response);
if (response && response.status === 300) {
window.location.reload(true);
return;
window.location.reload();
}
fire(target, 'fetchcomplete', response);
});
}

function formReset(event, target) {
$$('[disabled][data-disable-with][data-enable-with]', target).forEach(input => {
function formReset(_event: Event | null, target: HTMLElement) {
$$<HTMLElement>('[disabled][data-disable-with][data-enable-with]', target).forEach(input => {
const label = findFirstTextNode(input);
if (label) {
label.nodeValue = ` ${input.dataset.enableWith}`;
}
else { input.innerHTML = target.dataset.enableWith; }
else { input.innerHTML = target.dataset.enableWith || ''; }
delete input.dataset.enableWith;
input.removeAttribute('disabled');
});
}

function linkRemote(event, target) {
function linkRemote(event: Event, target: HTMLAnchorElement) {
event.preventDefault();

fetch(target.href, {
credentials: 'same-origin',
method: target.dataset.method.toUpperCase(),
method: (target.dataset.method || 'get').toUpperCase(),
headers: headers()
}).then(response =>
fire(target, 'fetchcomplete', response)
Expand All @@ -106,5 +105,7 @@ delegate(document, 'reset', {
});

window.addEventListener('pageshow', () => {
[].forEach.call(document.forms, form => formReset(null, form));
for (const form of document.forms) {
formReset(null, form);
}
});

0 comments on commit 35fc2f5

Please sign in to comment.