testing.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
  2. import { coerceBooleanProperty } from '@angular/cdk/coercion';
  3. /** Harness for interacting with a mat-slide-toggle in tests. */
  4. class MatSlideToggleHarness extends ComponentHarness {
  5. _label = this.locatorFor('label');
  6. _nativeElement = this.locatorFor('button');
  7. static hostSelector = '.mat-mdc-slide-toggle';
  8. /**
  9. * Gets a `HarnessPredicate` that can be used to search for a slide-toggle w/ specific attributes.
  10. * @param options Options for narrowing the search:
  11. * - `selector` finds a slide-toggle whose host element matches the given selector.
  12. * - `label` finds a slide-toggle with specific label text.
  13. * @return a `HarnessPredicate` configured with the given options.
  14. */
  15. static with(options = {}) {
  16. return (new HarnessPredicate(this, options)
  17. .addOption('label', options.label, (harness, label) => HarnessPredicate.stringMatches(harness.getLabelText(), label))
  18. // We want to provide a filter option for "name" because the name of the slide-toggle is
  19. // only set on the underlying input. This means that it's not possible for developers
  20. // to retrieve the harness of a specific checkbox with name through a CSS selector.
  21. .addOption('name', options.name, async (harness, name) => (await harness.getName()) === name)
  22. .addOption('checked', options.checked, async (harness, checked) => (await harness.isChecked()) == checked)
  23. .addOption('disabled', options.disabled, async (harness, disabled) => (await harness.isDisabled()) == disabled));
  24. }
  25. /** Toggle the checked state of the slide-toggle. */
  26. async toggle() {
  27. return (await this._nativeElement()).click();
  28. }
  29. /** Whether the slide-toggle is checked. */
  30. async isChecked() {
  31. const checked = (await this._nativeElement()).getAttribute('aria-checked');
  32. return coerceBooleanProperty(await checked);
  33. }
  34. /** Whether the slide-toggle is disabled. */
  35. async isDisabled() {
  36. const nativeElement = await this._nativeElement();
  37. const disabled = await nativeElement.getAttribute('disabled');
  38. if (disabled !== null) {
  39. return coerceBooleanProperty(disabled);
  40. }
  41. return (await nativeElement.getAttribute('aria-disabled')) === 'true';
  42. }
  43. /** Whether the slide-toggle is required. */
  44. async isRequired() {
  45. const ariaRequired = await (await this._nativeElement()).getAttribute('aria-required');
  46. return ariaRequired === 'true';
  47. }
  48. /** Whether the slide-toggle is valid. */
  49. async isValid() {
  50. const invalid = (await this.host()).hasClass('ng-invalid');
  51. return !(await invalid);
  52. }
  53. /** Gets the slide-toggle's name. */
  54. async getName() {
  55. return (await this._nativeElement()).getAttribute('name');
  56. }
  57. /** Gets the slide-toggle's aria-label. */
  58. async getAriaLabel() {
  59. return (await this._nativeElement()).getAttribute('aria-label');
  60. }
  61. /** Gets the slide-toggle's aria-labelledby. */
  62. async getAriaLabelledby() {
  63. return (await this._nativeElement()).getAttribute('aria-labelledby');
  64. }
  65. /** Gets the slide-toggle's label text. */
  66. async getLabelText() {
  67. return (await this._label()).text();
  68. }
  69. /** Focuses the slide-toggle. */
  70. async focus() {
  71. return (await this._nativeElement()).focus();
  72. }
  73. /** Blurs the slide-toggle. */
  74. async blur() {
  75. return (await this._nativeElement()).blur();
  76. }
  77. /** Whether the slide-toggle is focused. */
  78. async isFocused() {
  79. return (await this._nativeElement()).isFocused();
  80. }
  81. /**
  82. * Puts the slide-toggle in a checked state by toggling it if it is currently unchecked, or doing
  83. * nothing if it is already checked.
  84. */
  85. async check() {
  86. if (!(await this.isChecked())) {
  87. await this.toggle();
  88. }
  89. }
  90. /**
  91. * Puts the slide-toggle in an unchecked state by toggling it if it is currently checked, or doing
  92. * nothing if it is already unchecked.
  93. */
  94. async uncheck() {
  95. if (await this.isChecked()) {
  96. await this.toggle();
  97. }
  98. }
  99. }
  100. export { MatSlideToggleHarness };
  101. //# sourceMappingURL=testing.mjs.map