-
-
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
107 additions
and
14 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,91 @@ | ||
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(); | ||
}); | ||
|
||
describe('a[data-remote]', () => { | ||
const mockVerb = 'POST'; | ||
|
||
it('should call native fetch with the correct parameters (without body)', () => { | ||
const a = document.createElement('a'); | ||
a.href = mockEndpoint; | ||
a.dataset.remote = 'remote'; | ||
a.dataset.method = mockVerb; | ||
|
||
document.documentElement.insertAdjacentElement('beforeend', a); | ||
a.click(); | ||
|
||
expect(fetch).toHaveBeenNthCalledWith(1, mockEndpoint, { | ||
method: mockVerb, | ||
credentials: 'same-origin', | ||
headers: { | ||
'x-csrf-token': window.booru.csrfToken, | ||
'x-requested-with': 'XMLHttpRequest' | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
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; | ||
|
||
document.addEventListener('fetchcomplete', event => { | ||
expect(event.target).toEqual(form); | ||
resolve(); | ||
}); | ||
|
||
form = submitForm(); | ||
})); | ||
|
||
it('should should reload on 300 error response', () => { | ||
const locationSpy = jest.spyOn(window.location, 'reload'); | ||
locationSpy.mockImplementation(() => {}); | ||
|
||
fetchMock.mockResponse('', { status: 300 }); | ||
|
||
submitForm(); | ||
expect(locationSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
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