UNPKG

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