huridocs/uwazi

View on GitHub
app/api/utils/testingUserInContext.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { permissionsContext } from 'api/permissions/permissionsContext';
import { UserSchema } from 'shared/types/userType';
import { UserRole } from 'shared/types/userSchema';
import { DataType } from 'api/odm';

export class UserInContextMockFactory {
  spy: jest.SpyInstance | undefined;

  mock(user?: DataType<UserSchema>) {
    this.spy = jest.spyOn(permissionsContext, 'getUserInContext').mockReturnValue(user);
  }

  mockEditorUser() {
    this.mock({
      _id: 'userId',
      role: UserRole.EDITOR,
      username: 'editorUser',
      email: 'editor@test.com',
    });
  }

  restore() {
    this.spy?.mockRestore();
  }
}