Skip to content

Commit

Permalink
ft(user-can-rate-artilce)
Browse files Browse the repository at this point in the history
add a temporary login form
add a temporary get all articles page
add read one article page
add a dropdown button
add the star rating  modal
add the an on-success notification
[Starts #166840975]
  • Loading branch information
Alpha1202 committed Sep 3, 2019
1 parent f45793c commit d7896fd
Show file tree
Hide file tree
Showing 20 changed files with 781 additions and 32 deletions.
1 change: 1 addition & 0 deletions _test_/Articles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Adapter from 'enzyme-adapter-react-16';

import { GetAllArticles } from '../src/views/Articles';


Enzyme.configure({ adapter: new Adapter() });

describe('Articles', () => {
Expand Down
17 changes: 17 additions & 0 deletions _test_/Dropdown.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import expect from 'expect';
import { shallow } from 'enzyme';

import DropLeft from '../src/components/SingleArticle/RatingsBtn/index';


describe('App', () => {
let app;
beforeEach(() => {
app = shallow(<DropLeft />);
});

it('renders successfully', () => {
expect(app).toBeDefined();
});
});
36 changes: 36 additions & 0 deletions _test_/RatingsBtn.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import expect from 'expect';
import { shallow } from 'enzyme';

import RatingsBtn from '../src/components/SingleArticle/RatingsBtn/index';


describe('App', () => {
const props = {
toggle: jest.fn(),
dropdownOpen: false,
prevState: { dropdown: false },
};

let app;
beforeEach(() => {
app = shallow(<RatingsBtn {...props} />);
});

it('renders successfully', () => {
expect(app).toBeDefined();
});

it('toggles the dropdown menu when clicked', () => {
const toggleButton = app.find('#toggle');
expect(toggleButton).toHaveLength(1);
toggleButton.simulate('click');

expect(app).toMatchSnapshot();
});
it('toggles the dropdown menu if clicked', () => {
app.instance().toggle(props.prevState);
const render = jest.spyOn(app.instance(), 'toggle');
expect(render).toHaveBeenCalledTimes(0);
});
});
40 changes: 40 additions & 0 deletions _test_/RatingsModal.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import expect from 'expect';
import { shallow } from 'enzyme';

import RatingsModal from '../src/components/SingleArticle/RatingsModal/index';


describe('App', () => {
let app;
beforeEach(() => {
app = shallow(<RatingsModal />);
});

it('renders successfully', () => {
expect(app).toBeDefined();
});

it('tags have classes', () => {
expect(app.find('_class').length).toBe(9);
});

it('renders a span tag', () => {
expect(app.find('span').length).toBe(1);
});

it('renders a div', () => {
expect(app.find('div').length).toBe(1);
});

it('renders a h2 tag', () => {
expect(app.find('h2').length).toBe(1);
});

it('renders StarRatingComponent', () => {
expect(app.find('StarRatingComponent').length).toBe(1);
});
});



27 changes: 26 additions & 1 deletion _test_/SingleArticle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@ import MainArticle from '../src/components/SingleArticle/MainArticle';
import Tags from '../src/components/SingleArticle/Tags';
import Recommended from '../src/components/SingleArticle/Recommended';
import CommentsBtn from '../src/components/SingleArticle/CommentsBtn';
import RatingsModal from '../src/components/SingleArticle/RatingsModal';



describe('Single Article', () => {
let component;
let prevProps;

let nextValue;

const props = {
prevProps: { match: { params: { slug: 'hello' } } },
match: { params: { slug: '' } },
article: { userId: 1 },
viewArticle: jest.fn(),
fetchArticles: jest.fn(),
articleRating: jest.fn(),
loading: false,
componentDidUpdate: jest.fn(prevProps),
onStarClick: jest.fn(nextValue),
handleRatingsSubmit: jest.fn(),
ccheckUser: jest.fn(),
};


beforeEach(() => {
component = shallow(<SingleArticle {...props} />);
});
Expand Down Expand Up @@ -50,6 +59,10 @@ describe('Single Article', () => {
expect(component.find(CommentsBtn).length).toBe(1);
});

it('renders a RatingsModal component', () => {
expect(component.find(RatingsModal).length).toBe(1);
});

it('renders a Loader component', () => {
component.setProps({ loading: true });
expect(component.find(Loader).length).toBe(1);
Expand All @@ -60,4 +73,16 @@ describe('Single Article', () => {
const render = jest.spyOn(component.instance(), 'render');
expect(render).toHaveBeenCalledTimes(0);
});

it('Click on ratings modal', () => {
component.instance().onStarClick(nextValue);
const starClick = jest.spyOn(component.instance(), 'onStarClick');
expect(starClick).toHaveBeenCalledTimes(0);
});

it('submits ratings', () => {
component.instance().handleRatingsSubmit();
const starSubmit = jest.spyOn(component.instance(), 'handleRatingsSubmit');
expect(starSubmit).toHaveBeenCalledTimes(0);
});
});
67 changes: 67 additions & 0 deletions _test_/article.action.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import expect from 'expect';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import moxios from 'moxios';
import * as types from '../src/actions/types';
import * as actions from '../src/actions/article.action';
import axios from '../src/config/axiosInstance';

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

const initialState = {};
const store = mockStore(initialState);


describe('rate Article Tests', () => {
afterEach(() => {
moxios.install(axios);
store.clearActions();
});

afterEach(() => moxios.uninstall(axios));

it('Returns success if rate article was successful', (done) => {
const dataValue = {
value: 3,
};
jest.spyOn(axios, 'post').mockResolvedValue({ data: { message: '', data: {} } });

const expectedActions = [
{
type: types.RATE_ARTICLE_START,
},
{
type: types.RATE_ARTICLE_SUCCESS,
},
];
store.dispatch(actions.articleRating(dataValue))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
done();
});

});

it('Return failure if rate article was unsuccessful', (done) => {

jest.spyOn(axios, 'post').mockRejectedValue({ response: { data: { message: '' } } });


const expectedActions = [
{
type: types.RATE_ARTICLE_START,
},
{
type: types.RATE_ARTICLE_FAILURE,
},
];

store.dispatch(actions.articleRating(3))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
done();
});

});
});
16 changes: 16 additions & 0 deletions _test_/authReducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ describe('Login reducer', () => {
loading: false,
});
});
it('should set loading to true', () => {
expect(reducer(initialState, {
type: actionTypes.AUTH_LOADING,
})).toEqual({
...initialState,
loading: true,
});
});
it('should remove current user', () => {
expect(reducer(initialState, {
type: actionTypes.REMOVE_CURRENT_USER,
})).toEqual({
...initialState,
loading: false,
});
});

it('should successfully store the token upon login', () => {
expect(reducer(initialState, {
Expand Down
28 changes: 28 additions & 0 deletions _test_/singleArticleReducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ describe('Login reducer', () => {
initialState = {
article: {},
loading: false,
alert: '',
rating: '',
};
});
it('should return the initial state', () => {
Expand All @@ -33,4 +35,30 @@ describe('Login reducer', () => {
article: 'Some random article',
});
});
it('should set loading to true rating submit is clicked', () => {
expect(reducer(initialState, {
type: actionTypes.RATE_ARTICLE_START,
})).toEqual({
...initialState,
loading: true,
});
});
it('should update rating successful', () => {
expect(reducer(initialState, {
type: actionTypes.RATE_ARTICLE_SUCCESS, rating: {}, message: 'rating updated',
})).toEqual({
...initialState,
loading: false,
rating: {},
alert: 'rating updated',
});
});
it('should set loading to false rating submit fails', () => {
expect(reducer(initialState, {
type: actionTypes.RATE_ARTICLE_FAILURE,
})).toEqual({
...initialState,
loading: false,
});
});
});
Loading

0 comments on commit d7896fd

Please sign in to comment.