Skip to content

Commit

Permalink
Merge pull request #2 from labcodes/feature/add-tests
Browse files Browse the repository at this point in the history
Feature/add tests
  • Loading branch information
lucianoratamero committed Jul 18, 2019
2 parents a0063ff + 54ff3ca commit fccb066
Show file tree
Hide file tree
Showing 13 changed files with 411 additions and 217 deletions.
11 changes: 10 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"presets": ["@babel/preset-env"],
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current",
},
},
],
],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"]
Expand Down
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repo_token: Bn0yjmlwMyda9F4v9czf1TI4nqoRdKSsa
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typings/

# Yarn Integrity file
.yarn-integrity
yarn.lock

# dotenv environment variables file
.env
Expand Down
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "current"
script:
- npm run coveralls
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# react-redux-api-tools

This project provides a middleware and a request helper to streamline react-redux data fetching.
[![Coverage Status](https://coveralls.io/repos/github/labcodes/react-redux-api-tools/badge.svg?branch=master)](https://coveralls.io/github/labcodes/react-redux-api-tools?branch=master) [![Build Status](https://travis-ci.org/labcodes/react-redux-api-tools.svg?branch=master)](https://travis-ci.org/labcodes/react-redux-api-tools)


*It's still on beta, so be aware of that ;)*
This project provides a middleware and a request helper to streamline react-redux data fetching.

### Installing

Expand Down
82 changes: 82 additions & 0 deletions __tests__/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { fetchFromApi } from '../lib/api';

describe('fetchFromApi', () => {
beforeEach(() => {
fetch.resetMocks();
});

it('should resolve fetch and return data with error', async () => {
fetch.mockResolvedValue({
status: 400,
message: 'The name is empty'
});

const requestData = {
method: 'POST',
body: { name: '' }
};
try {
await fetchFromApi('http://localhost:3000/products', requestData);
} catch (e) {
expect(e).toEqual({
status: 400,
message: 'The name is empty'
});
}
});

it('should resolve fetch and return data', async () => {
fetch.mockResolvedValue({
data: [{ name: 'Xbox' }]
});

const response = await fetchFromApi('http://localhost:3000/products');

expect(response).toEqual({
data: [{ name: 'Xbox' }]
});
});

it('should define application/json as default headers', async () => {
fetch.mockResolvedValue({
data: [{ name: 'Xbox' }]
});
let requestData = {};

const response = await fetchFromApi('http://localhost:3000/products', requestData);

expect(requestData).toEqual({
headers: {
'content-type': 'application/json'
}
});
});

it('should not overwrite content type if specified in requestData', async () => {
fetch.mockResolvedValue({
data: [{ name: 'Xbox' }]
});
let requestData = {
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
};

const response = await fetchFromApi('http://localhost:3000/products', requestData);

expect(requestData).toEqual({
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
});
});

it('should reject fetch and catch by error', async () => {
fetch.mockRejectedValue(new Error('404 Not found'));
try {
await fetchFromApi('http://localhost:3000/products/999');
} catch (e) {
expect(e).toEqual(new Error('404 Not found'));
}
});
});
Loading

0 comments on commit fccb066

Please sign in to comment.