diff --git a/libs/components/core/testing/src/public-api.ts b/libs/components/core/testing/src/public-api.ts index 8ffea0553f..fc6455e482 100644 --- a/libs/components/core/testing/src/public-api.ts +++ b/libs/components/core/testing/src/public-api.ts @@ -10,6 +10,7 @@ export { } from './resize-observer-mock'; export { SkyComponentHarness } from './shared/component-harness'; export { SkyHarnessFilters } from './shared/harness-filters'; +export { SkyHarnessUtility } from './shared/harness-utility'; export { SkyQueryableComponentHarness } from './shared/queryable-component-harness'; export { SkyHelpTestingController } from './help/help-testing-controller'; export { SkyHelpTestingModule } from './help/help-testing.module'; diff --git a/libs/components/core/testing/src/shared/harness-utility.spec.ts b/libs/components/core/testing/src/shared/harness-utility.spec.ts new file mode 100644 index 0000000000..527248e513 --- /dev/null +++ b/libs/components/core/testing/src/shared/harness-utility.spec.ts @@ -0,0 +1,40 @@ +import { TestElement } from '@angular/cdk/testing'; + +import { SkyHarnessUtility } from './harness-utility'; + +describe('Harness utility', () => { + async function validate( + imageCss: string, + imageUrl: string | undefined, + ): Promise { + const el: Partial = { + getCssValue: async (property) => { + if (property === 'background-image') { + return Promise.resolve(imageCss); + } + + return ''; + }, + }; + + await expectAsync( + SkyHarnessUtility.getBackgroundImageUrl(el as TestElement), + ).toBeResolvedTo(imageUrl); + } + + it('should get the background image of a test element', async () => { + await validate( + `url('https://example.com/bg.png')`, + 'https://example.com/bg.png', + ); + + await validate( + 'url("https://example.com/bg.png")', + 'https://example.com/bg.png', + ); + + await validate('url("blob:example.com/abc")', 'blob:example.com/abc'); + + await validate('', undefined); + }); +}); diff --git a/libs/components/core/testing/src/shared/harness-utility.ts b/libs/components/core/testing/src/shared/harness-utility.ts new file mode 100644 index 0000000000..f1c3127bd4 --- /dev/null +++ b/libs/components/core/testing/src/shared/harness-utility.ts @@ -0,0 +1,11 @@ +import { TestElement } from '@angular/cdk/testing'; + +export class SkyHarnessUtility { + public static async getBackgroundImageUrl( + el: TestElement, + ): Promise { + const backgroundImage = await el.getCssValue('background-image'); + + return /url\(('|")([^'"]+)('|")\)/gi.exec(backgroundImage)?.[2]; + } +}