@testing-library/react-native
Version:
Simple and complete React Native testing utilities that encourage good testing practices.
80 lines (77 loc) • 4.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.configureHostComponentNamesIfNeeded = configureHostComponentNamesIfNeeded;
exports.getHostComponentNames = getHostComponentNames;
exports.isHostText = isHostText;
exports.isHostTextInput = isHostTextInput;
var React = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _config = require("../config");
var _renderAct = require("../render-act");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const userConfigErrorMessage = `There seems to be an issue with your configuration that prevents React Native Testing Library from working correctly.
Please check if you are using compatible versions of React Native and React Native Testing Library.`;
function getHostComponentNames() {
let hostComponentNames = (0, _config.getConfig)().hostComponentNames;
if (!hostComponentNames) {
hostComponentNames = detectHostComponentNames();
(0, _config.configureInternal)({
hostComponentNames
});
}
return hostComponentNames;
}
function configureHostComponentNamesIfNeeded() {
const configHostComponentNames = (0, _config.getConfig)().hostComponentNames;
if (configHostComponentNames) {
return;
}
const hostComponentNames = detectHostComponentNames();
(0, _config.configureInternal)({
hostComponentNames
});
}
function detectHostComponentNames() {
try {
const renderer = (0, _renderAct.renderWithAct)( /*#__PURE__*/React.createElement(_reactNative.View, null, /*#__PURE__*/React.createElement(_reactNative.Text, {
testID: "text"
}, "Hello"), /*#__PURE__*/React.createElement(_reactNative.TextInput, {
testID: "textInput"
}), /*#__PURE__*/React.createElement(_reactNative.Switch, {
testID: "switch"
})));
return {
text: getByTestId(renderer.root, 'text').type,
textInput: getByTestId(renderer.root, 'textInput').type,
switch: getByTestId(renderer.root, 'switch').type
};
} catch (error) {
const errorMessage = error && typeof error === 'object' && 'message' in error ? error.message : null;
throw new Error(`Trying to detect host component names triggered the following error:\n\n${errorMessage}\n\n${userConfigErrorMessage}`);
}
}
function getByTestId(instance, testID) {
const nodes = instance.findAll(node => typeof node.type === 'string' && node.props.testID === testID);
if (nodes.length === 0) {
throw new Error(`Unable to find an element with testID: ${testID}`);
}
return nodes[0];
}
/**
* Checks if the given element is a host Text.
* @param element The element to check.
*/
function isHostText(element) {
return element?.type === getHostComponentNames().text;
}
/**
* Checks if the given element is a host TextInput.
* @param element The element to check.
*/
function isHostTextInput(element) {
return element?.type === getHostComponentNames().textInput;
}
//# sourceMappingURL=host-component-names.js.map
;