Skip to content

Commit

Permalink
Fix handling of array schemas in factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
asadhshujau committed Sep 26, 2024
1 parent 78cbaf8 commit 614fe8b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2024-09-24
## [2.0.1] - 2023-09-26

### Fixed
- Corrected handling of array schemas in the factory function
- Added warning when `isSample` option is incorrectly used with array schemas

## [2.0.0] - 2024-09-26

### Changed
- Refactored `factory` function to use an options object for additional parameters.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ Returns an array of generated objects.
#### Simple Array
```javascript
const schema = ['id', 'name', 'email'];
const users = factory(schema, { quantity: 2 });
```

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.

#### Object with Types
```javascript
const schema = {
Expand Down
54 changes: 39 additions & 15 deletions __tests__/factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ describe('mock-factory', () => {
expect(typeof result[0].somethingelse).toBe('string')
})

test('generates data based on array schema without isSample flag', () => {
const schema = ['id', 'name', 'email']
const result = factory(schema, { quantity: 2 })

expect(result).toHaveLength(2)
result.forEach(item => {
expect(item).toHaveProperty('id')
expect(item).toHaveProperty('name')
expect(item).toHaveProperty('email')
})
})

test('generates data based on array schema', () => {
const schema = ['id', 'name', 'email']
const result = factory(schema, { quantity: 2 })

expect(result).toHaveLength(2)
result.forEach(item => {
expect(item).toHaveProperty('id')
expect(item).toHaveProperty('name')
expect(item).toHaveProperty('email')
})
})

test('generates data based on object schema with explicit types', () => {
const schema = {
id: 'uuid',
Expand Down Expand Up @@ -115,28 +139,28 @@ describe('mock-factory', () => {
})

test('supports old API with number as second argument', () => {
const schema = { id: 'uniqueInt', name: 'fullName' };
const result = factory(schema, 3);
const schema = { id: 'uniqueInt', name: 'fullName' }
const result = factory(schema, 3)

expect(result).toHaveLength(3);
expect(result).toHaveLength(3)
result.forEach(item => {
expect(item).toHaveProperty('id');
expect(item).toHaveProperty('name');
});
});
expect(item).toHaveProperty('id')
expect(item).toHaveProperty('name')
})
})

test('new API and old API produce same results', () => {
const schema = { id: 'uniqueInt', name: 'fullName' };
const seed = 123;
const schema = { id: 'uniqueInt', name: 'fullName' }
const seed = 123

setSeed(seed);
const resultOld = factory(schema, 2);
setSeed(seed)
const resultOld = factory(schema, 2)

setSeed(seed);
const resultNew = factory(schema, { quantity: 2 });
setSeed(seed)
const resultNew = factory(schema, { quantity: 2 })

expect(resultOld).toEqual(resultNew);
});
expect(resultOld).toEqual(resultNew)
})

describe('uniqueInt generator', () => {
beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shujau-mock-factory",
"version": "2.0.0",
"version": "2.0.1",
"description": "A flexible fake data generator based on Faker.js",
"main": "src/index.js",
"type": "module",
Expand Down
4 changes: 4 additions & 0 deletions src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export const factory = (schemaOrSample, options = {}) => {
isSample = false
} = options

if (Array.isArray(schemaOrSample) && isSample) {
console.warn('Warning: isSample option is ignored for array schemas. Arrays are always treated as explicit schemas.');
}

if (seed !== undefined) {
setSeed(seed)
}
Expand Down

0 comments on commit 614fe8b

Please sign in to comment.