Created
August 8, 2017 12:55
-
-
Save mattraykowski/8c022994fe93c0c9c6cbaeacd954e242 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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