Return a random item / array of items from an input array.
0.1.0
Generate some mock of any data type on the fly.
(boolean)
: Returns a call from generatorFn.
(boolean[])
: Returns anArray
of N timesgeneratorFn
calls.
- Define some types and an
User
class definition:
interface UserMockInterface {
id: string;
fullName: string;
isActive: boolean;
}
type UserMockType = {
id: string;
fullName: string;
email: string;
isActive: boolean;
sayMyName: () => string;
};
class User implements UserMockType {
id: string;
fullName: string;
email: string;
isActive: boolean;
constructor(model: UserMockInterface) {
this.id = model.id;
this.fullName = model.fullName;
this.email = `${this.fullName}@hello.com`;
}
sayMyName(): string {
return `Say my name, say my naaaame... ${this.fullName}`;
}
}
- Implements
MockFactory
coupled with some randomizers like piupiu.RandomString & piupiu.RandomBoolean to generate some fake users
const userMockGenerator = new MockFactory<UserMockType>(({ options = {}, override = {} }) => {
return new User({
id: RandomString.getOne(),
fullName: RandomString.getOne(),
isActive: RandomBoolean.getOne(),
...override,
});
});
const user = new userMockGenerator.getOne()
console.log(user.sayMyName()) // "Say my name, say my naaaame... Erdf"
const user = new userMockGenerator.getOne({
override: {
fullName: "Aaron Swartz"
}
})
console.log(user.sayMyName()) // "Say my name, say my naaaame... Aaron Swartz"
const users = new userMockGenerator.getArray({ length: 2})
console.log(users)
// [
// {
// id: 'wef3',
// fullName: 'wdWE',
// isActive: true,
// },
// {
// id: 'dub243ub',
// fullName: 'qedqe',
// isActive: true,
// }
const users = new userMockGenerator.getArray({
length: 2,
overrides: [
{}, // no override on the first user
{ fullName: 'Aaron Swartz' } //override second user
]
})
console.log(users)
// [
// {
// id: 'wef3',
// fullName: 'wdWE',
// isActive: true,
// },
// {
// id: 'dub243ub',
// fullName: 'Aaron Swartz',
// isActive: true,
// }