Search code examples
angularjasminejestjsangular-directive

test fails in jest, passes in jasmine for an angular directive with @input


I sat down this fine Saturday morning with the goal of moving my angular 9 project to jest. Fail so far. Besides JSDOM not supporting ClipboardEvent of DragDropEvent (for which I have workarounds), a test that passes in Jasmine fails in Jest.

Here is what I am testing:

@Directive({
  selector: '[evAutoTab]'
})
export class EvAutoTabDirective {

  @Input('evAutoTab') target: string;

  @HostListener('keyup') onKeyup() {
    this.moveFocus();
  }

  constructor(private el: ElementRef) {
  }

  private moveFocus() {
    const maxLen = this.el.nativeElement.getAttribute('maxlength');
    const len = this.el.nativeElement.value.length;

    // console.log(`len ${len} maxLen ${maxLen} target ${this.target}`);

    if (len === parseInt(maxLen, 10)) {
      const next: HTMLElement = document.querySelector('#' + this.target);
      next.focus();
    }
  }
}

In both jest and jasmine configurations, the directive I want to test is called, but the "target" is never set in jest, so the test fails. evAutoTab="target".

I believe I have jest configured properly (or rather angular configured properly for jest)

The test:

@Component({
  template: `
    <div>
      <input evAutoTab="AutoTab1" id="AutoTab0" maxlength="4" value=""/>
      <input evAutoTab id="AutoTab1" value=""/>
      <input evAutoTab="AutoTab4" id="AutoTab2" maxlength="2" value=""/>
    </div>
    <div>
      <input evAutoTab id="AutoTab3" value=""/>
      <input evAutoTab id="AutoTab4" value=""/>
      <input evAutoTab id="AutoTab5" value=""/>
    </div>
  `
})
class TestComponent {

  constructor() {
  }
}

describe('EvAutoTabDirective', () => {
  let component: TestComponent;
  let fixture: ComponentFixture<TestComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        TestComponent,
        EvAutoTabDirective
      ]
    });

    fixture = TestBed.createComponent(TestComponent);
    component = fixture.componentInstance;
  });

  it('should move focus from third element skipping to fifth', () => {
    const debugEl: HTMLElement = fixture.debugElement.nativeElement;
    const autoTab2: HTMLInputElement = debugEl.querySelector('#AutoTab2');
    const autoTab4: HTMLInputElement = debugEl.querySelector('#AutoTab4');
    const focusSpy = spyOn({
      here: () => {
      }
    }, 'here');

    // verify setup
    autoTab2.focus();
    expect(document.activeElement.id).toEqual('AutoTab2');

    // act
    autoTab2.value = '19';
    autoTab2.dispatchEvent(new Event('keyup'));
    fixture.detectChanges();

    expect(document.activeElement.id).toEqual('AutoTab4');
  });
});

Any suggestions?


Solution

  • So testing with jest I needed a second fixture.detectChanges();

    fixture.detectChanges();
    fixture.detectChanges();
    expect(document.activeElement.id).toEqual('AutoTab4');
    

    Now it works.

    Giving jest a second chance