-
Notifications
You must be signed in to change notification settings - Fork 49
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
feat: add generatorLibrary options and allow faker to select #93
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c19722
refactor: add CasualMockValueGenerator class
MH4GF 9866d14
refactor: move function tokens to mockValueGenerator
MH4GF f6ef697
feat(deps): execute `yarn add @faker-js/faker --dev`
MH4GF 5a8cd1b
fix(deps): remove `@types/faker` package
MH4GF f05238e
feat: add FakerMockValueGenerator and fakerFunctionTokens
MH4GF 9acbdb2
refactor: support to switch mockValueGenerator
MH4GF 78734ea
fix(faker): specify refDate to fix value of faker.date.past()
MH4GF 846b86a
feat: add generateLibrary option
MH4GF a048815
feat(docs): update README to add description of generateLibrary option
MH4GF 5a9262f
Merge remote-tracking branch 'origin/main' into add-generator-library…
ardeois 9d351f6
test: update snapshots
ardeois 8447044
Merge branch 'main' into add-generator-library-option
ardeois File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { faker } from '@faker-js/faker'; | ||
import casual from 'casual'; | ||
|
||
interface MockValueGenerator { | ||
dynamicValues: boolean; | ||
word: () => string; | ||
uuid: () => string; | ||
boolean: () => boolean | string; | ||
integer: () => number | string; | ||
float: () => number | string; | ||
date: () => string; | ||
seed: (seed: number) => void; | ||
} | ||
|
||
type MockValueGeneratorOptions = { | ||
dynamicValues: boolean; | ||
}; | ||
|
||
type FunctionTokens = Record<'import' | 'seed' | 'seedFunction', string>; | ||
|
||
type SetupMockValueGeneratorOptions = { | ||
generateLibrary: 'casual' | 'faker'; | ||
dynamicValues: boolean; | ||
}; | ||
|
||
class CasualMockValueGenerator implements MockValueGenerator { | ||
dynamicValues: boolean; | ||
|
||
constructor(opts: MockValueGeneratorOptions) { | ||
this.dynamicValues = opts.dynamicValues; | ||
} | ||
|
||
word = () => (this.dynamicValues ? `casual.word` : `'${casual.word}'`); | ||
uuid = () => (this.dynamicValues ? `casual.uuid` : `'${casual.uuid}'`); | ||
boolean = () => (this.dynamicValues ? `casual.boolean` : casual.boolean); | ||
integer = () => (this.dynamicValues ? `casual.integer(0, 9999)` : `${casual.integer(0, 9999)}`); | ||
float = () => | ||
this.dynamicValues | ||
? `Math.round(casual.double(0, 10) * 100) / 100` | ||
: `${Math.round(casual.double(0, 10) * 100) / 100}`; | ||
date = () => | ||
this.dynamicValues | ||
? `new Date(casual.unix_time).toISOString()` | ||
: `'${new Date(casual.unix_time).toISOString()}'`; | ||
seed = (seed: number) => casual.seed(seed); | ||
} | ||
|
||
const casualFunctionTokens: FunctionTokens = { | ||
import: `import casual from 'casual';`, | ||
seed: 'casual.seed(0);', | ||
seedFunction: 'export const seedMocks = (seed: number) => casual.seed(seed);', | ||
}; | ||
|
||
class FakerMockValueGenerator implements MockValueGenerator { | ||
dynamicValues: boolean; | ||
|
||
constructor(opts: MockValueGeneratorOptions) { | ||
this.dynamicValues = opts.dynamicValues; | ||
} | ||
|
||
word = () => (this.dynamicValues ? `faker.lorem.word()` : `'${faker.lorem.word()}'`); | ||
uuid = () => (this.dynamicValues ? `faker.datatype.uuid()` : `'${faker.datatype.uuid()}'`); | ||
boolean = () => (this.dynamicValues ? `faker.datatype.boolean()` : faker.datatype.boolean()); | ||
integer = () => | ||
this.dynamicValues | ||
? `faker.datatype.number({ min: 0, max: 9999 })` | ||
: faker.datatype.number({ min: 0, max: 9999 }); | ||
float = () => | ||
this.dynamicValues | ||
? `faker.datatype.float({ min: 0, max: 10, precision: 0.1 })` | ||
: faker.datatype.float({ min: 0, max: 10, precision: 0.1 }); | ||
date = () => | ||
this.dynamicValues | ||
? `faker.date.past().toISOString(1, new Date(2022, 0))` | ||
: `'${faker.date.past(1, new Date(2022, 0)).toISOString()}'`; | ||
seed = (seed: number) => faker.seed(seed); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The compatibility of the execution results of each function is checked here: |
||
|
||
const fakerFunctionTokens: FunctionTokens = { | ||
import: `import { faker } from '@faker-js/faker';`, | ||
seed: 'faker.seed(0);', | ||
seedFunction: 'export const seedMocks = (seed: number) => faker.seed(seed);', | ||
}; | ||
|
||
export const setupMockValueGenerator = ({ | ||
generateLibrary, | ||
dynamicValues, | ||
}: SetupMockValueGeneratorOptions): MockValueGenerator => { | ||
switch (generateLibrary) { | ||
case 'casual': | ||
return new CasualMockValueGenerator({ dynamicValues }); | ||
case 'faker': | ||
return new FakerMockValueGenerator({ dynamicValues }); | ||
} | ||
}; | ||
|
||
export const setupFunctionTokens = (generateLibrary: 'casual' | 'faker'): FunctionTokens => { | ||
switch (generateLibrary) { | ||
case 'casual': | ||
return casualFunctionTokens; | ||
case 'faker': | ||
return fakerFunctionTokens; | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was probably left in there because it was forgotten to erase it, but now the type definitions have been imported into the repository and are no longer needed.
5a8cd1b