-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13961 from margelo/hanno-andrew-mock-api-responses
Mocking API responses (for CI/CD perf regression tests)
- Loading branch information
Showing
9 changed files
with
2,942 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* eslint-disable rulesdir/no-api-in-views */ | ||
import _ from 'underscore'; | ||
import Onyx from 'react-native-onyx'; | ||
import Log from '../Log'; | ||
|
||
/** | ||
* A dictionary which has the name of a API command as key, and a function which | ||
* receives the api command parameters as value and is expected to return a response | ||
* object. | ||
*/ | ||
const mocks = { | ||
BeginSignIn: ({email}) => { | ||
const response = require('../E2E/apiMocks/beginSignin.json'); | ||
response.onyxData.forEach((data) => { | ||
if (data.key !== 'credentials') { | ||
return; | ||
} | ||
// eslint-disable-next-line no-param-reassign | ||
data.value.login = email; | ||
}); | ||
return response; | ||
}, | ||
SigninUser: ({email}) => { | ||
const response = require('../E2E/apiMocks/signinUser.json'); | ||
response.onyxData.forEach((data) => { | ||
if (data.key !== 'session') { | ||
return; | ||
} | ||
// eslint-disable-next-line no-param-reassign | ||
data.value.email = email; | ||
}); | ||
return response; | ||
}, | ||
OpenApp: () => require('../E2E/apiMocks/openApp.json'), | ||
OpenReport: () => require('../E2E/apiMocks/openReport.json'), | ||
AuthenticatePusher: () => require('../E2E/apiMocks/authenticatePusher.json'), | ||
}; | ||
|
||
function mockCall(command, apiCommandParameters, tag) { | ||
const mockResponse = mocks[command] && mocks[command](apiCommandParameters); | ||
if (!mockResponse || !_.isArray(mockResponse.onyxData)) { | ||
Log.warn(`[${tag}] for command ${command} is not mocked yet!`); | ||
return; | ||
} | ||
|
||
return Onyx.update(mockResponse.onyxData); | ||
} | ||
|
||
/** | ||
* All calls to API.write() will be persisted to disk as JSON with the params, successData, and failureData. | ||
* This is so that if the network is unavailable or the app is closed, we can send the WRITE request later. | ||
* | ||
* @param {String} command - Name of API command to call. | ||
* @param {Object} apiCommandParameters - Parameters to send to the API. | ||
* | ||
* @returns {Promise} | ||
*/ | ||
function write(command, apiCommandParameters = {}) { | ||
return mockCall(command, apiCommandParameters, 'API.write'); | ||
} | ||
|
||
/** | ||
* For commands where the network response must be accessed directly or when there is functionality that can only | ||
* happen once the request is finished (eg. calling third-party services like Onfido and Plaid, redirecting a user | ||
* depending on the response data, etc.). | ||
* It works just like API.read(), except that it will return a promise. | ||
* Using this method is discouraged and will throw an ESLint error. Use it sparingly and only when all other alternatives have been exhausted. | ||
* It is best to discuss it in Slack anytime you are tempted to use this method. | ||
* | ||
* @param {String} command - Name of API command to call. | ||
* @param {Object} apiCommandParameters - Parameters to send to the API. | ||
* | ||
* @returns {Promise} | ||
*/ | ||
function makeRequestWithSideEffects(command, apiCommandParameters = {}) { | ||
return mockCall(command, apiCommandParameters, 'API.makeRequestWithSideEffects'); | ||
} | ||
|
||
/** | ||
* Requests made with this method are not be persisted to disk. If there is no network connectivity, the request is ignored and discarded. | ||
* | ||
* @param {String} command - Name of API command to call. | ||
* @param {Object} apiCommandParameters - Parameters to send to the API. | ||
* | ||
* @returns {Promise} | ||
*/ | ||
function read(command, apiCommandParameters) { | ||
return mockCall(command, apiCommandParameters, 'API.read'); | ||
} | ||
|
||
export { | ||
write, | ||
makeRequestWithSideEffects, | ||
read, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"auth": "auth", | ||
"shared_secret": "secret", | ||
"jsonCode": 200, | ||
"requestID": "783ef7fc3991969a-SJC" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"onyxData": [ | ||
{ | ||
"onyxMethod": "merge", | ||
"key": "credentials", | ||
"value": { | ||
"login": "thisemailwillgetreplaced@duringsign.com" | ||
} | ||
}, | ||
{ | ||
"onyxMethod": "merge", | ||
"key": "account", | ||
"value": { | ||
"validated": true | ||
} | ||
}, | ||
{ | ||
"onyxMethod": "set", | ||
"key": "betas", | ||
"value": [ | ||
"passwordless" | ||
] | ||
} | ||
], | ||
"jsonCode": 200, | ||
"requestID": "783e54ef4b38cff5-SJC" | ||
} |
Oops, something went wrong.