Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Change/migrate mock api to typescript #15

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const config = {
const resolution = context.resolveRequest(context, moduleName, platform);
if (isUsingMockAPI && moduleName.includes('/API')) {
const originalPath = resolution.filePath;
const mockPath = originalPath.replace('src/libs/API.ts', 'src/libs/E2E/API.mock.js').replace('/src/libs/API.js/', 'src/libs/E2E/API.mock.js');
const mockPath = originalPath.replace('src/libs/API.ts', 'src/libs/E2E/API.mock.ts');
// eslint-disable-next-line no-console
console.log('⚠️⚠️⚠️⚠️ Replacing resolution path', originalPath, ' => ', mockPath);

Expand Down
32 changes: 13 additions & 19 deletions src/libs/E2E/API.mock.js → src/libs/E2E/API.mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable rulesdir/no-api-in-views */
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import Log from '@libs/Log';
import mockAuthenticatePusher from './apiMocks/authenticatePusher';
// mock functions
Expand All @@ -23,9 +22,10 @@ const mocks = {
AuthenticatePusher: mockAuthenticatePusher,
};

function mockCall(command, apiCommandParameters, tag) {
const mockResponse = mocks[command] && mocks[command](apiCommandParameters);
if (!mockResponse || !_.isArray(mockResponse.onyxData)) {
function mockCall(command: string, apiCommandParameters: Record<string, unknown>, tag: string) {
// @ts-expect-error Broken types
const mockResponse = mocks[command] == null ? undefined : mocks[command](apiCommandParameters);
if (!mockResponse || !Array.isArray(mockResponse.onyxData)) {
Log.warn(`[${tag}] for command ${command} is not mocked yet!`);
return;
}
Expand All @@ -37,12 +37,10 @@ function mockCall(command, apiCommandParameters, tag) {
* 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}
* @param command - Name of API command to call.
* @param apiCommandParameters - Parameters to send to the API.
*/
function write(command, apiCommandParameters = {}) {
function write(command: string, apiCommandParameters: Record<string, unknown> = {}) {
return mockCall(command, apiCommandParameters, 'API.write');
}

Expand All @@ -54,24 +52,20 @@ function write(command, apiCommandParameters = {}) {
* 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}
* @param command - Name of API command to call.
* @param apiCommandParameters - Parameters to send to the API.
*/
function makeRequestWithSideEffects(command, apiCommandParameters = {}) {
function makeRequestWithSideEffects(command: string, 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}
* @param command - Name of API command to call.
* @param apiCommandParameters - Parameters to send to the API.
*/
function read(command, apiCommandParameters) {
function read(command: string, apiCommandParameters: Record<string, unknown>) {
return mockCall(command, apiCommandParameters, 'API.read');
}

Expand Down
6 changes: 0 additions & 6 deletions src/libs/E2E/apiMocks/authenticatePusher.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/libs/E2E/apiMocks/authenticatePusher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Response from '@src/types/onyx/Response';

export default (): Response => ({
// @ts-expect-error Broken types
auth: '268df511a204fbb60884:fcfbf8f7098206811b407f04a2bf5616ce4049d2a3db54c5aeecc92fb8acd3c8',
jsonCode: 200,
requestID: '8187e2621e905a7d-VIE',
onyxData: [],
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default ({email}) => ({
import Response from '@src/types/onyx/Response';

export default ({email}: {email: string}): Response => ({
onyxData: [
{
onyxMethod: 'merge',
Expand Down
Loading
Loading