Skip to content

Commit

Permalink
feat: add override function to empty response mocks (#1484)
Browse files Browse the repository at this point in the history
* add override function to empty response mocks

* add required responses property to test spec
  • Loading branch information
karlismelderis-mckinsey authored Jun 26, 2024
1 parent c2fff70 commit e038704
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 20 deletions.
9 changes: 3 additions & 6 deletions packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,17 @@ const generateDefinition = (

const delay = getDelay(override, !isFunction(mock) ? mock : undefined);
const isHandlerOverridden = isReturnHttpResponse && !isTextPlain;
const infoParam = isHandlerOverridden ? 'info' : '';
const infoParam = 'info';
const handlerImplementation = `
export const ${handlerName} = (${
isHandlerOverridden
? `overrideResponse?: ${returnType} | ((${infoParam}: Parameters<Parameters<typeof http.${verb}>[1]>[0]) => Promise<${returnType}> | ${returnType})`
: ''
}) => {
export const ${handlerName} = (overrideResponse?: ${returnType} | ((${infoParam}: Parameters<Parameters<typeof http.${verb}>[1]>[0]) => Promise<${returnType}> | ${returnType})) => {
return http.${verb}('${route}', ${
(isReturnHttpResponse && !isTextPlain) || delay !== false ? 'async' : ''
} (${infoParam}) => {${
delay !== false
? `await delay(${isFunction(delay) ? `(${delay})()` : delay});`
: ''
}
${isReturnHttpResponse ? '' : `if (typeof overrideResponse === 'function') {await overrideResponse(info); }`}
return new HttpResponse(${
isReturnHttpResponse
? isTextPlain
Expand Down
15 changes: 13 additions & 2 deletions samples/angular-app/src/api/endpoints/pets/pets.msw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const getListPetsMockHandler = (
) => {
return http.get('*/v:version/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -47,9 +48,18 @@ export const getListPetsMockHandler = (
});
};

export const getCreatePetsMockHandler = () => {
return http.post('*/v:version/pets', async () => {
export const getCreatePetsMockHandler = (
overrideResponse?:
| void
| ((
info: Parameters<Parameters<typeof http.post>[1]>[0],
) => Promise<void> | void),
) => {
return http.post('*/v:version/pets', async (info) => {
await delay(1000);
if (typeof overrideResponse === 'function') {
await overrideResponse(info);
}
return new HttpResponse(null, { status: 201 });
});
};
Expand All @@ -63,6 +73,7 @@ export const getShowPetByIdMockHandler = (
) => {
return http.get('*/v:version/pets/:petId', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand Down
16 changes: 14 additions & 2 deletions samples/basic/api/endpoints/petstoreFromFileSpecWithTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const getListPetsMockHandler = (
) => {
return http.get('*/v:version/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -160,9 +161,18 @@ export const getListPetsMockHandler = (
});
};

export const getCreatePetsMockHandler = () => {
return http.post('*/v:version/pets', async () => {
export const getCreatePetsMockHandler = (
overrideResponse?:
| void
| ((
info: Parameters<Parameters<typeof http.post>[1]>[0],
) => Promise<void> | void),
) => {
return http.post('*/v:version/pets', async (info) => {
await delay(1000);
if (typeof overrideResponse === 'function') {
await overrideResponse(info);
}
return new HttpResponse(null, { status: 201 });
});
};
Expand All @@ -176,6 +186,7 @@ export const getListPetsNestedArrayMockHandler = (
) => {
return http.get('*/v:version/pets-nested-array', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -198,6 +209,7 @@ export const getShowPetByIdMockHandler = (
) => {
return http.get('*/v:version/pets/:petId', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand Down
4 changes: 4 additions & 0 deletions samples/next-app-with-fetch/app/gen/pets/pets.msw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ export const getListPetsMockHandler = (
) => {
return http.get('*/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -440,6 +441,7 @@ export const getCreatePetsMockHandler = (
) => {
return http.post('*/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -462,6 +464,7 @@ export const getUpdatePetsMockHandler = (
) => {
return http.put('*/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -484,6 +487,7 @@ export const getShowPetByIdMockHandler = (
) => {
return http.get('*/pets/:petId', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const getListPetsMockHandler = (
) => {
return http.get('*/v:version/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -69,6 +70,7 @@ export const getCreatePetsMockHandler = (
) => {
return http.post('*/v:version/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -91,6 +93,7 @@ export const getShowPetByIdMockHandler = (
) => {
return http.get('*/v:version/pets/:petId', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const getListPetsMockHandler = (
) => {
return http.get('*/v:version/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -47,9 +48,18 @@ export const getListPetsMockHandler = (
});
};

export const getCreatePetsMockHandler = () => {
return http.post('*/v:version/pets', async () => {
export const getCreatePetsMockHandler = (
overrideResponse?:
| void
| ((
info: Parameters<Parameters<typeof http.post>[1]>[0],
) => Promise<void> | void),
) => {
return http.post('*/v:version/pets', async (info) => {
await delay(1000);
if (typeof overrideResponse === 'function') {
await overrideResponse(info);
}
return new HttpResponse(null, { status: 201 });
});
};
Expand All @@ -63,6 +73,7 @@ export const getShowPetByIdMockHandler = (
) => {
return http.get('*/v:version/pets/:petId', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const getListPetsMockHandler = (
) => {
return http.get('*/v:version/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -99,9 +100,18 @@ export const getListPetsMockHandler = (
});
};

export const getCreatePetsMockHandler = () => {
return http.post('*/v:version/pets', async () => {
export const getCreatePetsMockHandler = (
overrideResponse?:
| void
| ((
info: Parameters<Parameters<typeof http.post>[1]>[0],
) => Promise<void> | void),
) => {
return http.post('*/v:version/pets', async (info) => {
await delay(1000);
if (typeof overrideResponse === 'function') {
await overrideResponse(info);
}
return new HttpResponse(null, { status: 201 });
});
};
Expand All @@ -115,6 +125,7 @@ export const getListPetsNestedArrayMockHandler = (
) => {
return http.get('*/v:version/pets-nested-array', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -137,6 +148,7 @@ export const getShowPetByIdMockHandler = (
) => {
return http.get('*/v:version/pets/:petId', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const getListPetsMockHandler = (
) => {
return http.get('*/v:version/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -99,9 +100,18 @@ export const getListPetsMockHandler = (
});
};

export const getCreatePetsMockHandler = () => {
return http.post('*/v:version/pets', async () => {
export const getCreatePetsMockHandler = (
overrideResponse?:
| void
| ((
info: Parameters<Parameters<typeof http.post>[1]>[0],
) => Promise<void> | void),
) => {
return http.post('*/v:version/pets', async (info) => {
await delay(1000);
if (typeof overrideResponse === 'function') {
await overrideResponse(info);
}
return new HttpResponse(null, { status: 201 });
});
};
Expand All @@ -115,6 +125,7 @@ export const getListPetsNestedArrayMockHandler = (
) => {
return http.get('*/v:version/pets-nested-array', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -137,6 +148,7 @@ export const getShowPetByIdMockHandler = (
) => {
return http.get('*/v:version/pets/:petId', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const getListPetsMockHandler = (
) => {
return http.get('*/v:version/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -47,9 +48,18 @@ export const getListPetsMockHandler = (
});
};

export const getCreatePetsMockHandler = () => {
return http.post('*/v:version/pets', async () => {
export const getCreatePetsMockHandler = (
overrideResponse?:
| void
| ((
info: Parameters<Parameters<typeof http.post>[1]>[0],
) => Promise<void> | void),
) => {
return http.post('*/v:version/pets', async (info) => {
await delay(1000);
if (typeof overrideResponse === 'function') {
await overrideResponse(info);
}
return new HttpResponse(null, { status: 201 });
});
};
Expand All @@ -63,6 +73,7 @@ export const getShowPetByIdMockHandler = (
) => {
return http.get('*/v:version/pets/:petId', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const getListPetsMockHandler = (
) => {
return http.get('*/v:version/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -67,6 +68,7 @@ export const getCreatePetsMockHandler = (
) => {
return http.post('*/v:version/pets', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -89,6 +91,7 @@ export const getShowPetByIdMockHandler = (
) => {
return http.get('*/v:version/pets/:petId', async (info) => {
await delay(1000);

return new HttpResponse(
JSON.stringify(
overrideResponse !== undefined
Expand All @@ -102,9 +105,18 @@ export const getShowPetByIdMockHandler = (
});
};

export const getPostApiV1UserLogoutMockHandler = () => {
return http.post('*/api/v1/user/logout', async () => {
export const getPostApiV1UserLogoutMockHandler = (
overrideResponse?:
| void
| ((
info: Parameters<Parameters<typeof http.post>[1]>[0],
) => Promise<void> | void),
) => {
return http.post('*/api/v1/user/logout', async (info) => {
await delay(1000);
if (typeof overrideResponse === 'function') {
await overrideResponse(info);
}
return new HttpResponse(null, { status: 200 });
});
};
Expand Down
3 changes: 3 additions & 0 deletions tests/specifications/any-of.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ paths:
enum: ['B']
- type: string
enum: ['C']
responses:
'204':
description: Ok
components:
schemas:
A:
Expand Down

0 comments on commit e038704

Please sign in to comment.