Untitled

 avatar
unknown
plain_text
2 years ago
3.9 kB
8
Indexable
import { TestBed } from '@angular/core/testing';
import { FormControl, NgForm } from '@angular/forms';
import { ErrorMessagesComponent } from './error-messages.component';

describe('ErrorMessagesComponent', () => {
  let component: ErrorMessagesComponent;
  let control: any;
  let form: any;
  let testForm: any;
  let s: any;
  let ControlContainer: any;
  let errorsArray: string[];
  beforeEach(() => {
    component = new ErrorMessagesComponent(control, form);
    component.fieldName = 'fieldName';
    // component['errorMessages'] = new Map([
    //   ['minlength', 'Please Enter Minimum 2 Characters'],
    //   ['maxlength', 'Exceeded Maximum Limit of 10 Characters'],
    //   ['pattern', 'Incorrect Format'],
    //   ['email', 'Invalid Email'],
    //   ['required', 'This Field is Required'],
    // ]);
    TestBed.configureTestingModule({
      // imports: [HttpClientTestingModule],
      // declarations: [ResponseFormComponent],
      providers: [NgForm],
    }).compileComponents();
    errorsArray = [];
    control = new FormControl();
    control.errors = false;
    control.touched = true;
    testForm = TestBed.inject(NgForm);
    form = {
      submitted: true,
    };
    form = document.createElement('form');
    s = document.createElement('input');
    s.setAttribute('type', 'submit');
    s.setAttribute('value', 'Submit');
    form.append(s);
    // form.submitted = true;
    // form = {
    //   control: {
    //     get: jest.fn().mockReturnValue(form),
    //   },
    // };
    ControlContainer = {
      control: {
        get: jest.fn().mockReturnValue(control),
      },
    };
    // instance = {
    //   form: {
    //     submitted: false,
    //   },
    // };
  });
  it('should return error messages for a control with errors', () => {
    const component = new ErrorMessagesComponent(ControlContainer, form);
    control.markAsTouched();
    control.errors = {
      minlength: '',
      pattern: '',
      email: '',
      maxlength: '',
      required: '',
    };
    expect(component.getErrorMessages()).toEqual([
      'Please Enter Minimum undefined Characters',
      'Incorrect Format',
      'Invalid Email',
      'Exceeded Maximum Limit of undefined Characters',
      'This Field is Required',
    ]);
  });
  it('', () => {
    const component = new ErrorMessagesComponent(ControlContainer, form);
    component['errorMessages']?.forEach(() => {});
    expect(errorsArray).toEqual(['error1', 'error', 'error2']);
  });
  it('should succed without errror', () => {
    const component = new ErrorMessagesComponent(ControlContainer, form);
    control.markAsTouched();
    control.errors = {};
    expect(component.getErrorMessages()).not.toEqual([
      'Please Enter Minimum undefined Characters',
      'Incorrect Format',
      'Invalid Email',
      'Exceeded Maximum Limit of undefined Characters',
      'This Field is Required',
    ]);
  });
  it('should return false if control is falsy', () => {
    const component = new ErrorMessagesComponent(ControlContainer, form);
    ControlContainer.errors = false;
    const result = component['shouldDisplayError'];
    expect(result(ControlContainer)).toBe(false);
  });
  it('should return true if form.submitted is falsy and control.touched is truthy', () => {
    ControlContainer.errors = true;
    const component = new ErrorMessagesComponent(ControlContainer, testForm);
    console.log(component['form']);
    expect(component['form'] instanceof NgForm).toBeTruthy();
  });
  it('should return false if control is falsy', () => {
    const component = new ErrorMessagesComponent(ControlContainer, form);
    console.log(form);
    control.errors = true;
    const result = component['shouldDisplayError'];
    expect(result(control)).toBe(false);
  });
});
Editor is loading...