n8n/packages/cli/test/unit/repositories/role.repository.test.ts
कारतोफ्फेलस्क्रिप्ट™ b895ba438a
refactor(core): Reduce boilterplate code in between tests 🧹, and fix the tests in node.js 20 (no-changelog) (#6654)
refactor(core): Reduce boilterplate code in between tests

also cleaned up some imports, and fixed the tests in node.js 20
2023-07-13 10:14:48 +02:00

49 lines
2 KiB
TypeScript

import { Container } from 'typedi';
import { DataSource, EntityManager } from 'typeorm';
import { mock } from 'jest-mock-extended';
import type { RoleNames, RoleScopes } from '@db/entities/Role';
import { Role } from '@db/entities/Role';
import { RoleRepository } from '@db/repositories/role.repository';
import { mockInstance } from '../../integration/shared/utils/';
import { randomInteger } from '../../integration/shared/random';
describe('RoleRepository', () => {
const entityManager = mockInstance(EntityManager);
const dataSource = mockInstance(DataSource, { manager: entityManager });
dataSource.getMetadata.mockReturnValue(mock());
Object.assign(entityManager, { connection: dataSource });
const roleRepository = Container.get(RoleRepository);
describe('findRole', () => {
test('should return the role when present', async () => {
entityManager.findOne.mockResolvedValueOnce(createRole('global', 'owner'));
const role = await roleRepository.findRole('global', 'owner');
expect(role?.name).toEqual('owner');
expect(role?.scope).toEqual('global');
});
test('should return null otherwise', async () => {
entityManager.findOne.mockResolvedValueOnce(null);
const role = await roleRepository.findRole('global', 'owner');
expect(role).toEqual(null);
});
});
describe('findRoleOrFail', () => {
test('should return the role when present', async () => {
entityManager.findOneOrFail.mockResolvedValueOnce(createRole('global', 'owner'));
const role = await roleRepository.findRoleOrFail('global', 'owner');
expect(role?.name).toEqual('owner');
expect(role?.scope).toEqual('global');
});
test('should throw otherwise', async () => {
entityManager.findOneOrFail.mockRejectedValueOnce(new Error());
await expect(async () => roleRepository.findRoleOrFail('global', 'owner')).rejects.toThrow();
});
});
const createRole = (scope: RoleScopes, name: RoleNames) =>
Object.assign(new Role(), { name, scope, id: `${randomInteger()}` });
});