Skip to content

Instantly share code, notes, and snippets.

@mattraykowski
Created August 8, 2017 12:55
Show Gist options
  • Save mattraykowski/8c022994fe93c0c9c6cbaeacd954e242 to your computer and use it in GitHub Desktop.
Save mattraykowski/8c022994fe93c0c9c6cbaeacd954e242 to your computer and use it in GitHub Desktop.
export class UserBuilder {
constructor() {
this.user = {
name: '',
avatar: '',
// etc
};
}
stub() {
this.user.name = 'Bob';
this.user.avatar = 'http://yesss.com';
this.user.role = 'user';
return this;
}
withName(name) {
this.user.name = name;
return this;
}
withAvatar(avatar) {
this.user.avatar = avatar;
return this;
}
asUser() {
this.user.role = 'user';
return this;
}
asAdmin() {
this.user.role = 'admin';
return this;
}
build() {
return {...this.user}
}
}
// Usage:
const mockUser = new UserBuilder().stub().asAdmin().build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment