Skip to content

Commit

Permalink
started working on tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JumaKahiga committed Apr 17, 2019
1 parent 700cc70 commit 9b99669
Show file tree
Hide file tree
Showing 20 changed files with 621 additions and 340 deletions.
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
"draft-js": "^0.10.5",
"draft-js-export-html": "^1.3.3",
"draftjs-to-html": "^0.8.4",
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.9.1",
"enzyme-to-json": "^3.3.5",
"fetch-mock": "^7.3.1",
"html-react-parser": "^0.7.0",
"import": "0.0.6",
"moxios": "^0.4.0",
"react": "^16.8.6",
"react-bootstrap": "^1.0.0-beta.6",
"react-dnd": "^7.4.5",
Expand All @@ -24,8 +23,10 @@
"react-scripts": "2.1.8",
"react-tag-input": "^6.4.0",
"react-tagsinput": "^3.19.0",
"react-testing-library": "^6.1.2",
"reading-time": "^1.2.0",
"redux": "^4.0.1",
"redux-mock-store": "^1.5.3",
"redux-thunk": "^2.3.0",
"watch": "^1.0.2"
},
Expand Down Expand Up @@ -56,15 +57,18 @@
"not op_mini all"
],
"devDependencies": {
"node-sass": "^4.11.0",
"npm-run-all": "^4.1.5",
"eslint-plugin-react": "^7.12.4",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.12.1",
"enzyme-to-json": "^3.3.5",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jest": "^22.4.1",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.12.4",
"node-sass": "^4.11.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.16.4"
}
}
4 changes: 2 additions & 2 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.App-header {
background-color: #F2F3F4;
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: black; }
color: white; }
106 changes: 17 additions & 89 deletions src/actions/postArticles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from 'axios';
import { FETCH_ARTICLES, FETCH_ARTICLE, NEW_ARTICLE, UPDATE_ARTICLE } from './types';
import urlPath from "../configs/axios";
import axiosHeader from '../axios_config';


var fetch_articles = "http://127.0.0.1:8000/quotes/my_quotes/"
Expand All @@ -12,95 +13,22 @@ var apiURL2 = "https://aholympian.herokuapp.com/api/articles/"
var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxNTYwMTYzNzc3fQ.B11_cs4FbTuosJZn1WRw8p9E1nJq18wCpVOMijBqEVE";


// export const createArticle = data => (dispatch) => {
// return urlPath
// .request({
// method: "post",
// url: "articles/",
// data: {
// title: data.title,
// description: data.description,
// body: data.body
// }
// })
// .then(res => res.json())
// .then(data =>
// dispatch({
// type: NEW_ARTICLE,
// payload: data
// }));
// };


export const createArticle = (postData) => dispatch => {
fetch(apiURL1, {
method: 'POST',
headers: {
'content-type': 'application/json',
'Authorization': `Token ${token}`
},
body: JSON.stringify(postData)
})
.then(res => res.json())
.then(data =>{console.log(data)
dispatch({
type: NEW_ARTICLE,
payload: data
})});
export const createArticle = postData => async (dispatch) => {
await axios
.post(`${apiURL2}`, postData, axiosHeader)
.then((result) => {
dispatch({ type: NEW_ARTICLE, payload: result.data });
})
.catch((error) => {
});
};

export const fetchArticle = slug => dispatch => {
fetch(apiURL2 + slug, {
mode: 'cors',
method: 'GET',
headers: {
"Accept": "application/json",
"Content-type": "application/json; charset=UTF-8",
"Authorization": `Token ${token}`
}
})
.then(res => res.json())
.then(data=> //console.log(data)
dispatch({
type: FETCH_ARTICLE,
payload: data
export const fetchArticle = slug => async (dispatch) => {
await axios
.get(`${apiURL2}${slug}`, axiosHeader)
.then((result) => {
dispatch({ type: FETCH_ARTICLE, payload: result.data });
})
);
.catch((error) => {
});
};

// fetch(newResource, {
// mode: 'cors',
// method: 'GET',
// headers: {
// "Accept": 'application/json',
// "Content-type": 'application/json; charset=UTF-8',
// "Authorization": `Bearer ${token}`
// }
// })

// export const createArticle = (postData) => dispatch => {
// axios(new_article, {
// method: 'POST',
// headers: {
// 'content-type': 'application/json'
// },
// body: JSON.stringify(postData)
// })
// .then(res => res.json())
// .then(data =>
// dispatch({
// type: NEW_ARTICLE,
// payload: data
// }));
// };

// export const fetchArticle = () => dispatch => {
// axios(fetch_article)
// .then(res => res.json())
// .then(data=>
// dispatch({
// type: FETCH_ARTICLE,
// payload: data
// })
// );
// };
57 changes: 57 additions & 0 deletions src/actions/tests/getarticles.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import axios from 'axios';
import fetchMock from 'fetch-mock';

import { FETCH_ARTICLES } from './../types';
import { getArticles } from './../getArticles';

jest.mock('axios');

describe('testing fetching all articles', () => {
it('tests retrieving all articles', () => {
const testStore = configureMockStore([thunk]);
const store = testStore({payload: "here"});
axios.get.mockResolvedValue({response: {data: {}}});

const expectedAction = [
{
type: FETCH_ARTICLES,
payload: {}
},
];

return store.dispatch(getArticles({})).then(() => {
expect(store.getActions()).toEqual(expectedAction);
});
});
})




// const middlewares = [thunk]
// const mockStore = configureMockStore(middlewares)

// describe('async actions', () => {
// afterEach(() => {
// fetchMock.restore()
// })

// it('creates FETCH_TODOS_SUCCESS when fetching todos has been done', () => {
// fetchMock.getOnce('/articles', {
// payload: { todos: ['do something'] },
// headers: { 'content-type': 'application/json' }
// })

// const expectedActions = [
// { type: FETCH_ARTICLES }
// ]
// const store = mockStore({ todos: [] })

// return store.dispatch(getArticles()).then(() => {
// // return of async actions
// expect(store.getActions()).toEqual(expectedActions)
// })
// })
// })
39 changes: 39 additions & 0 deletions src/actions/tests/postactions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import axios from 'axios';

import { NEW_ARTICLE, FETCH_ARTICLE } from './../types';
import { createArticle, fetchArticle } from './../postArticles';

jest.mock('axios');

describe('testing new article', () => {
it('tests posting a new article', () => {
const testStore = configureMockStore([thunk]);
const store = testStore({});
axios.post.mockResolvedValue({data: {} });

const expectedAction = [
{
type: NEW_ARTICLE,
payload: {}
},
];

return store.dispatch(createArticle({})).then(() => {
expect(store.getActions()).toEqual(expectedAction);
});
});

it('tests fetching a single article', () => {
const testStore = configureMockStore([thunk]);
const store = testStore({});

axios.get.mockResolvedValue({response: {data: {}}});

const expectedAction = [{type: FETCH_ARTICLE}];
return store.dispatch(fetchArticle({})).then(() => {
expect(store.getActions()).toEqual(expectedAction);
});
});
})
29 changes: 29 additions & 0 deletions src/actions/tests/testStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {createStore, applyMiddleware, compose } from 'redux';
import rootReducer from './../../reducers';
import thunk from 'redux-thunk';

const initialState = {};
const middleware = [thunk];

export const testStore = (initialState) => {
const store = applyMiddleware(...middleware)(createStore);
return store(rootReducer, initialState);
};


// import {createStore, applyMiddleware, compose } from 'redux';
// import thunk from 'redux-thunk';

// import rootReducer from './../../reducers';

// const initialState = {};
// const middleware = [thunk];

// const testStore = createStore(
// rootReducer,
// initialState, compose(
// applyMiddleware(...middleware),
// window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
// ));

// export default testStore;
2 changes: 0 additions & 2 deletions src/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ export const FETCH_ARTICLE = "FETCH_ARTICLE";
export const NEW_ARTICLE = "NEW_ARTICLE";
export const UPDATE_ARTICLE = "UPDATE_ARTICLE";
export const BASE_URL = "https://aholympian.herokuapp.com/api/"

//export const BASE_URL = "http://127.0.0.1:8000/quotes/"
49 changes: 0 additions & 49 deletions src/components/Articles/Articles.js

This file was deleted.

Loading

0 comments on commit 9b99669

Please sign in to comment.