Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
davguij committed Dec 20, 2017
2 parents ad27a66 + 113a2c4 commit a9df2cf
Show file tree
Hide file tree
Showing 8 changed files with 4,991 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ build/Release
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

dist
dist/
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tsconfig.json
.travis.yml
.nojekyll
src/
__tests__/
dist/__test__/
.npmignore
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js
node_js:
- "lts/*"
cache:
directories:
- "node_modules"
before_install:
- "npm install axios rxjs"
after_script:
- "npm run build"
after_success:
- "npm run semantic-release"
157 changes: 157 additions & 0 deletions __tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { rxios } from '../src';
import * as nock from 'nock';

const mockServer = nock('http://test.com');

describe('GET method', () => {
let rxiosInstance: rxios;

beforeEach(() => {
rxiosInstance = new rxios({
baseURL: 'http://test.com/'
});
});

it('makes a successful GET req', async () => {
const expected = { id: 1, title: 'rxios is so cool!', author: 'davguij' };
mockServer.get('/posts/1').reply(200, expected);
const promise = new Promise((resolve, reject) => {
rxiosInstance.get('http://test.com/posts/1').subscribe(resp => {
resolve(resp);
}, err => {
reject(err);
});
});
await expect(promise).resolves.toEqual(expected);
});

it('throws an error on a failed GET req', async () => {
mockServer.get('/posts/1').replyWithError('Request failed with status code 500');

const promise = new Promise((resolve, reject) => {
rxiosInstance.get('http://test.com/posts/1').subscribe(resp => {
resolve(resp);
}, err => {
reject(err);
});
});
await expect(promise).rejects.toBeInstanceOf(Error);
});

it('accepts queryParams', async () => {
mockServer.get('/posts').query({title: 'rxios', author: 'davguij'}).reply(200);
const promise = new Promise((resolve, reject) => {
rxiosInstance.get('http://test.com/posts', {title: 'rxios', author: 'davguij'}).subscribe(resp => {
resolve(resp);
}, err => {
reject(err);
})
});
await expect(promise).resolves.toBeDefined();
});

it('accepts a type for the response', async () => {
interface i {
cool: boolean;
};

const response: i = {cool: true};

mockServer.get('/post/1').reply(200, response);
const promise = new Promise((resolve, reject) => {
rxiosInstance.get<i>('http://test.com/post/1').subscribe(resp => {
resolve(resp);
}, err => {
reject(err);
})
});
await expect(promise).resolves.toBeDefined();
});

});

describe('POST method', () => {
let rxiosInstance: rxios;

beforeEach(() => {
rxiosInstance = new rxios({
baseURL: 'http://test.com/'
});
});

it('makes a successful POST req', async () => {
mockServer.post('/posts').reply(201);
const body = { title: 'json-server', author: 'davguij' };
const promise = new Promise((resolve, reject) => {
rxiosInstance.post('posts', body).subscribe(resp => {
resolve(resp);
}, err => {
reject(err);
})
});
await expect(promise).resolves.toBeDefined();
});

it('throws an error on a failed POST req', async () => {
mockServer.post('/posts').replyWithError('Request failed with status code 500');

const promise = new Promise((resolve, reject) => {
rxiosInstance.post('http://test.com/posts', {}).subscribe(resp => {
resolve(resp);
}, err => {
reject(err);
});
});
await expect(promise).rejects.toBeInstanceOf(Error);
});
});


describe('rest of methods', () => {
let rxiosInstance: rxios;

beforeEach(() => {
rxiosInstance = new rxios({
baseURL: 'http://test.com/'
});
});

it('makes a successful PUT req', async () => {
const body = { title: 'json-server', author: 'davguij' };
mockServer.put('/post/1').reply(200, body);
const promise = new Promise((resolve, reject) => {
rxiosInstance.put('post/1', body).subscribe(resp => {
resolve(resp);
}, err => {
reject(err);
})
});
await expect(promise).resolves.toEqual(body);
});

it('makes a successful PATCH req', async () => {
const body = { title: 'json-server', author: 'davguij' };
mockServer.patch('/post/1').reply(200, body);
const promise = new Promise((resolve, reject) => {
rxiosInstance.patch('post/1', body).subscribe(resp => {
resolve(resp);
}, err => {
reject(err);
})
});
await expect(promise).resolves.toEqual(body);
});

it('makes a successful DELETE req', async () => {
mockServer.delete('/post/1').reply(200);
const promise = new Promise((resolve, reject) => {
rxiosInstance.delete('post/1').subscribe(resp => {
resolve(resp);
}, err => {
reject(err);
})
});
await expect(promise).resolves.toBeDefined();
});

});
Loading

0 comments on commit a9df2cf

Please sign in to comment.