refactor: User fixture to pass additonal params
This commit is contained in:
25
packages/backend/test/fixtures/user.ts
vendored
25
packages/backend/test/fixtures/user.ts
vendored
@@ -1,25 +1,14 @@
|
|||||||
import createRole from './role';
|
import createRole from './role';
|
||||||
import { faker } from '@faker-js/faker';
|
import { faker } from '@faker-js/faker';
|
||||||
|
import User from '../../src/models/user';
|
||||||
|
|
||||||
type UserParams = {
|
const createUser = async (params: Partial<User> = {}) => {
|
||||||
roleId?: string;
|
params.roleId = params?.roleId || (await createRole()).id;
|
||||||
fullName?: string;
|
params.fullName = params?.fullName || faker.person.fullName();
|
||||||
email?: string;
|
params.email = params?.email || faker.internet.email();
|
||||||
password?: string;
|
params.password = params?.password || faker.internet.password();
|
||||||
};
|
|
||||||
|
|
||||||
const createUser = async (params: UserParams = {}) => {
|
const [user] = await global.knex.table('users').insert(params).returning('*');
|
||||||
const userData = {
|
|
||||||
roleId: params?.roleId || (await createRole()).id,
|
|
||||||
fullName: params?.fullName || faker.person.fullName(),
|
|
||||||
email: params?.email || faker.internet.email(),
|
|
||||||
password: params?.password || faker.internet.password(),
|
|
||||||
};
|
|
||||||
|
|
||||||
const [user] = await global.knex
|
|
||||||
.table('users')
|
|
||||||
.insert(userData)
|
|
||||||
.returning('*');
|
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user