Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #816 from mturley/simple-api-mock
Browse files Browse the repository at this point in the history
Replace broken axios API mock adapter, fix broken tests

(cherry picked from commit ed5249d)

https://bugzilla.redhat.com/show_bug.cgi?id=1657285
  • Loading branch information
michaelkro authored and simaishi committed Dec 11, 2018
1 parent fd367df commit 8e04c01
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 26 deletions.
11 changes: 9 additions & 2 deletions .jest-setup.js
@@ -1,11 +1,18 @@
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { APImock } from './app/javascript/common/mockRequests';

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

// Mocking translation function
global.__ = str => str;
global.n__ = str => str;
global.sprintf = str => str;
global.Jed = { sprintf: str => str };
global.API.get = jest.fn(() => Promise.resolve());
global.API.post = jest.fn(() => Promise.resolve());

APImock.reset();

global.API.get = jest.fn(url => APImock.respond('GET', url));
global.API.put = jest.fn(url => APImock.respond('PUT', url));
global.API.post = jest.fn(url => APImock.respond('POST', url));
global.API.delete = jest.fn(url => APImock.respond('DELETE', url));
34 changes: 23 additions & 11 deletions app/javascript/common/mockRequests.js
@@ -1,15 +1,27 @@
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
let mockedRequests;

const mock = new MockAdapter(axios);
const methods = {
GET: 'onGet',
POST: 'onPost',
PUT: 'onPut',
DELETE: 'onDelete'
export const APImock = {
reset: () => {
mockedRequests = { GET: {}, POST: {}, PUT: {}, DELETE: {} };
},
mock: (method, url, status, response) => {
mockedRequests[method][url] = { status, response };
},
respond: (method, url) => {
const mockMatchingUrl = mockedRequests[method][url];
const mocksMatchingMethod = Object.values(mockedRequests[method]);
const mocked = mockMatchingUrl || (mocksMatchingMethod.length > 0 && mocksMatchingMethod[0]);
if (mocked) {
if (mocked.status === 200) {
return Promise.resolve(mocked.response);
}
return Promise.reject(new Error('<mocked error>'));
}
return Promise.reject(new Error(`<no such mocked request ${method} ${url}>`));
}
};

export const mockRequest = ({ method = 'GET', url, data = null, status = 200, response = null }) =>
mock[methods[method]](url, data).reply(status, response);
export const mockRequest = ({ method = 'GET', url, status = 200, response = null }) =>
APImock.mock(method, url, status, response);

export const mockReset = () => mock.reset();
export const mockReset = () => APImock.reset();
Expand Up @@ -22,7 +22,7 @@ describe('mappingWizard actions', () => {
it('should fetch source clusters and return PENDING and FULFILLED action', () => {
const { fetchSourceClustersUrl } = requestSourceClustersData;
mockRequest({
fetchSourceClustersUrl,
url: fetchSourceClustersUrl,
status: 200
});
return store.dispatch(actions.fetchSourceClustersAction(fetchSourceClustersUrl)).then(() => {
Expand All @@ -32,7 +32,7 @@ describe('mappingWizard actions', () => {
it('should fetch source clusters and return PENDING and REJECTED action', () => {
const { fetchSourceClustersUrl } = requestSourceClustersData;
mockRequest({
fetchSourceClustersUrl,
url: fetchSourceClustersUrl,
status: 404
});
return store.dispatch(actions.fetchSourceClustersAction(fetchSourceClustersUrl)).catch(() => {
Expand All @@ -43,7 +43,7 @@ describe('mappingWizard actions', () => {
it('should fetch target clusters and return PENDING and FULFILLED action', () => {
const { fetchTargetClustersUrl } = requestTargetClustersData;
mockRequest({
fetchTargetClustersUrl,
url: fetchTargetClustersUrl,
status: 200
});
return store.dispatch(actions.fetchTargetClustersAction(fetchTargetClustersUrl)).then(() => {
Expand All @@ -53,7 +53,7 @@ describe('mappingWizard actions', () => {
it('should fetch target clusters and return PENDING and REJECTED action', () => {
const { fetchTargetClustersUrl } = requestTargetClustersData;
mockRequest({
fetchTargetClustersUrl,
url: fetchTargetClustersUrl,
status: 404
});
return store.dispatch(actions.fetchTargetClustersAction(fetchTargetClustersUrl)).catch(() => {
Expand Down
Expand Up @@ -11,6 +11,19 @@ Array [
]
`;

exports[`mappingWizard actions should fetch source clusters and return PENDING and REJECTED action 1`] = `
Array [
Object {
"type": "FETCH_V2V_SOURCE_CLUSTERS_PENDING",
},
Object {
"error": true,
"payload": [Error: <mocked error>],
"type": "FETCH_V2V_SOURCE_CLUSTERS_REJECTED",
},
]
`;
exports[`mappingWizard actions should fetch target clusters and return PENDING and FULFILLED action 1`] = `
Array [
Object {
Expand All @@ -21,3 +34,16 @@ Array [
},
]
`;
exports[`mappingWizard actions should fetch target clusters and return PENDING and REJECTED action 1`] = `
Array [
Object {
"type": "FETCH_V2V_TARGET_CLUSTERS_PENDING",
},
Object {
"error": true,
"payload": [Error: <mocked error>],
"type": "FETCH_V2V_TARGET_CLUSTERS_REJECTED",
},
]
`;
Expand Up @@ -24,7 +24,7 @@ describe('mappingWizard general step actions', () => {
it('should fetch conversion hosts and return PENDING and FULFILLED action', () => {
const { fetchConversionHostsUrl } = requestConversionHostsData;
mockRequest({
fetchConversionHostsUrl,
url: fetchConversionHostsUrl,
status: 200
});
return store.dispatch(actions.fetchConversionHostsAction(fetchConversionHostsUrl)).then(() => {
Expand Down
Expand Up @@ -24,9 +24,8 @@ describe('fetchTransformationPlansAction', () => {
mockRequest({
method: 'GET',
url: fetchTransformationPlansUrl,
params: null,
status: 200,
...response
response
});

return store
Expand All @@ -37,17 +36,21 @@ describe('fetchTransformationPlansAction', () => {
})
)
.then(() => {
expect(store.getActions()).toMatchSnapshot();
const actions = store.getActions();
expect(actions).toHaveLength(3);
expect(actions[0].type).toBe('FETCH_V2V_TRANSFORMATION_PLANS_PENDING');
expect(actions[1].type).toBe('FETCH_V2V_ALL_REQUESTS_WITH_TASKS_PENDING');
expect(actions[2].type).toBe('FETCH_V2V_TRANSFORMATION_PLANS_FULFILLED');
expect(actions[2].payload.data.resources).toHaveLength(7);
});
});

test('dispatches PENDING and REJECTED actions', () => {
mockRequest({
method: 'GET',
url: fetchTransformationPlansUrl,
params: null,
status: 404,
...response
response
});

return store
Expand Down Expand Up @@ -84,6 +87,7 @@ describe('setMigrationsFilterAction', () => {
describe('cancelPlanRequestAction', () => {
const request = {
method: 'POST',
url: TRANSFORMATION_PLAN_REQUESTS_URL,
data: { action: 'cancel' }
};

Expand Down
@@ -1,24 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cancelPlanRequestAction dispatched PENDING and REJECTED actions 1`] = `
Array [
Object {
"payload": "/api/requests/1",
"type": "V2V_CANCEL_PLAN_REQUEST_PENDING",
},
Object {
"error": true,
"payload": [Error: <mocked error>],
"type": "V2V_CANCEL_PLAN_REQUEST_REJECTED",
},
]
`;
exports[`cancelPlanRequestAction dispatches PENDING and FULFILLED actions 1`] = `
Array [
Object {
"payload": "/api/requests/1",
"type": "V2V_CANCEL_PLAN_REQUEST_PENDING",
},
Object {
"payload": Object {
"data": Object {
"href": "http://0.0.0.0:8080/api/requests/28",
"message": "MiqRequest 28 canceled",
"success": true,
},
},
"type": "V2V_CANCEL_PLAN_REQUEST_FULFILLED",
},
]
`;
exports[`fetchTransformationPlansAction dispatches PENDING and FULFILLED actions 1`] = `
exports[`fetchTransformationPlansAction dispatches PENDING and REJECTED actions 1`] = `
Array [
Object {
"type": "FETCH_V2V_TRANSFORMATION_PLANS_PENDING",
},
Object {
"type": "FETCH_V2V_TRANSFORMATION_PLANS_FULFILLED",
"error": true,
"payload": [Error: <mocked error>],
"type": "FETCH_V2V_TRANSFORMATION_PLANS_REJECTED",
},
]
`;
Expand Down
Expand Up @@ -20,7 +20,7 @@ describe('FETCH_V2V_PLAN', () => {
const id = '1';
const { fetchPlanUrl } = requestPlanData(id);
const request = {
fetchPlanUrl,
url: fetchPlanUrl,
status: 200
};

Expand Down
Expand Up @@ -10,3 +10,16 @@ Array [
},
]
`;

exports[`FETCH_V2V_PLAN fetchPlanAction dispatches the PENDING and REJECTED actions 1`] = `
Array [
Object {
"type": "FETCH_V2V_PLAN_PENDING",
},
Object {
"error": true,
"payload": [Error: <mocked error>],
"type": "FETCH_V2V_PLAN_REJECTED",
},
]
`;

0 comments on commit 8e04c01

Please sign in to comment.