screen.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.screen = void 0;
  7. var _lzString = _interopRequireDefault(require("lz-string"));
  8. var _getQueriesForElement = require("./get-queries-for-element");
  9. var _helpers = require("./helpers");
  10. var _prettyDom = require("./pretty-dom");
  11. var queries = _interopRequireWildcard(require("./queries"));
  12. 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); }
  13. 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; }
  14. // WARNING: `lz-string` only has a default export but statically we assume named exports are allowd
  15. // TODO: Statically verify we don't rely on NodeJS implicit named imports.
  16. function unindent(string) {
  17. // remove white spaces first, to save a few bytes.
  18. // testing-playground will reformat on load any ways.
  19. return string.replace(/[ \t]*[\n][ \t]*/g, '\n');
  20. }
  21. function encode(value) {
  22. return _lzString.default.compressToEncodedURIComponent(unindent(value));
  23. }
  24. function getPlaygroundUrl(markup) {
  25. return `https://testing-playground.com/#markup=${encode(markup)}`;
  26. }
  27. const debug = (element, maxLength, options) => Array.isArray(element) ? element.forEach(el => (0, _prettyDom.logDOM)(el, maxLength, options)) : (0, _prettyDom.logDOM)(element, maxLength, options);
  28. const logTestingPlaygroundURL = (element = (0, _helpers.getDocument)().body) => {
  29. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  30. if (!element || !('innerHTML' in element)) {
  31. console.log(`The element you're providing isn't a valid DOM element.`);
  32. return;
  33. }
  34. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  35. if (!element.innerHTML) {
  36. console.log(`The provided element doesn't have any children.`);
  37. return;
  38. }
  39. const playgroundUrl = getPlaygroundUrl(element.innerHTML);
  40. console.log(`Open this URL in your browser\n\n${playgroundUrl}`);
  41. return playgroundUrl;
  42. };
  43. const initialValue = {
  44. debug,
  45. logTestingPlaygroundURL
  46. };
  47. const screen = typeof document !== 'undefined' && document.body // eslint-disable-line @typescript-eslint/no-unnecessary-condition
  48. ? (0, _getQueriesForElement.getQueriesForElement)(document.body, queries, initialValue) : Object.keys(queries).reduce((helpers, key) => {
  49. // `key` is for all intents and purposes the type of keyof `helpers`, which itself is the type of `initialValue` plus incoming properties from `queries`
  50. // if `Object.keys(something)` returned Array<keyof typeof something> this explicit type assertion would not be necessary
  51. // see https://stackoverflow.com/questions/55012174/why-doesnt-object-keys-return-a-keyof-type-in-typescript
  52. helpers[key] = () => {
  53. 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');
  54. };
  55. return helpers;
  56. }, initialValue);
  57. exports.screen = screen;