screen.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.screen = void 0;
  6. var _lzString = require("lz-string");
  7. var queries = _interopRequireWildcard(require("./queries"));
  8. var _getQueriesForElement = require("./get-queries-for-element");
  9. var _prettyDom = require("./pretty-dom");
  10. var _helpers = require("./helpers");
  11. 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); }
  12. 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; }
  13. function unindent(string) {
  14. // remove white spaces first, to save a few bytes.
  15. // testing-playground will reformat on load any ways.
  16. return string.replace(/[ \t]*[\n][ \t]*/g, '\n');
  17. }
  18. function encode(value) {
  19. return (0, _lzString.compressToEncodedURIComponent)(unindent(value));
  20. }
  21. function getPlaygroundUrl(markup) {
  22. return `https://testing-playground.com/#markup=${encode(markup)}`;
  23. }
  24. const debug = (element, maxLength, options) => Array.isArray(element) ? element.forEach(el => (0, _prettyDom.logDOM)(el, maxLength, options)) : (0, _prettyDom.logDOM)(element, maxLength, options);
  25. const logTestingPlaygroundURL = (element = (0, _helpers.getDocument)().body) => {
  26. if (!element || !('innerHTML' in element)) {
  27. console.log(`The element you're providing isn't a valid DOM element.`);
  28. return;
  29. }
  30. if (!element.innerHTML) {
  31. console.log(`The provided element doesn't have any children.`);
  32. return;
  33. }
  34. console.log(`Open this URL in your browser\n\n${getPlaygroundUrl(element.innerHTML)}`);
  35. };
  36. const initialValue = {
  37. debug,
  38. logTestingPlaygroundURL
  39. };
  40. const screen = typeof document !== 'undefined' && document.body ? (0, _getQueriesForElement.getQueriesForElement)(document.body, queries, initialValue) : Object.keys(queries).reduce((helpers, key) => {
  41. helpers[key] = () => {
  42. throw new TypeError('For queries bound to document.body a global document has to be available... Learn more: https://testing-library.com/s/screen-global-error');
  43. };
  44. return helpers;
  45. }, initialValue);
  46. exports.screen = screen;