Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(components/core): add test harness utility #2851

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/components/core/testing/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
40 changes: 40 additions & 0 deletions libs/components/core/testing/src/shared/harness-utility.spec.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const el: Partial<TestElement> = {
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);
});
});
11 changes: 11 additions & 0 deletions libs/components/core/testing/src/shared/harness-utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TestElement } from '@angular/cdk/testing';

export class SkyHarnessUtility {
public static async getBackgroundImageUrl(
el: TestElement,
): Promise<string | undefined> {
const backgroundImage = await el.getCssValue('background-image');

return /url\(('|")([^'"]+)('|")\)/gi.exec(backgroundImage)?.[2];
}
}
Loading