Skip to content

Commit

Permalink
resolved iteration error
Browse files Browse the repository at this point in the history
  • Loading branch information
Na'aman Hirschfeld authored and Na'aman Hirschfeld committed Jul 5, 2021
1 parent f1b115f commit 804bdc2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@
[1.1.5]

- updated build options

[1.1.6]

- fix iteration error
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "interface-forge",
"author": "Na'aman Hirschfeld",
"version": "1.1.5",
"version": "1.1.6",
"license": "MIT",
"description": "Gracefully generate testing data using TypeScript",
"keywords": [
Expand Down
24 changes: 14 additions & 10 deletions src/type-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ export class TypeFactory<T> {
}
}

private async parseOptions(
private parseOptions(
options: FactoryBuildOptions<T> | undefined,
iteration: number,
): Promise<[overrides: FactoryOptions<Partial<T>> | undefined, factory: FactoryFunction<T> | undefined]> {
): [
overrides: FactoryOptions<Partial<T>> | undefined,
factory: FactoryFunction<T> | undefined,
] {
const overrides = (
options
? Reflect.has(options, 'overrides')
Expand All @@ -82,13 +84,11 @@ export class TypeFactory<T> {
: {}
: {}
) as FactoryOptions<T>;
const resolvedOverrides =
typeof overrides === 'function' ? await overrides(iteration) : overrides;
const factory =
options && Reflect.has(options, 'factory')
? (Reflect.get(options, 'factory') as FactoryFunction<T>)
: this.factory;
return [resolvedOverrides, factory];
return [overrides, factory];
}

resetCounter(value = 0): void {
Expand All @@ -98,16 +98,20 @@ export class TypeFactory<T> {
async build(options?: FactoryBuildOptions<T>): Promise<T> {
const iteration = this.counter;
this.counter++;
const [overrides, factory] = await this.parseOptions(options, iteration)
const defaults = await this.defaults
const [overrides, factory] = this.parseOptions(options);
const defaults = await this.defaults;
const mergedSchema = Object.assign(
{},
defaults,
overrides,
await Promise.resolve(
typeof overrides === 'function'
? overrides(iteration)
: overrides,
),
);
this.validateSchema(mergedSchema);
const value = await TypeFactory.parseSchema<T>(mergedSchema, iteration);
return factory ? factory(value, iteration) : value
return factory ? factory(value, iteration) : value;
}

async batch(size: number, options?: FactoryBuildOptions<T>): Promise<T[]> {
Expand Down
15 changes: 15 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ describe('InterfaceFactory', () => {
name: 'newObject',
});
});
it('merges options correctly when passed options async function', async () => {
const factoryOne = new TypeFactory<ComplexObject>(defaults)
const factoryTwo = new TypeFactory<ComplexObject>(async () => {
const defaults = await factoryOne.build()
return defaults
});
expect(
await factoryTwo.build({
overrides: async () => Promise.resolve({ name: 'newObject' }),
}),
).toStrictEqual<ComplexObject>({
...defaults,
name: 'newObject',
});
});
});
describe('.batch', () => {
it('returns an array of unique objects', async () => {
Expand Down

0 comments on commit 804bdc2

Please sign in to comment.