Fakultas Ilmu Komputer UI

Commit 8483327f authored by Zamil Majdy's avatar Zamil Majdy
Browse files

[Refactor] change fetchmock using ES6 import style and add restore mocking to test files

parent a0b5785d
/* eslint-disable no-unused-expressions */
import React from 'react';
import ReactTestUtils from 'react-addons-test-utils';
import fetchMock from 'fetch-mock';
import VacancyPage from '../VacancyPage';
import Storage from '../lib/Storage';
// import Server from '../../lib/Server';
describe('VacancyPage', () => {
fetchMock.get('*', { data: 'value' });
const studentSession = {
url: 'http://localhost:8000/api/users/9/',
username: 'muhammad.reza42',
......
import React from 'react';
import ReactTestUtils from 'react-addons-test-utils';
import ApplyConfirmationModal from '../../components/ApplyConfirmationModal';
import fetchMock from 'fetch-mock';
import Storage from '../../lib/Storage';
import Server from '../../lib/Server';
describe('ApplyConfirmationModal', () => {
const fetchMock = require('fetch-mock');
it('renders without problem', () => {
const applyModal = ReactTestUtils.renderIntoDocument(
......@@ -26,6 +26,7 @@ describe('ApplyConfirmationModal', () => {
applyModal.handleOpen();
applyModal.close();
expect(applyModal.state.header).to.equal('Menghubungkan ke Server');
fetchMock.restore();
});
it('test apply with problem', () => {
......@@ -35,11 +36,11 @@ describe('ApplyConfirmationModal', () => {
const response = { student: { id: 1, name: 2 } };
Storage.set('user-data', response);
fetchMock.post('*', { status: 404, body: response });
fetchMock.post('*', 404);
applyModal.open();
applyModal.handleOpen();
applyModal.close();
expect(applyModal.state.header).to.equal('Menghubungkan ke Server');
fetchMock.restore();
});
});
\ No newline at end of file
/* eslint-disable no-unused-expressions */
import React from 'react';
import ReactTestUtils from 'react-addons-test-utils';
import fetchMock from 'fetch-mock';
import CancelModal from '../../components/CancelModal';
import Storage from '../../lib/Storage';
......@@ -29,7 +30,6 @@ describe('CancelModal', () => {
});
it('remove vacancy without problem', () => {
const fetchMock = require('fetch-mock');
fetchMock.delete('*', { data: 'value' });
const modalPendaftaran = ReactTestUtils.renderIntoDocument(
<CancelModal id={4} />);
......@@ -38,21 +38,32 @@ describe('CancelModal', () => {
Storage.set('user-data', response3);
modalPendaftaran.removeVacancy();
expect(modalPendaftaran.state.header).to.exist;
fetchMock.restore();
});
it('confirm without problem', () => {
const fetchMock = require('fetch-mock');
fetchMock.delete('*', { data: 'value' });
it('remove vacancy with problem', () => {
fetchMock.delete('*', 404);
const modalPendaftaran = ReactTestUtils.renderIntoDocument(
<CancelModal id={4} />);
const response3 = { student: { id: 1, name: 2 } };
Storage.set('user-data', response3);
modalPendaftaran.removeVacancy();
expect(modalPendaftaran.state.header).to.exist;
fetchMock.restore();
});
it('confirm with problem', () => {
fetchMock.delete('*', 404);
const modalPendaftaran = ReactTestUtils.renderIntoDocument(
<CancelModal id={4} />);
modalPendaftaran.confirm();
expect(modalPendaftaran.state.header).to.equal('Permintaan gagal');
fetchMock.restore();
});
it('render next modal without problem', () => {
const fetchMock = require('fetch-mock');
fetchMock.delete('*', { data: 'value' });
const modalPendaftaran = ReactTestUtils.renderIntoDocument(
<CancelModal id={4} />);
......@@ -60,5 +71,4 @@ describe('CancelModal', () => {
modalPendaftaran.forceUpdate()
expect(modalPendaftaran).to.exist;
});
});
......@@ -53,7 +53,7 @@ describe('LoginForm', () => {
});
it('submit form without problem', () => {
fetchMock.post('*', {data: 'value'});
fetchMock.post('*', { data: 'value' });
const formLogin = ReactTestUtils.renderIntoDocument(
<LoginForm url="" />);
......@@ -62,5 +62,19 @@ describe('LoginForm', () => {
const form = ReactTestUtils.findRenderedDOMComponentWithTag(formLogin, 'Form');
ReactTestUtils.Simulate.submit(form);
fetchMock.restore();
});
it('submit form with problem', () => {
fetchMock.post('*', 404);
const formLogin = ReactTestUtils.renderIntoDocument(
<LoginForm url="" />);
const submitButton = ReactTestUtils.findRenderedDOMComponentWithTag(formLogin, 'Button');
ReactTestUtils.Simulate.click(submitButton);
const form = ReactTestUtils.findRenderedDOMComponentWithTag(formLogin, 'Form');
ReactTestUtils.Simulate.submit(form);
fetchMock.restore();
});
});
......@@ -3,9 +3,18 @@ import ReactTestUtils from 'react-addons-test-utils';
import Tabs from '../../components/Tabs';
describe('ApplyModal', () => {
it('renders without problem', () => {
it('renders without problem', () => {
const tabs = ReactTestUtils.renderIntoDocument(
<Tabs selected={1} children={[]} />);
<Tabs selected={1} children={[]} />,
);
expect(tabs).to.exist;
});
it('renders handle click properly', () => {
const tabs = ReactTestUtils.renderIntoDocument(
<Tabs selected={1} children={[]} />,
);
tabs.handleClick('stub', new Event('click'));
expect(tabs.state.selected).to.equal('stub');
});
});
/* eslint-disable no-unused-expressions */
import React from 'react';
import ReactTestUtils from 'react-addons-test-utils';
import fetchMock from 'fetch-mock';
import VacancyList from '../../components/VacancyList';
import Server from '../../lib/Server';
describe('VacancyList', () => {
const fetchMock = require('fetch-mock');
const response = [{
close_time: '2019-03-28T05:55:42Z',
company: {
......@@ -28,14 +27,16 @@ describe('VacancyList', () => {
const vacancyList = ReactTestUtils.renderIntoDocument(
<VacancyList userId={1} url="test" />);
expect(vacancyList).to.exist;
fetchMock.restore();
});
it('renders without problem for company', () => {
fetchMock.get('*', response);
const vacancyList = ReactTestUtils.renderIntoDocument(
<VacancyList type='company' userId={1} url="test" />);
<VacancyList type="company" userId={1} url="test" />);
vacancyList.state.vacancies = response;
expect(vacancyList.generateVacancies()).to.exist;
fetchMock.restore();
});
it('update bookmarks without problem', () => {
......@@ -44,6 +45,7 @@ describe('VacancyList', () => {
<VacancyList userId={1} url="test" />);
vacancyList.updateBookmarkList();
expect(JSON.stringify(vacancyList.state.bookmarkList)).to.be.defined;
fetchMock.restore();
});
it('renders marked bookmarked vacancies without problem', () => {
......@@ -51,8 +53,9 @@ describe('VacancyList', () => {
const vacancyList = ReactTestUtils.renderIntoDocument(
<VacancyList userId={1} url="test" />);
vacancyList.state.vacancies = response;
vacancyList.state.bookmarkList = [{id: 5}, {id: 3}, {id: 1}];
vacancyList.state.bookmarkList = [{ id: 5 }, { id: 3 }, { id: 1 }];
expect(vacancyList.generateVacancies()).to.exist;
fetchMock.restore();
});
it('renders not marked vacancies without problem', () => {
......@@ -60,8 +63,9 @@ describe('VacancyList', () => {
const vacancyList = ReactTestUtils.renderIntoDocument(
<VacancyList userId={1} url="test" />);
vacancyList.state.vacancies = response;
vacancyList.state.bookmarkList = [{id: 6}, {id: 4}, {id: 2}];
vacancyList.state.bookmarkList = [{ id: 6 }, { id: 4 }, { id: 2 }];
expect(vacancyList.generateVacancies()).to.exist;
fetchMock.restore();
});
it('success calling API', () => {
......@@ -70,6 +74,7 @@ describe('VacancyList', () => {
<VacancyList userId={1} url="test" />);
vacancyList.state.vacancies = response;
expect(JSON.stringify(vacancyList.state.vacancies)).to.equal(JSON.stringify(response));
});
fetchMock.restore();
});
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment