UNPKG

jest-preset-angular

Version:

Jest preset configuration for Angular projects

154 lines (153 loc) 4.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.MockableJSDOM = void 0; const jsdom = __importStar(require("jsdom")); class DummyLocation { _hash = ''; _host = ''; _hostname = ''; _href = ''; _origin = ''; _pathname = ''; _port = ''; _protocol = ''; _search = ''; get hash() { return this._hash; } set hash(value) { this._hash = value; } get host() { return this._host; } set host(value) { this._host = value; } get hostname() { return this._hostname; } set hostname(value) { this._hostname = value; } get href() { return this._href; } set href(value) { this._href = value; } get origin() { return this._origin; } set origin(value) { this._origin = value; } get pathname() { return this._pathname; } set pathname(value) { this._pathname = value; } get port() { return this._port; } set port(value) { this._port = value; } get protocol() { return this._protocol; } set protocol(value) { this._protocol = value; } get search() { return this._search; } set search(value) { this._search = value; } toString() { return this._href; } assign(url) { this._href = String(url); } reload() { } replace(url) { this._href = String(url); } } class MockableJSDOM extends jsdom.JSDOM { #mockLocation = new DummyLocation(); #originalWindow; #documentProxy; #windowProxy; constructor(html, options) { super(html, options); this.#originalWindow = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Object.getPrototypeOf(this)), 'window').get.call(this); this.#documentProxy = new Proxy(this.#originalWindow.document, { get: (target, prop, receiver) => { if (prop !== 'location') return Reflect.get(target, prop, receiver); return this.#mockLocation; }, set: (target, prop, value, receiver) => { if (prop === 'location') { return true; } return Reflect.set(target, prop, value, receiver); }, }); this.#windowProxy = new Proxy(this.#originalWindow, { get: (target, prop, receiver) => { switch (prop) { case 'document': return this.#documentProxy; case 'location': return this.#mockLocation; default: { const value = Reflect.get(target, prop, target); return value; } } }, }); } get window() { return this.#windowProxy; } } exports.MockableJSDOM = MockableJSDOM;