Skip to content

Commit

Permalink
fix: hide loading indicator when endpoint call fails (#9380)
Browse files Browse the repository at this point in the history
  • Loading branch information
haijian-vaadin authored Nov 11, 2020
1 parent 529f711 commit 676f752
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,11 @@ export class ConnectClient {
const fetchNext: MiddlewareNext =
async(context: MiddlewareContext): Promise<Response> => {
this.loading(true);
return fetch(context.request).then(response => {
this.loading(false);
return response;
});
try {
return fetch(context.request);
} finally {
this.loading(false);
}
};

// Assemble the final middlewares array from internal
Expand Down
39 changes: 38 additions & 1 deletion flow-client/src/test/frontend/ConnectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,44 @@ describe('ConnectClient', () => {
expect(calls).to.equal('truefalse');
});

it('should hide Flow.loading indicator upon network failure', async() => {
let calls = '';
(window as any).Vaadin.Flow = {loading: (action: boolean) => calls += action};
fetchMock.post(
base + '/connect/FooEndpoint/reject',
Promise.reject(new TypeError('Network failure'))
);
try {
await client.call('FooEndpoint', 'reject');
} catch (error) {
// expected
} finally {
expect(calls).to.equal('truefalse');
}
});

it('should hide Flow.loading indicator upon server error', async() => {
const body = 'Unexpected error';
const errorResponse = new Response(
body,
{
status: 500,
statusText: 'Internal Server Error'
}
);
fetchMock.post(base + '/connect/FooEndpoint/vaadinConnectResponse', errorResponse);

let calls = '';
(window as any).Vaadin.Flow = {loading: (action: boolean) => calls += action};
try {
await client.call('FooEndpoint', 'vaadinConnectResponse');
} catch (error) {
// expected
} finally {
expect(calls).to.equal('truefalse');
}
});

it('should use JSON request headers', async() => {
await client.call('FooEndpoint', 'fooMethod');

Expand All @@ -143,7 +181,6 @@ describe('ConnectClient', () => {
});

it('should set header for preventing CSRF', async() => {
debugger;
await client.call('FooEndpoint', 'fooMethod');

const headers = fetchMock.lastOptions().headers;
Expand Down

0 comments on commit 676f752

Please sign in to comment.