import WsValidatorService from './ws-validator.service';

describe('wsValidatorService', () => {
  let wsValidatorService;

  const mock$window = {
    scrollTo: () => true,
  };
  const mockErrors = {
    name: ['required', 'is invalid'],
    password: ['required'],
  };
  const mockErrorResponse = {
    data: {
      error: 'Password required, Name required',
      error_type: 'record_invalid',
      errors: mockErrors,
    },
  };
  const mockObjWithErrors = {
    errors: mockErrors,
  };

  beforeEach(() => {
    wsValidatorService = new WsValidatorService(mock$window);
  });

  it('clearErrors clears errors correctly', () => {
    const obj = { errors: mockErrors };
    wsValidatorService.clearErrors(obj);
    expect(obj).toEqual({});
  });

  it('clearErrors clears errors correctly for an object with no errors', () => {
    const obj = {};
    wsValidatorService.clearErrors(obj);
    expect(obj).toEqual({});
  });

  it('attachErrorForField attaches errors for field correctly', () => {
    const obj = { errors: {} };
    wsValidatorService.attachErrorForField(obj, 'testField', 'Test message');
    expect(obj).toEqual({ errors: { testField: ['Test message'] } });
  });

  it('attachErrorsFor attaches errors correctly', () => {
    const obj = { errors: {} };
    wsValidatorService.attachErrorsFor(obj, mockErrorResponse);
    expect(obj).toEqual({ errors: mockErrorResponse.data.errors });
  });

  it('hasErrorsFor determines has errors for correctly', () => {
    const hasErrors = wsValidatorService.hasErrorsFor(mockObjWithErrors, 'name');
    expect(hasErrors).toBe(true);
  });

  it('hasErrorsFor determines has no errors for correctly', () => {
    const hasErrors = wsValidatorService.hasErrorsFor(mockObjWithErrors, 'test');
    expect(hasErrors).toBe(false);
  });

  it('getErrorsFor gets the error correctly', () => {
    const error = wsValidatorService.getErrorsFor(mockObjWithErrors, 'name');
    expect(error).toEqual(mockErrors.name[0]);
  });

  it('getErrorsFor gets no error correctly', () => {
    const error = wsValidatorService.getErrorsFor(mockObjWithErrors, 'test');
    expect(error).toEqual('');
  });
});
