GitHub

DaffModelFactory

import { DaffModelFactory } from '@daffodil/core/testing'

The base class for model factories.

The mock class is passed as the first constructor arg and any additional args are passed to the mock class constructor.

The constructor args can be omitted if the create method is overridden.

abstract class DaffModelFactory<<T extends Record<string, any>, Klass extends Constructable<T> = Constructable<T>>>  implements IDaffModelFactory<T> {
  _instantiationArgs: ConstructorParameters<Klass>
  type: Klass

  create(partial: Partial<T> = {}): T
  createMany(
    qty: number = 1
    partial: Partial<T> = {}
  ): T[]
}

Examples

Injecting a different factory into a mock class

class MyMockModel {
 constructor(
   private otherFactory: MyOtherFactory
 ) {}

 private createOtherModel() {
   return this.otherFactory.create()
 }

 otherModel = this.createOtherModel()
}

@Injectable()
class TestFactory extends DaffModelFactory<MyMockModel> {
 constructor(
   otherFactory: MyOtherFactory
 ) {
   super(MyMockModel, otherFactory)
 }
}