Skip to content

Commit

Permalink
fix(msw): when use useDates, convert faker values to Date instance (
Browse files Browse the repository at this point in the history
#1645)

* fix(msw): when use `useDates`, convert faker values to `Date` instance

* chore: add test case

* chore: regenerate sample apps
  • Loading branch information
soartec-lab authored Sep 28, 2024
1 parent a752a3e commit 535bb65
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/mock/src/faker/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,15 @@ export const getMockScalar = ({
};

if (item.format && ALL_FORMAT[item.format]) {
const dateFormats = ['date', 'date-time'];

const value =
context.output.override.useDates && dateFormats.includes(item.format)
? `new Date(${ALL_FORMAT[item.format]})`
: `${ALL_FORMAT[item.format]}`;

return {
value: getNullable(`${ALL_FORMAT[item.format]}`, item.nullable),
value: getNullable(value, item.nullable),
imports: [],
name: item.name,
overrided: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import { faker } from '@faker-js/faker';
import { HttpResponse, delay, http } from 'msw';
import { DomainStatusEnum } from '../model';
import type { Pet, Pets } from '../model';

export const getListPetsResponseMock = (): Pets =>
Expand All @@ -16,6 +17,10 @@ export const getListPetsResponseMock = (): Pets =>
email: faker.helpers.arrayElement([faker.internet.email(), undefined]),
id: (() => faker.number.int({ min: 1, max: 99999 }))(),
name: (() => faker.person.lastName())(),
status: faker.helpers.arrayElement([
faker.helpers.arrayElement(Object.values(DomainStatusEnum)),
undefined,
]),
tag: (() => faker.person.lastName())(),
}));

Expand All @@ -25,6 +30,10 @@ export const getCreatePetsResponseMock = (
email: faker.helpers.arrayElement([faker.internet.email(), undefined]),
id: faker.number.int({ min: undefined, max: undefined }),
name: (() => faker.person.lastName())(),
status: faker.helpers.arrayElement([
faker.helpers.arrayElement(Object.values(DomainStatusEnum)),
undefined,
]),
tag: (() => faker.person.lastName())(),
...overrideResponse,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Generated by orval v7.1.1 🍺
* Do not edit manually.
* Swagger Petstore
* OpenAPI spec version: 1.0.0
*/

export type DomainStatusEnum =
(typeof DomainStatusEnum)[keyof typeof DomainStatusEnum];

// eslint-disable-next-line @typescript-eslint/no-redeclare
export const DomainStatusEnum = {
new: 'new',
sold: 'sold',
uknown: 'uknown',
} as const;
1 change: 1 addition & 0 deletions samples/vue-query/vue-query-basic/src/api/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

export * from './createPetsBody';
export * from './domainStatusEnum';
export * from './error';
export * from './listPetsParams';
export * from './pet';
Expand Down
2 changes: 2 additions & 0 deletions samples/vue-query/vue-query-basic/src/api/model/pet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
* Swagger Petstore
* OpenAPI spec version: 1.0.0
*/
import type { DomainStatusEnum } from './domainStatusEnum';

export interface Pet {
email?: string;
id: number;
name: string;
status?: DomainStatusEnum;
tag?: string;
}
11 changes: 11 additions & 0 deletions tests/configs/mock.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,15 @@ export default defineConfig({
mock: true,
},
},
useDates: {
input: '../specifications/format.yaml',
output: {
target: '../generated/mock/useDates/endpoints.ts',
schemas: '../generated/mock/useDates/model',
mock: true,
override: {
useDates: true,
},
},
},
});
49 changes: 49 additions & 0 deletions tests/specifications/format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: format test
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
- name: testId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
components:
schemas:
Pet:
type: object
required:
- birthDate
- createdAt
properties:
birthDate:
type: string
format: date
createdAt:
type: string
format: date-time

0 comments on commit 535bb65

Please sign in to comment.