-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spawn): added the spawn helper function.
- Loading branch information
Showing
5 changed files
with
55 additions
and
247 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "@src/Factory"; | ||
export * from "@src/spawn"; |
This file contains 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,16 @@ | ||
import { Faker, faker } from "@faker-js/faker"; | ||
|
||
import { Factory } from "@src/Factory"; | ||
|
||
/** | ||
* Generate an array of Factory instances. | ||
* | ||
* @param count - The number of Factory instances to create. | ||
* @param data - A callback function that generates the schema using Faker. | ||
* | ||
* @remarks | ||
* This function takes a callback function that uses Faker to create a schema. It returns an array of Factory instances. | ||
*/ | ||
export function spawn<T extends object = Record<string, any>>(count: number, data: (faker: Faker) => T): Factory[] { | ||
return Array.from({ length: count }, () => new Factory<T>(data(faker))); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,23 @@ | ||
import { beforeEach, describe, expect, it } from "vitest"; | ||
|
||
import { Factory } from "@src/Factory"; | ||
import { spawn } from "@src/spawn"; | ||
|
||
it("Spawns one Factory instance", () => { | ||
expect(spawn(1, f => ({ name: f.person.fullName() }))).toHaveLength(1); | ||
}); | ||
it("Spawns multiple Factory instances", () => { | ||
expect(spawn(5, f => ({ name: f.person.fullName() }))).toHaveLength(5); | ||
}); | ||
it("All spawns are an instance of Factory", () => { | ||
expect(spawn(5, f => ({ name: f.person.fullName() }))[0]).toBeInstanceOf(Factory); | ||
}); | ||
it("Spawns multiple Factory instances with varying data", () => { | ||
const count = 5; | ||
const factories = spawn(count, f => ({ id: f.string.uuid() })); | ||
for (let i = 0; i < count; i++) { | ||
for (let j = i + 1; j < count; j++) { | ||
expect(factories[i].get().id).not.toBe(factories[j].get().id); | ||
} | ||
} | ||
}); |