-
-
Notifications
You must be signed in to change notification settings - Fork 497
Feature faker seed #2180
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
Closed
Closed
Feature faker seed #2180
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,105 @@ | ||
| import { describe, it, expect, vi } from 'vitest'; | ||
| import { generateMSWImports } from './index'; | ||
| import { OutputMockType } from '@orval/core'; | ||
|
|
||
| // Mock the generateDependencyImports function | ||
| vi.mock('@orval/core', async () => { | ||
| const actual = | ||
| await vi.importActual<typeof import('@orval/core')>('@orval/core'); | ||
| return { | ||
| ...actual, | ||
| generateDependencyImports: vi.fn( | ||
| () => | ||
| 'import { http, HttpResponse } from "msw";\nimport { faker } from "@faker-js/faker";', | ||
| ), | ||
| }; | ||
| }); | ||
|
|
||
| describe('generateMSWImports', () => { | ||
| const mockParams = { | ||
| implementation: 'mock-implementation', | ||
| imports: [], | ||
| specsName: { test: 'test-specs' }, | ||
| hasSchemaDir: false, | ||
| isAllowSyntheticDefaultImports: false, | ||
| }; | ||
|
|
||
| describe('faker seed functionality', () => { | ||
| it('should return import block without seed when seed is undefined', () => { | ||
| const result = generateMSWImports({ | ||
| ...mockParams, | ||
| options: { type: OutputMockType.MSW }, | ||
| }); | ||
|
|
||
| expect(result).toBe( | ||
| 'import { http, HttpResponse } from "msw";\nimport { faker } from "@faker-js/faker";', | ||
| ); | ||
| }); | ||
|
|
||
| it('should add faker.seed call with numeric seed', () => { | ||
| const result = generateMSWImports({ | ||
| ...mockParams, | ||
| options: { type: OutputMockType.MSW, seed: 12345 }, | ||
| }); | ||
|
|
||
| expect(result).toBe( | ||
| 'import { http, HttpResponse } from "msw";\nimport { faker } from "@faker-js/faker";\nfaker.seed(12345);\n\n', | ||
| ); | ||
| }); | ||
|
|
||
| it('should add faker.seed call with array seed', () => { | ||
| const result = generateMSWImports({ | ||
| ...mockParams, | ||
| options: { type: OutputMockType.MSW, seed: [123, 456, 789] }, | ||
| }); | ||
|
|
||
| expect(result).toBe( | ||
| 'import { http, HttpResponse } from "msw";\nimport { faker } from "@faker-js/faker";\nfaker.seed([123, 456, 789]);\n\n', | ||
| ); | ||
| }); | ||
|
|
||
| it('should add faker.seed call with single element array', () => { | ||
| const result = generateMSWImports({ | ||
| ...mockParams, | ||
| options: { type: OutputMockType.MSW, seed: [42] }, | ||
| }); | ||
|
|
||
| expect(result).toBe( | ||
| 'import { http, HttpResponse } from "msw";\nimport { faker } from "@faker-js/faker";\nfaker.seed([42]);\n\n', | ||
| ); | ||
| }); | ||
|
|
||
| it('should add faker.seed call with zero seed', () => { | ||
| const result = generateMSWImports({ | ||
| ...mockParams, | ||
| options: { type: OutputMockType.MSW, seed: 0 }, | ||
| }); | ||
|
|
||
| expect(result).toBe( | ||
| 'import { http, HttpResponse } from "msw";\nimport { faker } from "@faker-js/faker";\nfaker.seed(0);\n\n', | ||
| ); | ||
| }); | ||
|
|
||
| it('should not add seed when array is empty', () => { | ||
| const result = generateMSWImports({ | ||
| ...mockParams, | ||
| options: { type: OutputMockType.MSW, seed: [] }, | ||
| }); | ||
|
|
||
| expect(result).toBe( | ||
| 'import { http, HttpResponse } from "msw";\nimport { faker } from "@faker-js/faker";', | ||
| ); | ||
| }); | ||
|
|
||
| it('should not add seed when options is undefined', () => { | ||
| const result = generateMSWImports({ | ||
| ...mockParams, | ||
| options: undefined, | ||
| }); | ||
|
|
||
| expect(result).toBe( | ||
| 'import { http, HttpResponse } from "msw";\nimport { faker } from "@faker-js/faker";', | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -53,13 +53,19 @@ export const generateMSWImports: GenerateMockImports = ({ | |
| isAllowSyntheticDefaultImports, | ||
| options, | ||
| }) => { | ||
| return generateDependencyImports( | ||
| const importBlock = generateDependencyImports( | ||
| implementation, | ||
| [...getMSWDependencies(options), ...imports], | ||
| specsName, | ||
| hasSchemaDir, | ||
| isAllowSyntheticDefaultImports, | ||
| ); | ||
|
|
||
| const seed = options?.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. I think faker's concerns should be grouped in faker-related files like |
||
| if (seed !== undefined && !(Array.isArray(seed) && seed.length === 0)) { | ||
| return `${importBlock}\nfaker.seed(${Array.isArray(seed) ? `[${seed.join(', ')}]` : seed});\n\n`; | ||
| } | ||
| return importBlock; | ||
| }; | ||
|
|
||
| const generateDefinition = ( | ||
|
|
||
This file contains hidden or 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
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.
We need documentation for the property in the reference too.
https://orval.dev/reference/configuration/output#mock