Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIO-8354: fallback to passing response in argument if response.body is undefined #101

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/sdk/Formio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1566,13 +1566,13 @@ export class Formio {
if (!response.ok) {
if (response.status === 440) {
Formio.setToken(null, opts);
Formio.events.emit('formio.sessionExpired', response.body);
Formio.events.emit('formio.sessionExpired', response.body || response);
}
else if (response.status === 401) {
Formio.events.emit('formio.unauthorized', response.body);
Formio.events.emit('formio.unauthorized', response.body || response);
}
else if (response.status === 416) {
Formio.events.emit('formio.rangeIsNotSatisfiable', response.body);
Formio.events.emit('formio.rangeIsNotSatisfiable', response.body || response);
}
else if (response.status === 504) {
return Promise.reject(new Error('Network request failed'));
Expand Down
56 changes: 56 additions & 0 deletions src/sdk/__tests__/Formio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2173,4 +2173,60 @@ describe('Formio.js Tests', () => {
assert.ok(plugin.wrapStaticRequestPromise.calledOnce, 'wrapStaticRequestPromise should be called once');
});
});
describe('Formio.request', () => {
it('should emit a formio.sessionExpired event when the response status is 440 and the response object should exist', (done) => {
let eventFired = false
let responseNotUndefined = false
setTimeout(()=>{
assert(eventFired, 'formio.sessionExpired event was not called');
assert(responseNotUndefined, 'a response was not passed into the event');
fetchMock.restore()
done()
},200)
Formio.events.on('formio.sessionExpired', (response: any) => {
eventFired = true
if (response){
responseNotUndefined = true
}
})
fetchMock.mock('http://localhost:8080/test', 440);
Formio.request('http://localhost:8080/test');
});
it('should emit a formio.unauthorized event when the response status is 401', (done) => {
let eventFired = false
let responseNotUndefined = false
setTimeout(()=>{
assert(eventFired, 'formio.unauthorized event was not called');
assert(responseNotUndefined, 'a response was not passed into the event');
fetchMock.restore()
done()
},200);
Formio.events.on('formio.unauthorized', (response: any) => {
eventFired = true;
if (response){
responseNotUndefined = true;
}
})
fetchMock.mock('http://localhost:8080/test', 401);
Formio.request('http://localhost:8080/test');
});
it('should emit a formio.rangeIsNotSatisfiable event when the response status is 416', (done) => {
let eventFired = false;
let responseNotUndefined = false;
setTimeout(()=>{
assert(eventFired, 'formio.rangeIsNotSatisfiable event was not called');
assert(responseNotUndefined, 'a response was not passed into the event');
fetchMock.restore()
done()
},200);
Formio.events.on('formio.rangeIsNotSatisfiable', (response) => {
eventFired = true;
if (response) {
responseNotUndefined = true;
}
})
fetchMock.mock('http://localhost:8080/test', 416);
Formio.request('http://localhost:8080/test');
});
});
});
Loading