This repository was archived by the owner on Aug 7, 2018. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +80
-0
lines changed
Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 11import { OverwritableSketch , OverwriteFn } from './overwritable-sketch' ;
2+ import { Sketches } from './sketches' ;
23
34export declare type Constructable < T > = new ( ) => T ;
45export declare type ModelFactory < T > = T ;
@@ -23,6 +24,14 @@ export class Sketch<T> implements OverwritableSketch<T> {
2324 }
2425 }
2526
27+ take ( length : number ) : Sketches < T > {
28+ const sketches = Array . from ( { length } ) . map (
29+ ( ) => new Sketch ( this . _token , this . _createModel ( ) )
30+ ) ;
31+
32+ return new Sketches ( sketches ) ;
33+ }
34+
2635 set ( map : OverwriteFn < T > ) : OverwritableSketch < T > {
2736 const model = this . _apply ( map , this . _createModel ( ) ) ;
2837 return new Sketch ( this . _token , model ) ;
Original file line number Diff line number Diff line change 1+ import { OverwritableSketch } from './overwritable-sketch' ;
2+
3+ export class Sketches < T > {
4+ constructor ( private _sketches : Array < OverwritableSketch < T > > ) { }
5+
6+ all ( ) : OverwritableSketch < T > [ ] {
7+ return this . _sketches ;
8+ }
9+
10+ models ( generators ?: { [ key in keyof T ] ?: ( ) => T [ key ] } ) : T [ ] {
11+ if ( generators ) {
12+ const overrides = this . _resolve ( generators ) ;
13+
14+ return this . _sketches . map ( sketch => sketch . model ( overrides ) ) ;
15+ }
16+ return this . _sketches . map ( sketch => sketch . model ( ) ) ;
17+ }
18+
19+ private _resolve (
20+ generators : { [ key in keyof T ] ?: ( ) => T [ key ] }
21+ ) : { [ key in keyof T ] ?: T [ key ] } {
22+ return Object . keys ( generators )
23+ . map ( key => ( {
24+ [ key ] : ( generators as any ) [ key ] ( )
25+ } ) )
26+ . reduce (
27+ ( combinedOverrides , override ) => ( {
28+ ...combinedOverrides ,
29+ ...override
30+ } ) ,
31+ { }
32+ ) as any ;
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ import { Kentan , Sketch } from '../src/kentan' ;
2+
3+ class Empty {
4+ id : string ;
5+ }
6+
7+ class ForEmpty extends Sketch < Empty > {
8+ constructor ( ) {
9+ super ( Empty ) ;
10+ }
11+ }
12+
13+ describe ( 'When a list of test data is needed' , ( ) => {
14+ it ( 'should be possible to generate a list of sketches' , ( ) => {
15+ const sketches = Kentan . sketch ( ForEmpty ) . take ( 2 ) ;
16+ expect ( sketches . all ( ) . length ) . toBe ( 2 ) ;
17+ } ) ;
18+
19+ it ( 'should allow to get the models from the sketch list' , ( ) => {
20+ const models = Kentan . sketch ( ForEmpty )
21+ . take ( 1 )
22+ . models ( ) ;
23+
24+ expect ( models [ 0 ] ) . toBeInstanceOf ( Empty ) ;
25+ } ) ;
26+
27+ it ( 'should be allowed to pass a factory for a property' , ( ) => {
28+ const id = '12-34-56-78' ;
29+ const models = Kentan . sketch ( ForEmpty )
30+ . take ( 1 )
31+ . models ( {
32+ id : ( ) => id
33+ } ) ;
34+
35+ expect ( models [ 0 ] . id ) . toBe ( id ) ;
36+ } ) ;
37+ } ) ;
You can’t perform that action at this time.
0 commit comments