Skip to content
This repository was archived by the owner on Aug 7, 2018. It is now read-only.

Commit 0897eaa

Browse files
author
Gregor Woiwode
committed
feat(sketches): enable use of index inside factory function
1 parent 12878a2 commit 0897eaa

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/sketches.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { OverwritableSketch } from './overwritable-sketch';
22

3-
export declare type PartialFactory<T> = { [key in keyof T]?: () => T[key] };
3+
export declare type PartialFactory<T> = {
4+
[key in keyof T]?: (index: number) => T[key]
5+
};
46

57
export class Sketches<T> {
68
constructor(private _sketches: Array<OverwritableSketch<T>>) {}
@@ -16,8 +18,8 @@ export class Sketches<T> {
1618

1719
private _resolve(generators: PartialFactory<T>): Partial<T> {
1820
return Object.keys(generators)
19-
.map(key => ({
20-
[key]: (generators as any)[key]()
21+
.map((key, index) => ({
22+
[key]: (generators as any)[key](index)
2123
}))
2224
.reduce(
2325
(combinedOverrides, override) => ({

test/create-list-of-models.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,17 @@ describe('When a list of test data is needed', () => {
3434

3535
expect(models[0].id).toBe(id);
3636
});
37+
38+
it('should pass the index of the respective model', () => {
39+
const id = '12-34-56-78-';
40+
const expectedId = '12-34-56-78-0';
41+
42+
const models = Kentan.sketch(ForEmpty)
43+
.take(1)
44+
.models({
45+
id: index => `${id}${index}`
46+
});
47+
48+
expect(models[0].id).toBe(expectedId);
49+
});
3750
});

0 commit comments

Comments
 (0)