-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
122 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
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) => { | ||
let form: HTMLFormElement | null = null; | ||
|
||
addOneShotEventListener('fetchcomplete', event => { | ||
expect(event.target).toBe(form); | ||
resolve(); | ||
}); | ||
|
||
form = submitForm(); | ||
})); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters