https://github.com/angular/angular
Raw File
Tip revision: f0b94f5b4836adbc9a41b8a392f7123128196224 authored by Alex Rickabaugh on 08 June 2022, 20:00:25 UTC
release: cut the v14.0.1 release (#46308)
Tip revision: f0b94f5
dom_adapter_spec.ts
/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

import {ɵgetDOM as getDOM} from '@angular/common';
import {isTextNode} from '@angular/platform-browser/testing/src/browser_util';

{
  describe('dom adapter', () => {
    let defaultDoc: any;
    beforeEach(() => {
      defaultDoc = getDOM().supportsDOMEvents ? document : getDOM().createHtmlDocument();
    });

    it('should be able to create text nodes and use them with the other APIs', () => {
      const t = getDOM().getDefaultDocument().createTextNode('hello');
      expect(isTextNode(t)).toBe(true);
      const d = getDOM().createElement('div');
      d.appendChild(t);
      expect(d.innerHTML).toEqual('hello');
    });

    it('should set className via the class attribute', () => {
      const d = getDOM().createElement('div');
      d.setAttribute('class', 'class1');
      expect(d.className).toEqual('class1');
    });

    it('should allow to remove nodes without parents', () => {
      const d = getDOM().createElement('div');
      expect(() => getDOM().remove(d)).not.toThrow();
    });

    if (getDOM().supportsDOMEvents) {
      describe('getBaseHref', () => {
        beforeEach(() => getDOM().resetBaseElement());

        it('should return null if base element is absent', () => {
          expect(getDOM().getBaseHref(defaultDoc)).toBeNull();
        });

        it('should return the value of the base element', () => {
          const baseEl = getDOM().createElement('base');
          baseEl.setAttribute('href', '/drop/bass/connon/');
          const headEl = defaultDoc.head;
          headEl.appendChild(baseEl);

          const baseHref = getDOM().getBaseHref(defaultDoc);
          headEl.removeChild(baseEl);
          getDOM().resetBaseElement();

          expect(baseHref).toEqual('/drop/bass/connon/');
        });

        it('should return a relative url', () => {
          const baseEl = getDOM().createElement('base');
          baseEl.setAttribute('href', 'base');
          const headEl = defaultDoc.head;
          headEl.appendChild(baseEl);

          const baseHref = getDOM().getBaseHref(defaultDoc)!;
          headEl.removeChild(baseEl);
          getDOM().resetBaseElement();

          expect(baseHref.endsWith('/base')).toBe(true);
        });
      });
    }
  });
}
back to top