All files / src/tests jestHelpers.ts

100% Statements 92/92
100% Branches 8/8
85.71% Functions 6/7
100% Lines 92/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 921x 1x 1x 1x 1x 96835x 96835x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x
import { newPublicCtx } from '../ctx'
import { models } from './models'
 
jest.mock('../helpers/getGreenDotConfigs', () => ({
    getMainConfig: () => ({
        allPermissions: ['user', 'admin']
    })
}))
 
 
 
export type UserModels = {
    Write: User<string>
    Read: User<string>
    // WithoutGenerics: User<string>
    // WithoutGenericsWrite: User<string>
}
 
export type OrgModels = {
    Write: Organization<string[]>
    Read: Organization<string[]>
    // WithoutGenerics: Organization<string[]>
    // WithoutGenericsWrite: Organization<string[]>
}
 
export interface User<Org extends string | Organization<any>> {
    _id: string
    name: string,
    password: string,
    organization: Org
    // /!\ when adding fields here please add them to __mock__.models.validation
}
 
export interface Organization<Users extends string[] | User<any>[]> {
    _id: string
    name: string
    adminField1: string
    adminField2: string
    anotherFieldUserHaveNoAccess: string
    teams: { name: string, users: Users, adminAuth: string }[]
    // /!\ when adding fields here please add them to __mock__.models.validation
}
 
 
export function createUser<Org extends string | Organization<any> = string>(
    id: number,
    organization: Org
): User<Org> {
    return {
        _id: `USER_${id}`,
        name: `USER_${id}`,
        password: 'pass',
        organization,
    }
}
 
export function createOrg<Users extends string[] | User<any>[] = string[]>(
    id: number,
    users: Users = [] as Users
): Organization<Users> {
    return {
        _id: `ORG_${id}`,
        name: `ORG_${id}`,
        adminField1: 'af1',
        adminField2: 'af2',
        anotherFieldUserHaveNoAccess: 'af3',
        teams: users.map((_, i) => ({ name: `TEAM_${i + 1}`, users, adminAuth: 'secret' }))
    }
}
 
 
export const createUserId = <T extends number>(id: T): `USER_${T}` => `USER_${id}`
export const createOrgId = <T extends number>(id: T): `ORG_${T}` => `ORG_${id}`
export const createTeamId = <T extends number>(id: T): `TEAM_${T}` => `TEAM_${id}`
 
export const userId1 = createUserId(1)
export const userId2 = createUserId(2)
// export const userId3 = createUserId(3)
 
export const orgId1 = createOrgId(1)
// export const orgId2 = createOrgId(2)
// export const orgId3 = createOrgId(3)
 
//  const teamId1 = createTeamId(1)
//  const teamId2 = createTeamId(2)
//  const teamId3 = createTeamId(3)
 
type TestRoles = 'public' | 'admin' | 'user' | 'system'
 
export function getCtx(userRole: TestRoles = 'public', additionalFields = {}) {
    return { ...newPublicCtx().useRole(userRole as any), ...additionalFields } as any as Ctx
}