Skip to content

Commit

Permalink
feat(spawn): added the spawn helper function.
Browse files Browse the repository at this point in the history
  • Loading branch information
thekeogh committed Oct 10, 2023
1 parent 0dab724 commit f91fd79
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 247 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ const user = new Factory<User>({

> The specification of the type `<User>` during Factory initialisation is entirely optional.
### `spawn(count: number, data: (faker: Faker) => T)`

To create multiple `Factory` instances simultaneously, you can utilise the `spawn` helper method, which generates an array of `Factory` instances:

```typescript
import { spawn } from "factoryse";

// Spawn an array of 5 Factory instances
const users: Factory<User>[] = spawn(5, faker => ({
name: faker.person.fullName(),
}));
```

When initialising your objects, you have the flexibility to use the [Faker](https://github.com/faker-js/faker) closure to easily generate random data. However, if you prefer, you can provide static strings or values instead of random data.

### `add(key: string, value: any)`

This method adds a new key-value pair to the object. You have the flexibility to use dot notation for nesting keys within your schema:
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "@src/Factory";
export * from "@src/spawn";
16 changes: 16 additions & 0 deletions src/spawn.ts
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)));
}
247 changes: 0 additions & 247 deletions tests/Factory.testold.ts

This file was deleted.

23 changes: 23 additions & 0 deletions tests/spawn.test.ts
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);
}
}
});

0 comments on commit f91fd79

Please sign in to comment.