UNPKG

@skyux/core

Version:

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

518 lines (498 loc) 18.6 kB
import * as i0 from '@angular/core'; import { NgZone, Injectable, NgModule, inject } from '@angular/core'; import { SkyMediaQueryService, SkyMediaBreakpoints, SkyUIConfigService, SkyFileReaderService, SkyHelpService, SkyContainerBreakpointObserver, SkyMediaBreakpointObserver, SKY_BREAKPOINT_OBSERVER } from '@skyux/core'; import { BehaviorSubject, of, ReplaySubject } from 'rxjs'; import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing'; /** * @internal * @deprecated Use `provideSkyMediaQueryTesting()` coupled with `SkyMediaQueryTestingController`. */ class MockSkyMediaQueryService extends SkyMediaQueryService { static { this.xs = '(max-width: 767px)'; } static { this.sm = '(min-width: 768px) and (max-width: 991px)'; } static { this.md = '(min-width: 992px) and (max-width: 1199px)'; } static { this.lg = '(min-width: 1200px)'; } get current() { return this.currentBreakpoints; } set current(breakpoints) { this.currentBreakpoints = breakpoints; } constructor() { super(new NgZone({ enableLongStackTrace: true, })); this.currentMockSubject = new BehaviorSubject(this.current); this.currentBreakpoints = SkyMediaBreakpoints.md; } subscribe(listener) { return this.currentMockSubject.subscribe({ next: (breakpoints) => { listener(breakpoints); }, }); } fire(args) { this.currentBreakpoints = args; this.currentMockSubject.next(this.currentBreakpoints); } /* istanbul ignore next */ // eslint-disable-next-line @typescript-eslint/no-empty-function destroy() { } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: MockSkyMediaQueryService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: MockSkyMediaQueryService }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: MockSkyMediaQueryService, decorators: [{ type: Injectable }], ctorParameters: () => [] }); /** * @internal */ class MockSkyUIConfigService extends SkyUIConfigService { getConfig(key, defaultConfig) { switch (key) { case 'defaultSettings': return of(defaultConfig); case 'badData': return of({ invalidProperty: 'invalidData' }); default: { return of({ settings: { userSettings: { singleColumn: { tiles: [ { id: 'tile-1', isCollapsed: true, }, { id: 'tile-2', isCollapsed: true, }, ], }, multiColumn: [ { tiles: [ { id: 'tile-2', isCollapsed: true, }, ], }, { tiles: [ { id: 'tile-1', isCollapsed: true, }, ], }, ], }, defaultSettings: ['tile-1', 'tile-2'], }, }); } } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: MockSkyUIConfigService, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: MockSkyUIConfigService }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: MockSkyUIConfigService, decorators: [{ type: Injectable }] }); /** * @internal */ class SkyCoreTestingModule { static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyCoreTestingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: SkyCoreTestingModule }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyCoreTestingModule, providers: [ { provide: SkyMediaQueryService, useClass: MockSkyMediaQueryService }, { provide: SkyUIConfigService, useClass: MockSkyUIConfigService, }, ] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyCoreTestingModule, decorators: [{ type: NgModule, args: [{ providers: [ { provide: SkyMediaQueryService, useClass: MockSkyMediaQueryService }, { provide: SkyUIConfigService, useClass: MockSkyUIConfigService, }, ], }] }] }); /* istanbul ignore file: these are used in @skyux/core */ /** * @internal */ const mockResizeObserverEntry = { target: {}, borderBoxSize: [], contentBoxSize: [], contentRect: { width: 20, height: 20, x: 20, y: 20, top: 20, bottom: 20, left: 20, right: 20, toJSON: () => 'true', }, devicePixelContentBoxSize: [], }; const defaultCallback = (entries, observer) => { }; /** * @internal */ const mockResizeObserverHandle = { callback: defaultCallback, emit: (entries, observer) => { mockResizeObserverHandle.callback(entries, observer); }, }; /** * @internal */ function mockResizeObserver() { window.requestAnimationFrame = (callback) => { callback(); return 0; }; window.cancelAnimationFrame = (_) => undefined; window.ResizeObserver = class { constructor(callback) { mockResizeObserverHandle.callback = callback; } disconnect() { } observe(target, options) { } unobserve(element) { } }; } /** * @internal */ class SkyFileReaderTestingService extends SkyFileReaderService { async readAsDataURL(file) { return await new Promise((resolve) => { resolve(`data:${file.type};base64,MOCK_DATA`); }); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyFileReaderTestingService, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyFileReaderTestingService }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyFileReaderTestingService, decorators: [{ type: Injectable }] }); /** * Provides mocks for file reader testing. * @internal * @example * ```typescript * TestBed.configureTestingModule({ * providers: [provideSkyFileReaderTesting()] * }); * ``` */ function provideSkyFileReaderTesting() { return [ SkyFileReaderTestingService, { provide: SkyFileReaderService, useClass: SkyFileReaderTestingService, }, ]; } function formatHelpKeyForError(helpKey) { return helpKey === undefined ? 'undefined' : `'${helpKey}'`; } /** * Provides methods for validating global help in unit tests. */ class SkyHelpTestingController { #helpSvc = inject(SkyHelpService); /** * Validates the current help key and throws an error if the current help key does not match the expected help key. * @param expectedHelpKey The expected help key. */ expectCurrentHelpKey(expectedHelpKey) { const currentHelpKey = this.#helpSvc.getCurrentHelpKey(); if (currentHelpKey !== expectedHelpKey) { throw new Error(`Expected current help key to be ${formatHelpKeyForError(expectedHelpKey)}, but the current help key is ${formatHelpKeyForError(currentHelpKey)}.`); } } /** * Close the current help. */ closeHelp() { this.#helpSvc.closeHelp(); } } /** * @internal */ class SkyHelpTestingService extends SkyHelpService { get widgetReadyStateChange() { return of(true); } #currentHelpKey; #currentPageHelpKey; openHelp(args) { this.#currentHelpKey = args?.helpKey; } updateHelp(args) { if ('pageDefaultHelpKey' in args) { this.#currentPageHelpKey = args.pageDefaultHelpKey; } if ('helpKey' in args) { this.#currentHelpKey = args.helpKey; } } getCurrentHelpKey() { return this.#currentHelpKey || this.#currentPageHelpKey; } closeHelp() { this.#currentHelpKey = undefined; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyHelpTestingService, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyHelpTestingService }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyHelpTestingService, decorators: [{ type: Injectable }] }); /** * Mocks SkyHelpService to enable testing of global help. */ class SkyHelpTestingModule { static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyHelpTestingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.14", ngImport: i0, type: SkyHelpTestingModule }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyHelpTestingModule, providers: [ { provide: SkyHelpService, useClass: SkyHelpTestingService, }, SkyHelpTestingController, ] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyHelpTestingModule, decorators: [{ type: NgModule, args: [{ providers: [ { provide: SkyHelpService, useClass: SkyHelpTestingService, }, SkyHelpTestingController, ], }] }] }); /** * Overrides the media and container breakpoint observers for testing. * @internal */ class SkyBreakpointObserverTesting { get breakpointChange() { return this.#breakpointChangeObs; } #breakpointChange = new ReplaySubject(1); #breakpointChangeObs = this.#breakpointChange.asObservable(); destroy() { this.#breakpointChange.complete(); } setBreakpoint(breakpoint) { this.#breakpointChange.next(breakpoint); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyBreakpointObserverTesting, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyBreakpointObserverTesting }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyBreakpointObserverTesting, decorators: [{ type: Injectable }] }); /** * A controller to be injected into tests, which mocks the * `SkyMediaQueryService` and handles interactions with breakpoints. */ class SkyMediaQueryTestingController { #observer = inject(SkyBreakpointObserverTesting); /** * Emits the provided breakpoint to all subscribers. */ setBreakpoint(breakpoint) { this.#observer.setBreakpoint(breakpoint); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyMediaQueryTestingController, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyMediaQueryTestingController }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImport: i0, type: SkyMediaQueryTestingController, decorators: [{ type: Injectable }] }); /** * Adds mocks to allow interactions with breakpoints in tests. */ function provideSkyMediaQueryTesting() { return [ SkyMediaQueryService, SkyBreakpointObserverTesting, SkyMediaQueryTestingController, { provide: SkyContainerBreakpointObserver, useExisting: SkyBreakpointObserverTesting, }, { provide: SkyMediaBreakpointObserver, useExisting: SkyBreakpointObserverTesting, }, { provide: SKY_BREAKPOINT_OBSERVER, useExisting: SkyBreakpointObserverTesting, }, ]; } /** * @internal */ class SkyComponentHarness extends ComponentHarness { static getDataSkyIdPredicate(filters) { return new HarnessPredicate(this, filters).addOption('dataSkyId', filters.dataSkyId, (harness, text) => HarnessPredicate.stringMatches(harness.#getSkyId(), text)); } async #getSkyId() { return await (await this.host()).getAttribute('data-sky-id'); } } /** * @internal */ class SkyQueryableComponentHarness extends SkyComponentHarness { /** * Returns a child harness or throws an error if not found. */ async queryHarness(query) { return await this.locatorFor(query)(); } /** * Returns a child harness or null if not found. */ async queryHarnessOrNull(query) { return await this.locatorForOptional(query)(); } /** * Returns child harnesses. */ async queryHarnesses(harness) { return await this.locatorForAll(harness)(); } /** * Returns a child test element or throws an error if not found. */ async querySelector(selector) { return await this.locatorFor(selector)(); } /** * Returns a child test element or null if not found. */ async querySelectorOrNull(selector) { return await this.locatorForOptional(selector)(); } /** * Returns child test elements. */ async querySelectorAll(selector) { return await this.locatorForAll(selector)(); } } /** * Harness for interacting with an overlay component in tests. * @internal */ class SkyOverlayHarness extends SkyQueryableComponentHarness { /** * @internal */ static { this.hostSelector = 'sky-overlay'; } /** * Gets a `HarnessPredicate` that can be used to search for a * `SkyOverlayHarness` that meets certain criteria. */ static with(filters) { return new HarnessPredicate(SkyOverlayHarness, filters); } } class SkyHarnessUtility { static async getBackgroundImageUrl(el) { const backgroundImage = await el.getCssValue('background-image'); return /url\(('|")([^'"]+)('|")\)/gi.exec(backgroundImage)?.[2]; } } /** * Harness used to interact with native input elements in tests. */ class SkyInputHarness extends ComponentHarness { /** * Blurs the input. */ async blur() { await (await this.host()).blur(); } /** * Clears the input value. */ async clear() { await (await this.host()).clear(); } /** * Focuses the input. */ async focus() { await (await this.host()).focus(); } /** * Gets the value of the input. */ async getValue() { return await (await this.host()).getProperty('value'); } /** * Whether the input is disabled. */ async isDisabled() { const disabled = await (await this.host()).getAttribute('disabled'); return disabled !== null; } /** * Whether the input is focused. */ async isFocused() { return await (await this.host()).isFocused(); } /** * Sets the value of the input. The value will be set by simulating key * presses that correspond to the given value. */ async setValue(value) { const inputEl = await this.host(); await inputEl.clear(); // We don't want to send keys for the value if the value is an empty // string in order to clear the value. Sending keys with an empty string // still results in unnecessary focus events. if (value) { await inputEl.sendKeys(value); } // Some input types won't respond to key presses (e.g. `color`) so to be sure that the // value is set, we also set the property after the keyboard sequence. Note that we don't // want to do it before, because it can cause the value to be entered twice. await inputEl.setInputValue(value); } } /** * Generated bundle index. Do not edit. */ export { MockSkyMediaQueryService, MockSkyUIConfigService, SkyComponentHarness, SkyCoreTestingModule, SkyHarnessUtility, SkyHelpTestingController, SkyHelpTestingModule, SkyInputHarness, SkyMediaQueryTestingController, SkyOverlayHarness, SkyQueryableComponentHarness, mockResizeObserver, mockResizeObserverEntry, mockResizeObserverHandle, provideSkyFileReaderTesting, provideSkyMediaQueryTesting }; //# sourceMappingURL=skyux-core-testing.mjs.map