11/* eslint-disable @typescript-eslint/no-explicit-any */
22import { CLASS_META_KEY } from '@/constants/meta.constants'
3- import { getGenerateArrayLength , randomBoolean } from './common'
3+ import faker from '@faker-js/faker'
4+ import { getGenerateArrayLength , IsGroupsIntersect , randomBoolean } from './common'
45import { MetadataStorage } from './meta-storage'
56import { BaseClass , BasePropertyConfig , MockPropertyMetadata } from './types-helper'
67
7- export interface CreateMockOptions < IsArray extends boolean = false > extends BasePropertyConfig {
8+ export interface CreateMockOptions < IsArray extends boolean = false > extends Omit < BasePropertyConfig , 'partial' > {
89 /**
910 * if true, will generate an array
1011 * @default false
1112 */
1213 array ?: IsArray
14+
15+ /**
16+ * seed for generating same random value
17+ */
18+ seed ?: number | number [ ]
1319}
1420
1521export function createMock < T extends BaseClass , IsArray extends boolean = false > (
1622 Entity : T ,
1723 options : CreateMockOptions < IsArray > = { }
1824) : IsArray extends false ? T : T [ ] {
25+ if ( options . seed ) {
26+ faker . seed ( options . seed )
27+ delete options . seed
28+ }
1929 if ( ! options . array ) {
2030 const metas = MetadataStorage . instance . getClassMetadatas ( Entity )
2131 const entity = new Entity ( )
2232 metas . map ( meta => {
2333 const {
2434 target,
2535 propertyName,
26- alwaysRandom = true ,
36+ alwaysRandom : _alwaysRandom ,
2737 randomResult,
2838 partialResult,
2939 partial,
30- // groups = [] ,
40+ groups,
3141 array = false ,
3242 mockFn,
3343 entityFn
3444 } = meta
3545 if ( propertyName === CLASS_META_KEY ) return
36- const exclude = partial === 'exclude'
46+ const exclude = partial === 'exclude' || ! IsGroupsIntersect ( options . groups , groups )
3747 const maybePartial = partial === 'partial' || partial === true
48+ const alwaysRandom = options . alwaysRandom ?? _alwaysRandom ?? true
3849
3950 const updateMeta = ( newMeta : Partial < MockPropertyMetadata > ) => {
4051 MetadataStorage . instance . updateMockMetadata ( target , propertyName , newMeta )
@@ -48,8 +59,8 @@ export function createMock<T extends BaseClass, IsArray extends boolean = false>
4859 } else if ( typeof entityFn === 'function' ) {
4960 const PropertyEntity = entityFn ( )
5061 return createMock ( PropertyEntity , {
51- array : false ,
52- ... otherOptions
62+ ... otherOptions ,
63+ array : false
5364 } )
5465 }
5566 return undefined
@@ -64,11 +75,11 @@ export function createMock<T extends BaseClass, IsArray extends boolean = false>
6475 } else if ( typeof entityFn === 'function' ) {
6576 const PropertyEntity = entityFn ( )
6677 return createMock ( PropertyEntity , {
78+ ...otherOptions ,
6779 array : true ,
6880 length,
6981 min,
70- max,
71- ...otherOptions
82+ max
7283 } )
7384 }
7485 return undefined
@@ -92,7 +103,12 @@ export function createMock<T extends BaseClass, IsArray extends boolean = false>
92103 if ( ! shouldCreate ) return delete entity [ propertyName ]
93104 }
94105
95- propertyValue = ! array ? createSingleValue ( meta ) : createListValue ( meta )
106+ const newMeta : MockPropertyMetadata = {
107+ ...meta ,
108+ groups : options . groups ,
109+ alwaysRandom
110+ }
111+ propertyValue = ! array ? createSingleValue ( newMeta ) : createListValue ( newMeta )
96112 updateMeta ( { randomResult : propertyValue } )
97113 entity [ propertyName ] = propertyValue
98114 } )
0 commit comments