testing.mjs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
  2. import * as i0 from '@angular/core';
  3. import { Injectable, NgModule } from '@angular/core';
  4. import { of } from 'rxjs';
  5. import { M as MatIconRegistry } from '../icon-registry-B2IMBfNA.mjs';
  6. import '@angular/common';
  7. import '@angular/common/http';
  8. import '@angular/platform-browser';
  9. import 'rxjs/operators';
  10. /** Possible types of icons. */
  11. var IconType;
  12. (function (IconType) {
  13. IconType[IconType["SVG"] = 0] = "SVG";
  14. IconType[IconType["FONT"] = 1] = "FONT";
  15. })(IconType || (IconType = {}));
  16. /** Harness for interacting with a standard mat-icon in tests. */
  17. class MatIconHarness extends ComponentHarness {
  18. /** The selector for the host element of a `MatIcon` instance. */
  19. static hostSelector = '.mat-icon';
  20. /**
  21. * Gets a `HarnessPredicate` that can be used to search for a `MatIconHarness` that meets
  22. * certain criteria.
  23. * @param options Options for filtering which icon instances are considered a match.
  24. * @return a `HarnessPredicate` configured with the given options.
  25. */
  26. static with(options = {}) {
  27. return new HarnessPredicate(MatIconHarness, options)
  28. .addOption('type', options.type, async (harness, type) => (await harness.getType()) === type)
  29. .addOption('name', options.name, (harness, text) => HarnessPredicate.stringMatches(harness.getName(), text))
  30. .addOption('namespace', options.namespace, (harness, text) => HarnessPredicate.stringMatches(harness.getNamespace(), text));
  31. }
  32. /** Gets the type of the icon. */
  33. async getType() {
  34. const type = await (await this.host()).getAttribute('data-mat-icon-type');
  35. return type === 'svg' ? IconType.SVG : IconType.FONT;
  36. }
  37. /** Gets the name of the icon. */
  38. async getName() {
  39. const host = await this.host();
  40. const nameFromDom = await host.getAttribute('data-mat-icon-name');
  41. // If we managed to figure out the name from the attribute, use it.
  42. if (nameFromDom) {
  43. return nameFromDom;
  44. }
  45. // Some icons support defining the icon as a ligature.
  46. // As a fallback, try to extract it from the DOM text.
  47. if ((await this.getType()) === IconType.FONT) {
  48. // Other directives may add content to the icon (e.g. `MatBadge`), however only the direct
  49. // text nodes affect the name of the icon. Exclude all element descendants from the result.
  50. const text = await host.text({ exclude: '*' });
  51. // There are some internal cases where the icon name is wrapped in another node.
  52. // Fall back to extracting the entire text if we ended up excluding everything above.
  53. return text.length > 0 ? text : host.text();
  54. }
  55. return null;
  56. }
  57. /** Gets the namespace of the icon. */
  58. async getNamespace() {
  59. return (await this.host()).getAttribute('data-mat-icon-namespace');
  60. }
  61. /** Gets whether the icon is inline. */
  62. async isInline() {
  63. return (await this.host()).hasClass('mat-icon-inline');
  64. }
  65. }
  66. /**
  67. * A null icon registry that must be imported to allow disabling of custom
  68. * icons.
  69. */
  70. class FakeMatIconRegistry {
  71. addSvgIcon() {
  72. return this;
  73. }
  74. addSvgIconLiteral() {
  75. return this;
  76. }
  77. addSvgIconInNamespace() {
  78. return this;
  79. }
  80. addSvgIconLiteralInNamespace() {
  81. return this;
  82. }
  83. addSvgIconSet() {
  84. return this;
  85. }
  86. addSvgIconSetLiteral() {
  87. return this;
  88. }
  89. addSvgIconSetInNamespace() {
  90. return this;
  91. }
  92. addSvgIconSetLiteralInNamespace() {
  93. return this;
  94. }
  95. registerFontClassAlias() {
  96. return this;
  97. }
  98. classNameForFontAlias(alias) {
  99. return alias;
  100. }
  101. getDefaultFontSetClass() {
  102. return ['material-icons'];
  103. }
  104. getSvgIconFromUrl() {
  105. return of(this._generateEmptySvg());
  106. }
  107. getNamedSvgIcon() {
  108. return of(this._generateEmptySvg());
  109. }
  110. setDefaultFontSetClass() {
  111. return this;
  112. }
  113. addSvgIconResolver() {
  114. return this;
  115. }
  116. ngOnDestroy() { }
  117. _generateEmptySvg() {
  118. const emptySvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  119. emptySvg.classList.add('fake-testing-svg');
  120. // Emulate real icon characteristics from `MatIconRegistry` so size remains consistent in tests.
  121. emptySvg.setAttribute('fit', '');
  122. emptySvg.setAttribute('height', '100%');
  123. emptySvg.setAttribute('width', '100%');
  124. emptySvg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
  125. emptySvg.setAttribute('focusable', 'false');
  126. return emptySvg;
  127. }
  128. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: FakeMatIconRegistry, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  129. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: FakeMatIconRegistry });
  130. }
  131. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: FakeMatIconRegistry, decorators: [{
  132. type: Injectable
  133. }] });
  134. /** Import this module in tests to install the null icon registry. */
  135. class MatIconTestingModule {
  136. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: MatIconTestingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  137. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.6", ngImport: i0, type: MatIconTestingModule });
  138. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: MatIconTestingModule, providers: [{ provide: MatIconRegistry, useClass: FakeMatIconRegistry }] });
  139. }
  140. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.6", ngImport: i0, type: MatIconTestingModule, decorators: [{
  141. type: NgModule,
  142. args: [{
  143. providers: [{ provide: MatIconRegistry, useClass: FakeMatIconRegistry }],
  144. }]
  145. }] });
  146. export { FakeMatIconRegistry, IconType, MatIconHarness, MatIconTestingModule };
  147. //# sourceMappingURL=testing.mjs.map