'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var leftest = require('@antischematic/leftest'); require('cypress-pipe'); var _a; class CypressTestSuiteAdapter { constructor() { this.isAsync = false; } suite(name, impl, flag) { switch (flag) { case leftest.Flag.SKIP: describe.skip(name, impl); break; case leftest.Flag.ONLY: describe.only(name, impl); break; case leftest.Flag.DEFAULT: describe(name, impl); } } test(name, impl, { flag }) { switch (flag) { case leftest.Flag.SKIP: it.skip(name, () => impl()); break; case leftest.Flag.ONLY: it.only(name, () => impl()); break; case leftest.Flag.DEFAULT: it(name, () => impl()); } } step(name, description, impl) { cy.step(name, description, impl); } beforeScenario(impl) { cy.beforeScenario(impl); } beforeStep(impl) { cy.beforeStep(impl); } afterScenario(impl) { cy.afterScenario(impl); } afterStep(impl) { cy.afterStep(impl); } } function collapseLastGroup() { const openExpanders = window.top.document.getElementsByClassName('command-expander-is-open'); const numExpanders = openExpanders.length; const el = openExpanders[numExpanders - 1]; if (el) el.parentElement.click(); } Cypress.Commands.add('step', (name, description, impl) => { Cypress.log({ name, message: description, type: 'parent', groupStart: true }); cy.then(impl); cy.then(() => { collapseLastGroup(); Cypress.log({ groupEnd: true, emitOnly: true }); }); }); const hooks = [['beforeScenario', 'Before Scenario'], ['beforeStep', 'Before Step'], ['afterScenario', 'After Scenario'], ['afterStep', 'After Step']]; for (const [hook, name] of hooks) { Cypress.Commands.add(hook, (impl) => { Cypress.log({ name, message: '', type: 'parent', groupStart: true }); cy.then(impl); cy.then(() => { collapseLastGroup(); Cypress.log({ groupEnd: true, emitOnly: true }); }); }); } leftest.setAdapter(new CypressTestSuiteAdapter()); leftest.setTags((_a = Cypress.env("LEFTEST_TAGS")) !== null && _a !== void 0 ? _a : ""); /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; class CypressHarnessEnvironment extends leftest.HarnessEnvironment { constructor(rawRootElement, { documentRoot }) { super(rawRootElement); this._documentRoot = documentRoot; } static getNativeElement(element) { if (element instanceof leftest.UnitTestElement) { return element.element; } if (element instanceof leftest.ComponentHarness) { return this.getNativeElement(element.host()); } throw new Error("This TestElement was not created by the CypressHarnessEnvironment"); } forceStabilize() { return __awaiter(this, void 0, void 0, function* () { const window = this._documentRoot.ownerDocument.defaultView; if (window) { yield new Promise(window.requestAnimationFrame); } else { throw new Error('Could not get window object'); } }); } waitForTasksOutsideAngular() { throw new Error('Method not implemented.'); } getDocumentRoot() { return this._documentRoot; } createTestElement(element) { return new leftest.UnitTestElement(element, () => Promise.resolve()); } createEnvironment(element) { return new CypressHarnessEnvironment(element, { documentRoot: this._documentRoot, }); } getAllRawElements(selector) { return __awaiter(this, void 0, void 0, function* () { return Array.from(this.rawRootElement.querySelectorAll(selector)); }); } } function getNativeElement(handle) { return __awaiter(this, void 0, void 0, function* () { if (handle && "then" in handle && typeof handle.then === 'function') { return getNativeElement(yield handle); } else if (handle instanceof leftest.ComponentHarness) { return leftest.UnitTestHarnessEnvironment.getNativeElement(yield handle.host()); } return null; }); } /** * Adds harness methods to chainer. * * Given a harness with a `getValue()` method, * users can call `getHarness().getValue()` * instead of `getHarness().invoke('getValue')` */ function addHarnessMethodsToChainer(chainer) { const handler = { get: (chainableTarget, prop) => (...args) => { /* Don't wrap cypress methods like `invoke`, `should` etc.... */ if (prop in chainableTarget) { return chainableTarget.then((target) => __awaiter(this, void 0, void 0, function* () { var _a; return (Array.isArray(target) ? Promise.all(target.map(t => { var _a; return (_a = CypressHarnessEnvironment.getNativeElement(t)) !== null && _a !== void 0 ? _a : t; })) : typeof target === "object" ? (_a = (yield getNativeElement(target))) !== null && _a !== void 0 ? _a : target : target); }))[prop](...args); } return addHarnessMethodsToChainer(chainableTarget.then((target) => target[prop](...args))); }, }; return new Proxy(chainer, handler); } function getDocumentRoot() { return cy.get('body', { log: false, }); } function createRootEnvironment($documentRoot) { const documentRoot = $documentRoot.get(0); return new CypressHarnessEnvironment(documentRoot, { documentRoot }); } function getAllHarnesses(query) { /* Create a local variable so `pipe` can log name. */ const getAllHarnesses = ($documentRoot) => createRootEnvironment($documentRoot).getAllHarnesses(query); return new Proxy({}, { get: (_, prop) => getDocumentRoot().pipe(getAllHarnesses)[prop], }); } function getHarness(query) { /* Create a local variable so `pipe` can log name. */ const getHarness = ($documentRoot) => { return createRootEnvironment($documentRoot).getHarness(query); }; return new Proxy({}, { get: (_, prop) => addHarnessMethodsToChainer(getDocumentRoot().pipe(getHarness))[prop], }); } function getRootHarness(harnessType) { /* Create a local variable so `pipe` can log name. */ const getRootHarness = ($documentRoot) => { var _a; const documentRoot = $documentRoot.get(0); const rootEl = /* This is the selector when using `@jscutlery/cypress-mount>=0.5.0` (real app bootstrap). */ (_a = documentRoot.querySelector('#root')) !== null && _a !== void 0 ? _a : /* This is the selector when using `@jscutlery/cypress-mount<0.5.0` (TestBed). */ documentRoot.querySelector('#root0'); if (rootEl == null) { throw new Error('Root element not found'); } return new harnessType(new CypressHarnessEnvironment(rootEl, { documentRoot, })); }; return new Proxy({}, { get: (_, prop) => addHarnessMethodsToChainer(getDocumentRoot().pipe(getRootHarness))[prop], }); } exports.CypressTestSuiteAdapter = CypressTestSuiteAdapter; exports.getAllHarnesses = getAllHarnesses; exports.getHarness = getHarness; exports.getRootHarness = getRootHarness;