-
-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Remove existing value with undefined #44
Comments
When you say removing I thought you'd expect |
Thanks for reporting this, @rikardaxelsson. I've been on extended leave after having a child but am hoping to look at this one more soon. If you wanted to experiment with this, I'd welcome your contributions. I'm guessing the place to start would be to try to override the lodash |
@stevehanson experimented little with that.
As lodash docs say, if const mergeCustomizer = (_object: any, srcVal: any) => {
if (Array.isArray(srcVal)) {
return srcVal;
} else if (srcVal === undefined) {
return null;
}
return undefined;
};
const result = mergeWith(
{},
{ foo: 'bar', fizz: 'buzz', buzz: 'lightyear' },
{ foo: undefined, fizz: null, a: 5 },
mergeCustomizer
) // {foo: null, fizz: null, buzz: "lightyear", a: 5} |
@siquel thanks for looking into that. I'm not sure when I'll have more time to look into this. In the meantime, I welcome any community thoughts or contributions. It seems like Lodash's To get around this right now, I recommend using an const userWithoutNameFactory = userFactory.afterBuild(user => { user.name = undefined; return user});
const user = userWithoutNameFactory.build();
user.name // undefined To add some syntax sugar to that, you could write your factory as a class and define methods on the factory: class UserFactory extends Factory<User> {
withoutName() {
return this.afterBuild(user => { user.name = undefined; return user});
}
withoutPosts() {
return this.afterBuild(user => { user.posts = undefined; return user });
}
}
const userFactory = UserFactory.define(() => ({ ... }))
const user = userFactory.withoutName().withoutPosts().build();
user.name // undefined
user.posts // undefined |
@stevehanson - I might have successfully found a solution to this problem without doing the merge in an afterBuild phase. Please let me know if the fix would cause problems. I will be happy to work on a failing test. |
This was closed by #62 and has been released in version 1.4.0. |
Appreciate the issue and I understand the solution. But I have to say the PR #62 just introduced a breaking change. In my case, I have a very complex data model I need to build. I pass a transient parameter to e.g.
So my test cases know the mocked data always has values for the properties. But with the merge including the The workaround that I have to do now is adding extra logic in my (🚨 Please let me know if I am building my factory in a wrong way) |
I have the same problem has @JonathanGuo and would agree that it introduced a breaking change. I also used the original behaviour of // you have a cms-like app where you have existing entities and new drafts which start out as copies
// of the last published version
const artistBuilder = Factory.define<Artist>(() => ({
__typename: "Artist",
name: "Bono"
}))
const artistDraftBuilder = Factory.define<ArtistDraft>(() => ({
__typename: "ArtistDraft",
name: "Michael"
}))
const publishedVersion = artistBuilder.build({ name: "Gaga" })
// `publishedVersion` contains an incorrect `__typename` which I want to reset to `ArtistDraft` from the original factory, I can of course set it explicitly but the actual code does this is in a generalized fashion for lots of different data-types where it was beneficial that I did not need to know the concrete type
const draft = artistDraftBuilder.build({ ...publishedVersion, __typename: undefined }) I agree that it is no big deal and the intended change makes sense to me. It is probably more to peoples expectations as |
Sometimes you want to remove existing data, but it seems like
null
andundefined
are handled differently. The code below returns{ firstname: 'First', lastname: null }
, but i would expect{ firstname: undefined, lastname: null }
.The text was updated successfully, but these errors were encountered: