testing.mjs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
  2. import { coerceNumberProperty } from '@angular/cdk/coercion';
  3. import { MatSelectHarness } from '../select/testing.mjs';
  4. import '../form-field/testing/control.mjs';
  5. import '../option-harness-BFcc-M_4.mjs';
  6. import '../core/testing.mjs';
  7. /** Harness for interacting with a mat-paginator in tests. */
  8. class MatPaginatorHarness extends ComponentHarness {
  9. /** Selector used to find paginator instances. */
  10. static hostSelector = '.mat-mdc-paginator';
  11. _nextButton = this.locatorFor('.mat-mdc-paginator-navigation-next');
  12. _previousButton = this.locatorFor('.mat-mdc-paginator-navigation-previous');
  13. _firstPageButton = this.locatorForOptional('.mat-mdc-paginator-navigation-first');
  14. _lastPageButton = this.locatorForOptional('.mat-mdc-paginator-navigation-last');
  15. _select = this.locatorForOptional(MatSelectHarness.with({
  16. ancestor: '.mat-mdc-paginator-page-size',
  17. }));
  18. _pageSizeFallback = this.locatorFor('.mat-mdc-paginator-page-size-value');
  19. _rangeLabel = this.locatorFor('.mat-mdc-paginator-range-label');
  20. /**
  21. * Gets a `HarnessPredicate` that can be used to search for a paginator with specific attributes.
  22. * @param options Options for filtering which paginator instances are considered a match.
  23. * @return a `HarnessPredicate` configured with the given options.
  24. */
  25. static with(options = {}) {
  26. return new HarnessPredicate(this, options);
  27. }
  28. /** Goes to the next page in the paginator. */
  29. async goToNextPage() {
  30. return (await this._nextButton()).click();
  31. }
  32. /** Returns whether or not the next page button is disabled. */
  33. async isNextPageDisabled() {
  34. const disabledValue = await (await this._nextButton()).getAttribute('aria-disabled');
  35. return disabledValue == 'true';
  36. }
  37. /* Returns whether or not the previous page button is disabled. */
  38. async isPreviousPageDisabled() {
  39. const disabledValue = await (await this._previousButton()).getAttribute('aria-disabled');
  40. return disabledValue == 'true';
  41. }
  42. /** Goes to the previous page in the paginator. */
  43. async goToPreviousPage() {
  44. return (await this._previousButton()).click();
  45. }
  46. /** Goes to the first page in the paginator. */
  47. async goToFirstPage() {
  48. const button = await this._firstPageButton();
  49. // The first page button isn't enabled by default so we need to check for it.
  50. if (!button) {
  51. throw Error('Could not find first page button inside paginator. ' +
  52. 'Make sure that `showFirstLastButtons` is enabled.');
  53. }
  54. return button.click();
  55. }
  56. /** Goes to the last page in the paginator. */
  57. async goToLastPage() {
  58. const button = await this._lastPageButton();
  59. // The last page button isn't enabled by default so we need to check for it.
  60. if (!button) {
  61. throw Error('Could not find last page button inside paginator. ' +
  62. 'Make sure that `showFirstLastButtons` is enabled.');
  63. }
  64. return button.click();
  65. }
  66. /**
  67. * Sets the page size of the paginator.
  68. * @param size Page size that should be select.
  69. */
  70. async setPageSize(size) {
  71. const select = await this._select();
  72. // The select is only available if the `pageSizeOptions` are
  73. // set to an array with more than one item.
  74. if (!select) {
  75. throw Error('Cannot find page size selector in paginator. ' +
  76. 'Make sure that the `pageSizeOptions` have been configured.');
  77. }
  78. return select.clickOptions({ text: `${size}` });
  79. }
  80. /** Gets the page size of the paginator. */
  81. async getPageSize() {
  82. const select = await this._select();
  83. const value = select ? select.getValueText() : (await this._pageSizeFallback()).text();
  84. return coerceNumberProperty(await value);
  85. }
  86. /** Gets the text of the range label of the paginator. */
  87. async getRangeLabel() {
  88. return (await this._rangeLabel()).text();
  89. }
  90. }
  91. export { MatPaginatorHarness };
  92. //# sourceMappingURL=testing.mjs.map