Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature proposal] Allow to specify factory as field default value of another factory #37

Open
lukaszfiszer opened this issue Mar 25, 2020 · 1 comment

Comments

@lukaszfiszer
Copy link

Given that we have following hierarchy of interfaces:

interface Person {
  id: number;
  name: Name
  age: number;
}

interface Name {
  firstName: string;
  lastName: string;
  fullName: string;
}

I propose that it should be possible to define factories in the following way:

const nameFactory = Factory.Sync.makeFactory<Name>({
  firstName: "Bob",
  lastName: "Smith",
  fullName: "Robert J. Smith, Jr.",
});

const personFactory = Factory.Sync.makeFactory<Person>({
  id: 1,
  name: nameFactory,
  age: 10
});

When personFactory .build() is invoked with person data, the corresponding subset is passed to nameFactory.build():

personFactory.build({
  id: 9,
  name: {
    firstName: "John"
  }
});
// { id: 9 , name: { firstName: "John", lastName: "Smith", fullName: "Robert J. Smith, Jr." }, age: 10}
@lukaszfiszer lukaszfiszer changed the title [feature request] Allow to specify factory as field default value of another factory [feature proposal] Allow to specify factory as field default value of another factory Mar 25, 2020
@ebramanti
Copy link
Contributor

It seems like the same behavior can be accomplished by doing the following:

const nameFactory = Factory.Sync.makeFactory<Name>({
  firstName: "Bob",
  lastName: "Smith",
  fullName: "Robert J. Smith, Jr."
});

const personFactory = Factory.Sync.makeFactory<Person>({
  id: 1,
  name: Factory.each(() => nameFactory.build()),
  age: 10
});

personFactory.build({
  id: 9,
  name: nameFactory.build({
    firstName: 'John'
  })
})

It does require an import of nameFactory but it have the same behavior that you are requesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants