UNPKG

@skyux-sdk/testing

Version:

This library was generated with [Nx](https://nx.dev).

361 lines (354 loc) 13 kB
import { _skyTestingCheckResourceTemplate, _skyTestingHasResourceText, _skyTestingCheckResourceText, _skyTestingHasText, _skyTestingHasStyle, _skyTestingHasCssClass, _skyTestingCheckExistence, _skyTestingCheckVisibility, _SkyA11yAnalyzer, _skyTestingCheckLibResourceTemplate, _skyTestingHasLibResourceText, _skyTestingCheckLibResourceText, _skyTestingCheckAccessibility } from '@skyux-sdk/testing/private'; export { _SkyA11yAnalyzer as SkyA11yAnalyzer } from '@skyux-sdk/testing/private'; import { By } from '@angular/platform-browser'; // eslint-disable-next-line @nx/enforce-module-boundaries const windowRef = window; const matchers = { toBeAccessible() { return { compare(element, callback, config) { _SkyA11yAnalyzer .run(element, config) .then(() => { /*istanbul ignore else*/ if (callback) { callback(); } }) .catch((err) => { windowRef.fail(err.message); /*istanbul ignore else*/ if (callback) { callback(); } }); // Asynchronous matchers are currently unsupported, but // the method above works to fail the specific test in the // callback manually, if checks do not pass. // --- // A side effect of this technique is the matcher cannot be // paired with a `.not.toBeAccessible` operator (since the returned // result is always `true`). For this particular matcher, // checking if an element is not accessible may be irrelevant. return { message: '', pass: true, }; }, }; }, toBeVisible() { return { compare(el, options) { const { pass, message } = _skyTestingCheckVisibility(el, options); return { pass, message, }; }, }; }, toExist() { return { compare(el) { const { pass, message } = _skyTestingCheckExistence(el); return { pass, message }; }, }; }, toHaveCssClass() { return { compare(el, expectedClassName) { const { pass, message } = _skyTestingHasCssClass(el, expectedClassName); return { pass, message }; }, }; }, toHaveStyle() { return { compare(el, expectedStyles) { const { pass, message } = _skyTestingHasStyle(el, expectedStyles); return { pass, message, }; }, }; }, toHaveText() { return { compare(el, expectedText, trimWhitespace = true) { const { pass, message } = _skyTestingHasText(el, expectedText, trimWhitespace); return { pass, message }; }, }; }, toEqualResourceText() { return { compare(actual, name, args, callback) { void _skyTestingCheckResourceText(actual, name, args).then(({ pass, message }) => { /*istanbul ignore else*/ if (!pass) { windowRef.fail(message); } /*istanbul ignore else*/ if (callback) { callback(); } }); // Asynchronous matchers are currently unsupported, but // the method above works to fail the specific test in the // callback manually, if checks do not pass. // --- // A side effect of this technique is the matcher cannot be // paired with a `.not.toHaveResourceText` operator (since the returned // result is always `true`). return { message: '', pass: true, }; }, }; }, toHaveResourceText() { return { compare(el, name, args, trimWhitespace = true, callback) { void _skyTestingHasResourceText(el, name, args, trimWhitespace).then(({ pass, message }) => { if (!pass) { windowRef.fail(message); } /*istanbul ignore else*/ if (callback) { callback(); } }); // Asynchronous matchers are currently unsupported, but // the method above works to fail the specific test in the // callback manually, if checks do not pass. // --- // A side effect of this technique is the matcher cannot be // paired with a `.not.toHaveResourceText` operator (since the returned // result is always `true`). return { message: '', pass: true, }; }, }; }, toMatchResourceTemplate() { return { compare(el, name, callback) { void _skyTestingCheckResourceTemplate(el, name).then(({ pass, message }) => { if (!pass) { windowRef.fail(message); } /*istanbul ignore else*/ if (callback) { callback(); } }); // Asynchronous matchers are currently unsupported, but // the method above works to fail the specific test in the // callback manually, if checks do not pass. // --- // A side effect of this technique is the matcher cannot be // paired with a `.not.toHaveResourceText` operator (since the returned // result is always `true`). return { message: '', pass: true, }; }, }; }, }; const asyncMatchers = { toBeAccessible() { return { async compare(element, config) { const { pass, message } = await _skyTestingCheckAccessibility(element, config); return { pass, message }; }, }; }, toEqualResourceText() { return { async compare(actual, name, args) { const { pass, message } = await _skyTestingCheckResourceText(actual, name, args); return { pass, message }; }, }; }, toEqualLibResourceText() { return { async compare(actual, name, args) { const { pass, message } = await _skyTestingCheckLibResourceText(actual, name, args); return { pass, message }; }, }; }, toHaveResourceText() { return { async compare(element, name, args, trimWhitespace = true) { const { pass, message } = await _skyTestingHasResourceText(element, name, args, trimWhitespace); return { pass, message }; }, }; }, toHaveLibResourceText() { return { async compare(element, name, args, trimWhitespace = true) { const { pass, message } = await _skyTestingHasLibResourceText(element, name, args, trimWhitespace); return { pass, message }; }, }; }, toMatchResourceTemplate() { return { async compare(element, name) { const { pass, message } = await _skyTestingCheckResourceTemplate(element, name); return { pass, message }; }, }; }, toMatchLibResourceTemplate() { return { async compare(element, name) { const { pass, message } = await _skyTestingCheckLibResourceTemplate(element, name); return { pass, message }; }, }; }, }; /** * @internal */ function registerJasmineMatchers() { if (typeof jasmine !== 'undefined') { windowRef.beforeEach(() => { jasmine.addMatchers(matchers); jasmine.addAsyncMatchers(asyncMatchers); }); } } registerJasmineMatchers(); /** * Create an expectation for a spec. * @param actual Actual computed value to test expectations against. */ function expect(actual) { return windowRef.expect(actual); } /** * Create an async expectation for a spec. * @param actual Actual computed value to test expectations against. */ function expectAsync(actual) { return windowRef.expectAsync(actual); } class SkyBy { static dataSkyId(skyId) { return By.css(`[data-sky-id="${skyId}"]`); } } function getNativeEl(el) { if (!el) { return undefined; } if (el.nativeElement) { return el.nativeElement; } return el; } class SkyAppTestUtility { static fireDomEvent(element, eventName, options) { if (!element) { throw new Error(`Event \`${eventName}\` could not be fired because the element is not defined.`); } const defaults = { bubbles: true, cancelable: true, keyboardEventInit: {}, }; const settings = Object.assign({}, defaults, options); // Apply keyboard event options. const event = Object.assign(document.createEvent('CustomEvent'), settings.keyboardEventInit, settings.customEventInit); event.initEvent(eventName, settings.bubbles, settings.cancelable); element.dispatchEvent(event); } /** * Returns the inner text content of an element. */ static getText(element) { const nativeEl = getNativeEl(element); if (nativeEl) { return nativeEl.innerText.trim(); } return undefined; } /** * Returns true if the element exists on the page. */ static isVisible(element) { const nativeEl = getNativeEl(element); if (nativeEl) { return getComputedStyle(nativeEl).display !== 'none'; } return undefined; } /** * Sets the value of an input element and triggers its 'input' and 'change' events. */ static setInputValue(element, value) { const inputEvent = document.createEvent('Event'); inputEvent.initEvent('input', false, false); const changeEvent = document.createEvent('Event'); changeEvent.initEvent('change', false, false); element.value = value; element.dispatchEvent(inputEvent); } /** * Returns the URL of an element's background image, if it exists. */ static getBackgroundImageUrl(el) { const nativeEl = getNativeEl(el); if (nativeEl) { const backgroundImageUrl = getComputedStyle(nativeEl).backgroundImage; /* istanbul ignore else */ // Browser will likely not return an empty value for the computed style, // but leave the if statement here anyway as a sanity check. if (backgroundImageUrl) { const matches = /url\(('|")([^'"]+)('|")\)/gi.exec(backgroundImageUrl); if (matches && matches.length > 0) { return matches[2]; } } } return undefined; } /** * Returns a DebugElement representing a SKY UX component. * @internal * @param fixture The ComponentFixture where the SKY UX component resides. * @param skyTestId The value of the `data-sky-id` property specified on the SKY UX component. * @param componentSelector The selector name for the SKY UX component (e.g. 'sky-alert'). */ static getDebugElementByTestId(fixture, skyTestId, componentSelector) { const skyEl = fixture.debugElement.query(By.css(`[data-sky-id="${skyTestId}"]`)); if (!skyEl) { throw new Error(`No element was found with a \`data-sky-id\` value of "${skyTestId}".`); } if (skyEl.name !== componentSelector) { throw new Error(`The element with the test ID "${skyTestId}" is not a component of type ${componentSelector}."`); } return skyEl; } } // Export the a11y analyzer to avoid a breaking change. Remove in a future major version. // eslint-disable-next-line @nx/enforce-module-boundaries /** * Generated bundle index. Do not edit. */ export { SkyAppTestUtility, SkyBy, expect, expectAsync }; //# sourceMappingURL=skyux-sdk-testing.mjs.map