Skip to content

Commit 614fe8b

Browse files
committed
Fix handling of array schemas in factory function
1 parent 78cbaf8 commit 614fe8b

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [2.0.0] - 2024-09-24
8+
## [2.0.1] - 2023-09-26
9+
10+
### Fixed
11+
- Corrected handling of array schemas in the factory function
12+
- Added warning when `isSample` option is incorrectly used with array schemas
13+
14+
## [2.0.0] - 2024-09-26
915

1016
### Changed
1117
- Refactored `factory` function to use an options object for additional parameters.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ Returns an array of generated objects.
8080
#### Simple Array
8181
```javascript
8282
const schema = ['id', 'name', 'email'];
83+
const users = factory(schema, { quantity: 2 });
8384
```
8485

86+
This above schema will generate objects with 'id', 'name', and 'email' properties, each using the default generator for its field name. Note that the `isSample` option should not be used when using an array as the schema.
87+
8588
#### Object with Types
8689
```javascript
8790
const schema = {

__tests__/factory.test.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@ describe('mock-factory', () => {
1717
expect(typeof result[0].somethingelse).toBe('string')
1818
})
1919

20+
test('generates data based on array schema without isSample flag', () => {
21+
const schema = ['id', 'name', 'email']
22+
const result = factory(schema, { quantity: 2 })
23+
24+
expect(result).toHaveLength(2)
25+
result.forEach(item => {
26+
expect(item).toHaveProperty('id')
27+
expect(item).toHaveProperty('name')
28+
expect(item).toHaveProperty('email')
29+
})
30+
})
31+
32+
test('generates data based on array schema', () => {
33+
const schema = ['id', 'name', 'email']
34+
const result = factory(schema, { quantity: 2 })
35+
36+
expect(result).toHaveLength(2)
37+
result.forEach(item => {
38+
expect(item).toHaveProperty('id')
39+
expect(item).toHaveProperty('name')
40+
expect(item).toHaveProperty('email')
41+
})
42+
})
43+
2044
test('generates data based on object schema with explicit types', () => {
2145
const schema = {
2246
id: 'uuid',
@@ -115,28 +139,28 @@ describe('mock-factory', () => {
115139
})
116140

117141
test('supports old API with number as second argument', () => {
118-
const schema = { id: 'uniqueInt', name: 'fullName' };
119-
const result = factory(schema, 3);
142+
const schema = { id: 'uniqueInt', name: 'fullName' }
143+
const result = factory(schema, 3)
120144

121-
expect(result).toHaveLength(3);
145+
expect(result).toHaveLength(3)
122146
result.forEach(item => {
123-
expect(item).toHaveProperty('id');
124-
expect(item).toHaveProperty('name');
125-
});
126-
});
147+
expect(item).toHaveProperty('id')
148+
expect(item).toHaveProperty('name')
149+
})
150+
})
127151

128152
test('new API and old API produce same results', () => {
129-
const schema = { id: 'uniqueInt', name: 'fullName' };
130-
const seed = 123;
153+
const schema = { id: 'uniqueInt', name: 'fullName' }
154+
const seed = 123
131155

132-
setSeed(seed);
133-
const resultOld = factory(schema, 2);
156+
setSeed(seed)
157+
const resultOld = factory(schema, 2)
134158

135-
setSeed(seed);
136-
const resultNew = factory(schema, { quantity: 2 });
159+
setSeed(seed)
160+
const resultNew = factory(schema, { quantity: 2 })
137161

138-
expect(resultOld).toEqual(resultNew);
139-
});
162+
expect(resultOld).toEqual(resultNew)
163+
})
140164

141165
describe('uniqueInt generator', () => {
142166
beforeEach(() => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "shujau-mock-factory",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "A flexible fake data generator based on Faker.js",
55
"main": "src/index.js",
66
"type": "module",

src/factory.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ export const factory = (schemaOrSample, options = {}) => {
8484
isSample = false
8585
} = options
8686

87+
if (Array.isArray(schemaOrSample) && isSample) {
88+
console.warn('Warning: isSample option is ignored for array schemas. Arrays are always treated as explicit schemas.');
89+
}
90+
8791
if (seed !== undefined) {
8892
setSeed(seed)
8993
}

0 commit comments

Comments
 (0)