Skip to content

Commit

Permalink
feat(msw): possible to value of the mock handler function overridable (
Browse files Browse the repository at this point in the history
…#1186)

* chore: refactoring for mock `handlerImplementation`

* feat: possible to orverride reponse values in mock handler

* feat: call handler function in aggregate function

* chore: refactoring to use shared variable
  • Loading branch information
soartec-lab authored Jan 31, 2024
1 parent 992085b commit 0d05129
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/writers/target-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const generateTargetTags = (
implementationMock: {
function: operation.implementationMock.function,
handler: operation.implementationMock.handler,
handlerName: ' ' + operation.implementationMock.handlerName,
handlerName: ' ' + operation.implementationMock.handlerName + '()',
},
};

Expand All @@ -61,7 +61,8 @@ const generateTargetTags = (
handlerName:
currentOperation.implementationMock.handlerName +
',\n ' +
operation.implementationMock.handlerName,
operation.implementationMock.handlerName +
'()',
},
mutators: operation.mutator
? [...(currentOperation.mutators ?? []), operation.mutator]
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/writers/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const generateTarget = (
? ',\n '
: ' ';
acc.implementationMock.handlerName +=
handlerNameSeparator + operation.implementationMock.handlerName;
handlerNameSeparator + operation.implementationMock.handlerName + '()';

if (operation.mutator) {
acc.mutators.push(operation.mutator);
Expand Down
48 changes: 23 additions & 25 deletions packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,39 @@ export const generateMSW = (
}

const isResponseOverridable = value.includes(overrideVarName);

const returnType = response.definition.success;

const isTextPlain = response.contentTypes.includes('text/plain');
const isReturnHttpResponse = value && value !== 'undefined';

const returnType = response.definition.success;
const functionName = `get${pascal(operationId)}Mock`;

const handlerName = `get${pascal(operationId)}MockHandler`;

const handlerImplementation = `
export const ${handlerName} = http.${verb}('${route}', async () => {
await delay(${getDelay(override, !isFunction(mock) ? mock : undefined)});
return new HttpResponse(${
value && value !== 'undefined'
? isTextPlain
? `${functionName}()`
: `JSON.stringify(${functionName}())`
: null
},
{
status: 200,
headers: {
'Content-Type': '${isTextPlain ? 'text/plain' : 'application/json'}',
export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrideResponse?: ${returnType}` : ''}) => {
return http.${verb}('${route}', async () => {
await delay(${getDelay(override, !isFunction(mock) ? mock : undefined)});
return new HttpResponse(${
isReturnHttpResponse
? isTextPlain
? `${functionName}()`
: `JSON.stringify(overrideResponse ? overrideResponse : ${functionName}())`
: null
},
{
status: 200,
headers: {
'Content-Type': '${isTextPlain ? 'text/plain' : 'application/json'}',
}
}
}
)
})
`;
)
})
}\n`;

return {
implementation: {
function:
value && value !== 'undefined'
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''}): ${returnType} => (${value})\n\n`
: '',
function: isReturnHttpResponse
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''}): ${returnType} => (${value})\n\n`
: '',
handlerName: handlerName,
handler: handlerImplementation,
},
Expand Down

0 comments on commit 0d05129

Please sign in to comment.