Skip to content

Commit

Permalink
Merge pull request #2 from knicklabs/fix/make-array
Browse files Browse the repository at this point in the history
Ensure make() always returns an array
  • Loading branch information
knicklabs authored Oct 16, 2019
2 parents 140ab3e + 9e6caf7 commit d6e67b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
5 changes: 2 additions & 3 deletions src/lib/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ export class Schema {
return postProcessSchema(preProcessSchema(schema))
}

make(num = 1, seed) {
const results = Array(num)
make(num = 0, seed) {
return Array(num)
.fill(0)
.map((_, index) => this.makeOne(seed ? seed + index : seed))
return results.length === 1 ? results[0] : results
}
}
43 changes: 24 additions & 19 deletions src/lib/__tests__/Schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,32 @@ describe('Schema', () => {
lastName: faker.name.lastName(),
})

test('make returns an empty schema by default', () => {
test('makeOne returns an empty schema by default', () => {
const schema = new Schema()
expect(schema.make()).toEqual({})
expect(schema.makeOne()).toEqual({})
})

test('make returns a single schema identical to blueprint by default', () => {
test('make returns an empty array by default', () => {
const schema = new Schema()
expect(schema.make()).toEqual([])
})

test('returns a single schema identical to blueprint by default', () => {
const personSchema = new Schema(blueprint)
expect(personSchema.make()).toEqual(blueprint)
expect(personSchema.makeOne()).toEqual(blueprint)
})

test('make returns schemas identical to blueprint by default', () => {
test('returns schemas identical to blueprint by default', () => {
const personSchema = new Schema(blueprint)
const results = personSchema.make(5)
expect(results).toHaveLength(5)
results.forEach(result => expect(result).toEqual(blueprint))
})

test('make returns a randomized schema when using Faker', () => {
test('returns a randomized schema when using Faker', () => {
const personSchema = new Schema(fakerBlueprint)
const person1 = personSchema.make()
const person2 = personSchema.make()
const person1 = personSchema.makeOne()
const person2 = personSchema.makeOne()
expect(person1).not.toEqual(personSchema)
expect(person2).not.toEqual(personSchema)
expect(person1.firstName).toBeDefined()
Expand All @@ -39,27 +44,27 @@ describe('Schema', () => {
expect(person1).not.toEqual(person2)
})

test('make returns a randomized schema with derived data when using faker', () => {
test('returns a randomized schema with derived data when using faker', () => {
const personSchema = new Schema({
...fakerBlueprint,
firstName: 'Bob',
lastName: 'Belcher',
fullName: ({ firstName, lastName }) => `${firstName} ${lastName}`
})
const person = personSchema.make()
expect(person.fullName).toEqual('Bob Belcher')
const people = personSchema.make(1)
expect(people[0].fullName).toEqual('Bob Belcher')
})

test('make returns the same randomized sequence when using Faker and seeded', () => {
test('returns the same randomized sequence when using Faker and seeded', () => {
const personSchema = new Schema(fakerBlueprint)
personSchema.setSeed(5)
expect(personSchema.seed).toEqual(5)
const person1A = personSchema.make()
const person2A = personSchema.make()
const person1A = personSchema.makeOne()
const person2A = personSchema.makeOne()
personSchema.setSeed(5)
expect(personSchema.seed).toEqual(5)
const person1B = personSchema.make()
const person2B = personSchema.make()
const person1B = personSchema.makeOne()
const person2B = personSchema.makeOne()
expect(person1A).toEqual(person1B)
expect(person2A).toEqual(person2B)
personSchema.setSeed(5)
Expand All @@ -68,10 +73,10 @@ describe('Schema', () => {
expect(people[1]).toEqual(person2B)
})

test('make returns the same results when invoked with seed argument', () => {
test('returns the same results when invoked with seed argument', () => {
const personSchema = new Schema(fakerBlueprint)
const person1 = personSchema.make(1, 123)
const person2 = personSchema.make(1, 124)
const person1 = personSchema.makeOne(123)
const person2 = personSchema.makeOne(124)
const people = personSchema.make(2, 123)
expect(people[0]).toEqual(person1)
expect(people[1]).toEqual(person2)
Expand Down

0 comments on commit d6e67b8

Please sign in to comment.