Skip to content

Commit

Permalink
Showing 2 changed files with 59 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/sdk/Formio.ts
Original file line number Diff line number Diff line change
@@ -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'));
56 changes: 56 additions & 0 deletions src/sdk/__tests__/Formio.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});

0 comments on commit 73c7c1e

Please sign in to comment.