Skip to content

Commit

Permalink
Merge pull request #1075 from capricorn86/task/802-dialog-is-not-acce…
Browse files Browse the repository at this point in the history
…ssible

#802@patch: Fixes issue with wrong computed style for "display" being…
  • Loading branch information
capricorn86 committed Sep 18, 2023
2 parents bbec9cf + 437144c commit 2aa2a66
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 18 deletions.
6 changes: 3 additions & 3 deletions packages/happy-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
"watch": "tsc -w --preserveWatchOutput",
"lint": "eslint --ignore-path .gitignore --max-warnings 0 .",
"lint:fix": "eslint --ignore-path .gitignore --max-warnings 0 --fix .",
"test": "vitest run",
"test": "vitest run --singleThread",
"test:ui": "vitest --ui",
"test:watch": "vitest",
"test:debug": "vitest run --inspect-brk --threads false"
"test:watch": "vitest --singleThread",
"test:debug": "vitest run --inspect-brk --threads=false"
},
"dependencies": {
"css.escape": "^1.5.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ export default class CSSStyleDeclarationElementStyle {

let elementCSSText = '';
if (CSSStyleDeclarationElementDefaultCSS[(<IElement>parentElement.element).tagName]) {
if (
typeof CSSStyleDeclarationElementDefaultCSS[(<IElement>parentElement.element).tagName] ===
'string'
) {
elementCSSText +=
CSSStyleDeclarationElementDefaultCSS[(<IElement>parentElement.element).tagName];
} else {
for (const key of Object.keys(
CSSStyleDeclarationElementDefaultCSS[(<IElement>parentElement.element).tagName]
)) {
if (key === 'default' || !!parentElement.element[key]) {
elementCSSText +=
CSSStyleDeclarationElementDefaultCSS[(<IElement>parentElement.element).tagName][
key
];
}
}
}
elementCSSText +=
CSSStyleDeclarationElementDefaultCSS[(<IElement>parentElement.element).tagName];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export default {
DEL: '',
DETAILS: 'display: block;',
DFN: '',
DIALOG: 'display: none;',
DIALOG: {
default: 'display: none;',
open: 'display: block;'
},
DIV: 'display: block;',
DL: 'display: block;',
DT: 'display: block;',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,26 @@ export default class HTMLDialogElement extends HTMLElement implements IHTMLDialo
public oncancel: (event: Event) => void | null = null;
public onclose: (event: Event) => void | null = null;

/**
* Sets the "open" attribute.
*
* @param open Open.
*/
public set open(open: boolean) {
if (open) {
this.setAttribute('open', '');
} else {
this.removeAttribute('open');
}
}

/**
* Returns open.
*
* @returns Open.
*/
public get open(): boolean {
return this.hasAttributeNS(null, 'open');
return this.getAttribute('open') !== null;
}

/**
Expand Down
26 changes: 20 additions & 6 deletions packages/happy-dom/test/console/VirtualConsole.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, it, expect } from 'vitest';
import { beforeEach, afterEach, describe, it, expect, vi } from 'vitest';
import VirtualConsole from '../../src/console/VirtualConsole.js';
import VirtualConsolePrinter from '../../src/console/VirtualConsolePrinter.js';

Expand All @@ -14,6 +14,10 @@ describe('VirtualConsole', () => {
virtualConsole = new VirtualConsole(virtualConsolePrinter);
});

afterEach(() => {
vi.restoreAllMocks();
});

describe('assert()', () => {
it('Should print a message if the assertion is false.', () => {
virtualConsole.assert(false, 'Test', { test: true });
Expand Down Expand Up @@ -221,37 +225,47 @@ describe('VirtualConsole', () => {

describe('time()', () => {
it('Should store time for the default label.', () => {
let performanceNow = 12345;
vi.spyOn(performance, 'now').mockImplementation(() => performanceNow++);
virtualConsole.time();
virtualConsole.timeEnd();
expect(virtualConsolePrinter.readAsString()).toMatch(/default: [\d.]+ms/);
expect(virtualConsolePrinter.readAsString()).toBe('default: 1ms - timer ended\n');
});

it('Should store time for a label.', () => {
let performanceNow = 12345;
vi.spyOn(performance, 'now').mockImplementation(() => performanceNow++);
virtualConsole.time('test');
virtualConsole.timeEnd('test');
expect(virtualConsolePrinter.readAsString()).toMatch(/test: [\d.]+ms/);
expect(virtualConsolePrinter.readAsString()).toBe('test: 1ms - timer ended\n');
});
});

describe('timeEnd()', () => {
it('Should print the time between the stored start time and when it was ended.', () => {
let performanceNow = 12345;
vi.spyOn(performance, 'now').mockImplementation(() => performanceNow++);
virtualConsole.time();
virtualConsole.timeEnd();
expect(virtualConsolePrinter.readAsString()).toMatch(/default: [\d.]+ms/);
expect(virtualConsolePrinter.readAsString()).toBe('default: 1ms - timer ended\n');
});
});

describe('timeLog()', () => {
it('Should print the time between the stored start time and when it was ended.', () => {
let performanceNow = 12345;
vi.spyOn(performance, 'now').mockImplementation(() => performanceNow++);
virtualConsole.time();
virtualConsole.timeLog();
expect(virtualConsolePrinter.readAsString()).toMatch(/default: [\d.]+ms/);
expect(virtualConsolePrinter.readAsString()).toMatch('default: 1ms');
});

it('Should print the time between the stored start time and when it was ended with a label.', () => {
let performanceNow = 12345;
vi.spyOn(performance, 'now').mockImplementation(() => performanceNow++);
virtualConsole.time('test');
virtualConsole.timeLog('test');
expect(virtualConsolePrinter.readAsString()).toMatch(/test: [\d.]+ms/);
expect(virtualConsolePrinter.readAsString()).toMatch('test: 1ms');
});
});

Expand Down
10 changes: 3 additions & 7 deletions packages/happy-dom/test/event/Event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,10 @@ describe('Event', () => {

describe('get timeStamp()', () => {
it('Returns the value returned by performance.now() at the time it was created.', () => {
Object.defineProperty(performance, 'now', {
value: vi.fn(),
configurable: true,
writable: true
});

const performanceNow = 12345;
vi.spyOn(performance, 'now').mockImplementation(() => performanceNow);
vi.spyOn(performance, 'now').mockImplementation(() => {
return performanceNow;
});
const event = new Event('click');
expect(event.timeStamp).toBe(performanceNow);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ describe('HTMLDialogElement', () => {
});
});

describe('set open()', () => {
it('Should set the open state', () => {
element.open = true;
expect(element.open).toBe(true);
element.open = false;
expect(element.open).toBe(false);
});
});

describe('get open()', () => {
it('Should be closed by default', () => {
expect(element.open).toBe(false);
Expand Down
22 changes: 22 additions & 0 deletions packages/happy-dom/test/window/Window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { beforeEach, afterEach, describe, it, expect, vi } from 'vitest';
import VirtualConsole from '../../src/console/VirtualConsole.js';
import VirtualConsolePrinter from '../../src/console/VirtualConsolePrinter.js';
import PackageVersion from '../../src/version.js';
import { IHTMLDialogElement } from '../../src/index.js';

const GET_NAVIGATOR_PLATFORM = (): string => {
return (
Expand Down Expand Up @@ -528,12 +529,33 @@ describe('Window', () => {
const element = <IHTMLElement>document.createElement('div');
const computedStyle = window.getComputedStyle(element);

expect(computedStyle.display).toBe('');

window.document.body.appendChild(element);

expect(computedStyle.direction).toBe('ltr');
expect(computedStyle.display).toBe('block');
});

it('Handles default properties "display" on a dialog element.', () => {
const element = <IHTMLDialogElement>document.createElement('dialog');
const computedStyle = window.getComputedStyle(element);

expect(computedStyle.display).toBe('');

window.document.body.appendChild(element);

expect(computedStyle.display).toBe('none');

element.show();

expect(computedStyle.display).toBe('block');

element.close();

expect(computedStyle.display).toBe('none');
});

it('Returns a CSSStyleDeclaration object with computed styles that are live updated whenever the element styles are changed.', () => {
const element = <IHTMLElement>document.createElement('div');
const computedStyle = window.getComputedStyle(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@ describe('TestingLibrary', () => {

expect(clickHandler).toHaveBeenCalledTimes(1);
});

it('Finds element using "screen.getByRole()".', () => {
render(<dialog open></dialog>);

const element = screen.getByRole('dialog');

expect(element).toBeInstanceOf(HTMLDialogElement);
});
});

0 comments on commit 2aa2a66

Please sign in to comment.