Merge remote-tracking branch 'origin/features/funnel-transitions' into features/funnel-transitions
This commit is contained in:
commit
07880f7abe
2 changed files with 77 additions and 1 deletions
|
|
@ -29,8 +29,11 @@ export default {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
// For mock use only!
|
|
||||||
|
|
||||||
|
/* istanbul ignore next */
|
||||||
callMockHttpClient({ method, endpoint}) {
|
callMockHttpClient({ method, endpoint}) {
|
||||||
|
// For Mock use only!
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
axios({
|
axios({
|
||||||
|
|
|
||||||
73
src/global-methods.spec.js
Normal file
73
src/global-methods.spec.js
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
import globalMethods from "@/global-methods";
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
//Mock external dependencies
|
||||||
|
jest.mock('axios');
|
||||||
|
|
||||||
|
it("Global Methods - Call Http Client - Should Resolve Promise", () => {
|
||||||
|
//Arrange
|
||||||
|
const endpoint = 'https://mock.safelite.com';
|
||||||
|
const httpArgs = setupMocksForHttpClient({ endpoint: endpoint });
|
||||||
|
|
||||||
|
//Act
|
||||||
|
globalMethods.callHttpClient(httpArgs)
|
||||||
|
.then((response) => {
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
expect(axios.mock.calls[0][0].url).toContain(endpoint);
|
||||||
|
expect(response.data.message).toContain('Success');
|
||||||
|
expect(response.status).toEqual(200);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Global Methods - Call Http Client - Should Reject Promise", () => {
|
||||||
|
//Arrange
|
||||||
|
const endpoint = 'https://mock.safelite.com';
|
||||||
|
const httpArgs = setupMocksForHttpClient({ endpoint: endpoint, isError: true });
|
||||||
|
|
||||||
|
//Act
|
||||||
|
globalMethods.callHttpClient(httpArgs)
|
||||||
|
.catch((err) => {
|
||||||
|
//Assert
|
||||||
|
expect(axios.mock.calls[0][0].url).toContain(endpoint);
|
||||||
|
expect(err.data.message).toContain('Error');
|
||||||
|
expect(err.status).toEqual(500);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function setupMocksForHttpClient({ endpoint = null, isError = false, additionalData = null }) {
|
||||||
|
|
||||||
|
//Clear node module
|
||||||
|
axios.mockClear();
|
||||||
|
|
||||||
|
// Success Response
|
||||||
|
const response = {
|
||||||
|
status: 200,
|
||||||
|
data: {
|
||||||
|
message: 'Success',
|
||||||
|
additionalData: additionalData
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Error Response
|
||||||
|
const error = {
|
||||||
|
response: {
|
||||||
|
status: 500,
|
||||||
|
data: {
|
||||||
|
message: 'Error',
|
||||||
|
additionalData: additionalData
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error interceptor on Axios returns a different object, so we need to mimic that.
|
||||||
|
if (isError) {
|
||||||
|
axios.mockRejectedValue(error);
|
||||||
|
} else {
|
||||||
|
axios.mockResolvedValue(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
endpoint: endpoint
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue