fix(matchers): toHaveClass on SVG elements (#15267)

Fixes #15260
This commit is contained in:
Max Schmitt 2022-06-30 16:01:26 +02:00 коммит произвёл GitHub
Родитель b0bb99f413
Коммит 71fc53bbf8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 8 добавлений и 3 удалений

Просмотреть файл

@ -1071,7 +1071,7 @@ export class InjectedScript {
if (expression === 'to.have.attribute') {
received = element.getAttribute(options.expressionArg) || '';
} else if (expression === 'to.have.class') {
received = element.className;
received = element.classList.toString();
} else if (expression === 'to.have.css') {
received = window.getComputedStyle(element).getPropertyValue(options.expressionArg);
} else if (expression === 'to.have.id') {
@ -1112,7 +1112,7 @@ export class InjectedScript {
if (expression === 'to.have.text.array' || expression === 'to.contain.text.array')
received = elements.map(e => options.useInnerText ? (e as HTMLElement).innerText : e.textContent || '');
else if (expression === 'to.have.class.array')
received = elements.map(e => e.className);
received = elements.map(e => e.classList.toString());
if (received && options.expectedText) {
// "To match an array" is "to contain an array" + "equal length"

Просмотреть файл

@ -216,6 +216,11 @@ test('should support toHaveClass', async ({ runInlineTest }) => {
await expect(locator).toHaveClass('foo bar baz');
});
test('pass with SVGs', async ({ page }) => {
await page.setContent(\`<svg class="c1 c2" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"></svg>\`);
await expect(page.locator('svg')).toHaveClass(/c1/);
});
test('fail', async ({ page }) => {
await page.setContent('<div class="bar baz"></div>');
const locator = page.locator('div');
@ -226,7 +231,7 @@ test('should support toHaveClass', async ({ runInlineTest }) => {
const output = stripAnsi(result.output);
expect(output).toContain('expect(locator).toHaveClass');
expect(output).toContain('Expected string: \"foo bar baz\"');
expect(result.passed).toBe(1);
expect(result.passed).toBe(2);
expect(result.failed).toBe(1);
expect(result.exitCode).toBe(1);
});