proactive-http-fetch
Version:
Proactive http fetch package
176 lines (147 loc) • 6.53 kB
text/typescript
import { EventAggregator } from 'aurelia-event-aggregator';
import { Container } from 'aurelia-dependency-injection';
import { HttpClient } from 'aurelia-fetch-client';
import { BrowserLocation } from '../src/browser-location';
import { HttpFetch } from '../src/http-fetch';
import * as fixture from './fixture-http-fetch';
describe('given http fetch', () => {
it('when request succedded then should receive expected result', async () => {
// arrange
fixture.success('/test1', JSON.stringify({data: 1}));
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get('/test1')
.then(a => expect(a).toEqual({data: 1}) )
.catch(a => expect(a).not.toBe(a) );
})
it('when send get request with query string then url as expected', async () => {
// arrange
fixture.success('/test2?x=1', null);
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get('/test2?x=1')
.then(a => expect(a).toBeUndefined() )
.catch(a => expect(a).not.toBe(a) );
})
it('when send get request with object param then url as expected', async () => {
// arrange
let input = JSON.stringify({x:1});
fixture.success(`/test3?${input}`, null);
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get(`/test3?${input}`)
.then(a => expect(a).toBeUndefined() )
.catch(a => expect(a).not.toBe(a) );
})
it('when response have empty body then should receive empty result', async () => {
// arrange
fixture.success('/test4', null);
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get('/test4')
.then(a => expect(a).toBeUndefined() )
.catch(a => expect(a).not.toBe(a) );
})
it('when get request succedded but response type is not valid json then should throw exception', async () => {
// arrange
fixture.success('/test5', 'abc');
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get('/test5')
.then(a => expect(a).not.toBe(a) )
.catch(a => expect(a).toContain('Network error.') );
})
it('when request failed then should throw exception', async () => {
// arrange
fixture.failure('/test6');
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get('/test6')
.then(a => expect(a).not.toBe(a) )
.catch(a => expect(a).toBe('Internal Server Error') );
})
it('when response redirect (401) then should change browser location', async () => {
// arrange
let container = new Container();
container.registerSingleton(BrowserLocation);
let spy = spyOn(container.get(BrowserLocation), 'change');
fixture.redirect('/test7', '/redirect', 401);
let sut = container.get(HttpFetch) as HttpFetch;
// act & assert
await sut
.put('/test7')
.catch(error => expect(spy).toHaveBeenCalledWith('/redirect') );
})
it('when response redirect (403) then should change browser location', async () => {
// arrange
let container = new Container();
container.registerSingleton(BrowserLocation);
let spy = spyOn(container.get(BrowserLocation), 'change');
fixture.redirect('/test8', '/redirect', 403);
let sut = container.get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get('/test8')
.catch(error => expect(spy).toHaveBeenCalledWith('/redirect') );
})
it('when request failed then should send error event', async () => {
// arrange
let container = new Container();
container.registerSingleton(EventAggregator);
let spy = spyOn(container.get(EventAggregator), 'publish');
fixture.failure('/test9');
let sut = container.get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get('/test9')
.catch(error => expect(spy).toHaveBeenCalledWith('error', 'Internal Server Error') );
})
it('when send real get request then result as expected', async () => {
// arrange
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get('https://jsonplaceholder.typicode.com/posts/1')
.then(response => expect(response.id).toEqual(1));
})
it('when send real get request with query string then result as expected', async () => {
// arrange
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get('https://jsonplaceholder.typicode.com/posts', "id=1")
.then(response => expect(response[0].id).toEqual(1));
})
it('when real get request failed then should throw exception', async () => {
// arrange
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.get('https://jsonplaceholder.typicode.com/posts/0')
.catch(error => expect(error).toBe('Not Found'));
})
it('when send real post request with json param then result as expected', async () => {
// arrange
let input = { title: 'foo', body: 'bar', userId: 1 };
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.post('https://jsonplaceholder.typicode.com/posts', input)
.then(response => expect(response.id).toBeGreaterThan(0));
})
it('when send real put request with json param then result as expected', async () => {
// arrange
let input = { id: 1, title: 'foo updated', body: 'bar', userId: 1 };
let sut = new Container().get(HttpFetch) as HttpFetch;
// act & assert
await sut
.put('https://jsonplaceholder.typicode.com/posts/1', JSON.stringify(input))
.then(response => expect(response).toEqual({ id: 1 }));
})
});