Skip to content

Commit

Permalink
fix(msw): refactor to include duplication of mock imports in mocks in…
Browse files Browse the repository at this point in the history
… `split` and `split-tags` modes (#1214)

* chore: not mix hooks imports with mock imports

* chore: refactoring to define variables so that return values are clear

* chore: change the order of variable definitions to align with return values

* fix: return type definitions are  taken from `response.imports`
  • Loading branch information
soartec-lab committed Feb 10, 2024
1 parent 1aff031 commit 1c39aab
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
5 changes: 1 addition & 4 deletions packages/core/src/writers/split-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ export const writeSplitMode = async ({

mockData += builder.importsMock({
implementation: implementationMock,
imports: [
{ exports: imports, dependency: relativeSchemasPath },
{ exports: importsMock, dependency: relativeSchemasPath },
],
imports: [{ exports: importsMock, dependency: relativeSchemasPath }],
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/writers/split-tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ export const writeSplitTagsMode = async ({
});
mockData += builder.importsMock({
implementation: implementationMock,
imports: [
...importsForBuilder,
{ exports: importsMock, dependency: relativeSchemasPath },
],
imports: [{ exports: importsMock, dependency: relativeSchemasPath }],
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
15 changes: 11 additions & 4 deletions packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export const generateMSW = (
const functionName = `get${pascal(operationId)}Mock`;
const handlerName = `get${pascal(operationId)}MockHandler`;

const mockImplementation = isReturnHttpResponse
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''}): ${returnType} => (${value})\n\n`
: '';

const handlerImplementation = `
export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrideResponse?: ${returnType}` : ''}) => {
return http.${verb}('${route}', async () => {
Expand All @@ -101,14 +105,17 @@ export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrid
})
}\n`;

const includeResponseImports =
isReturnHttpResponse && !isTextPlain
? [...imports, ...response.imports]
: imports;

return {
implementation: {
function: isReturnHttpResponse
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''}): ${returnType} => (${value})\n\n`
: '',
function: mockImplementation,
handlerName: handlerName,
handler: handlerImplementation,
},
imports,
imports: includeResponseImports,
};
};

0 comments on commit 1c39aab

Please sign in to comment.