testing.mjs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
  2. /** Harness for interacting with a standard Angular Material sort header in tests. */
  3. class MatSortHeaderHarness extends ComponentHarness {
  4. static hostSelector = '.mat-sort-header';
  5. _container = this.locatorFor('.mat-sort-header-container');
  6. /**
  7. * Gets a `HarnessPredicate` that can be used to
  8. * search for a sort header with specific attributes.
  9. */
  10. static with(options = {}) {
  11. return new HarnessPredicate(MatSortHeaderHarness, options)
  12. .addOption('label', options.label, (harness, label) => HarnessPredicate.stringMatches(harness.getLabel(), label))
  13. .addOption('sortDirection', options.sortDirection, (harness, sortDirection) => {
  14. return HarnessPredicate.stringMatches(harness.getSortDirection(), sortDirection);
  15. });
  16. }
  17. /** Gets the label of the sort header. */
  18. async getLabel() {
  19. return (await this._container()).text();
  20. }
  21. /** Gets the sorting direction of the header. */
  22. async getSortDirection() {
  23. const host = await this.host();
  24. const ariaSort = await host.getAttribute('aria-sort');
  25. if (ariaSort === 'ascending') {
  26. return 'asc';
  27. }
  28. else if (ariaSort === 'descending') {
  29. return 'desc';
  30. }
  31. return '';
  32. }
  33. /** Gets whether the sort header is currently being sorted by. */
  34. async isActive() {
  35. return !!(await this.getSortDirection());
  36. }
  37. /** Whether the sort header is disabled. */
  38. async isDisabled() {
  39. return (await this.host()).hasClass('mat-sort-header-disabled');
  40. }
  41. /** Clicks the header to change its sorting direction. Only works if the header is enabled. */
  42. async click() {
  43. return (await this.host()).click();
  44. }
  45. }
  46. /** Harness for interacting with a standard `mat-sort` in tests. */
  47. class MatSortHarness extends ComponentHarness {
  48. static hostSelector = '.mat-sort';
  49. /**
  50. * Gets a `HarnessPredicate` that can be used to search for a `mat-sort` with specific attributes.
  51. * @param options Options for narrowing the search.
  52. * @return a `HarnessPredicate` configured with the given options.
  53. */
  54. static with(options = {}) {
  55. return new HarnessPredicate(MatSortHarness, options);
  56. }
  57. /** Gets all of the sort headers in the `mat-sort`. */
  58. async getSortHeaders(filter = {}) {
  59. return this.locatorForAll(MatSortHeaderHarness.with(filter))();
  60. }
  61. /** Gets the selected header in the `mat-sort`. */
  62. async getActiveHeader() {
  63. const headers = await this.getSortHeaders();
  64. for (let i = 0; i < headers.length; i++) {
  65. if (await headers[i].isActive()) {
  66. return headers[i];
  67. }
  68. }
  69. return null;
  70. }
  71. }
  72. export { MatSortHarness, MatSortHeaderHarness };
  73. //# sourceMappingURL=testing.mjs.map