samedi 9 juillet 2016

How getDOM() works in Angular 2 specs

How does getDOM() from @angular/platform-browser/src/dom/dom_adapter actually work in specs?

In some specs getDOM() refers to some detached root element, similar to AngularJS $rootElement:

  var el1 = el('<div>a</div>');
  var el2 = el('<div>b</div>');
  getDOM().appendChild(el2, getDOM().firstChild(el1));
  expect(getDOM().childNodes(el2).length).toBe(2);

But in bundled Jasmine matchers getDOM() magically refers to target element itself:

    function(actual: any /** TODO #???? */, className: any /** TODO #???? */) {
      return {
        pass: getDOM().hasClass(actual, className) == !isNot,
        get message() {
          return `Expected ${actual.outerHTML} ${isNot ? 'not ' : ''}
                  to contain the CSS class "${className}"`;
        }
      };
    };

I was about to build some Chai assertions for convenience, but this makes me think that I'm missing something important about how Angular 2 behaves in unit tests.

What is going on there? How getDOM() context is being set? Is there a good use case for it in production code?

Aucun commentaire:

Enregistrer un commentaire