Given this very trivial attribute directive where all it does is putting text from input into innerText
import { Directive, ElementRef, Input } from '@angular/core';
@Directive({
selector: '[appTrivial]',
})
export class TrivialDirective {
@Input() set text(value: any) {
this.el.nativeElement.innerText = value.toString();
}
constructor(private el: ElementRef) { }
}
and I am trying to use rerender
from @testing-library/angular
to easily change the input:
import { render } from '@testing-library/angular';
import { TrivialDirective } from './trivial.directive';
describe('TrivialDirective', () => {
it('should replace case insensitive', async () => {
const template = `<div appTrivial text="qwe"></div>`;
const { container, rerender, fixture } = await render(template, { declarations: [TrivialDirective] });
expect(container.querySelector('div[appTrivial]').textContent).toEqual('qwe');
rerender({ template: `<div appTrivial text="rty"></div>` });
// fixture.detectChanges();
expect(container.querySelector('div[appTrivial]').textContent).toEqual('rty');
});
});
The second assertion doesn't work - with or without fixture.detectChanges()
.
Then, what is the proper way of testing such a directive? esp. when you have a couple of inputs and you want to change all their values at once during tests.
Something like this may work: