to-have-value.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.toHaveValue = toHaveValue;
  7. var _isEqualWith = _interopRequireDefault(require("lodash/isEqualWith"));
  8. var _utils = require("./utils");
  9. function toHaveValue(htmlElement, expectedValue) {
  10. (0, _utils.checkHtmlElement)(htmlElement, toHaveValue, this);
  11. if (htmlElement.tagName.toLowerCase() === 'input' && ['checkbox', 'radio'].includes(htmlElement.type)) {
  12. throw new Error('input with type=checkbox or type=radio cannot be used with .toHaveValue(). Use .toBeChecked() for type=checkbox or .toHaveFormValues() instead');
  13. }
  14. const receivedValue = (0, _utils.getSingleElementValue)(htmlElement);
  15. const expectsValue = expectedValue !== undefined;
  16. let expectedTypedValue = expectedValue;
  17. let receivedTypedValue = receivedValue;
  18. if (expectedValue == receivedValue && expectedValue !== receivedValue) {
  19. expectedTypedValue = `${expectedValue} (${typeof expectedValue})`;
  20. receivedTypedValue = `${receivedValue} (${typeof receivedValue})`;
  21. }
  22. return {
  23. pass: expectsValue ? (0, _isEqualWith.default)(receivedValue, expectedValue, _utils.compareArraysAsSet) : Boolean(receivedValue),
  24. message: () => {
  25. const to = this.isNot ? 'not to' : 'to';
  26. const matcher = this.utils.matcherHint(`${this.isNot ? '.not' : ''}.toHaveValue`, 'element', expectedValue);
  27. return (0, _utils.getMessage)(this, matcher, `Expected the element ${to} have value`, expectsValue ? expectedTypedValue : '(any)', 'Received', receivedTypedValue);
  28. }
  29. };
  30. }