Skip to content

Commit

Permalink
adds and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjwala committed Oct 30, 2023
1 parent 5036d52 commit acff601
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
13 changes: 11 additions & 2 deletions packages/dom/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const chromeBrowser = 'CHROME';
export const firefoxBrowser = 'FIREFOX';

// create and cleanup testing DOM
export function withExample(html, options = { withShadow: true }) {
export function withExample(html, options = { withShadow: true, invalidTagsOutsideBody: false }) {
let $test = document.getElementById('test');
if ($test) $test.remove();

Expand All @@ -16,14 +16,23 @@ export function withExample(html, options = { withShadow: true }) {

document.body.appendChild($test);

if (options?.withShadow) {
if (options.withShadow) {
$testShadow = document.createElement('div');
$testShadow.id = 'test-shadow';
let $shadow = $testShadow.attachShadow({ mode: 'open' });
$shadow.innerHTML = `<h1>Hello DOM testing</h1>${html}`;

document.body.appendChild($testShadow);
}

if (options.invalidTagsOutsideBody) {
let p = document.getElementById('invalid-p');
p?.remove();
p = document.createElement('p');
p.id = 'invalid-p';
p.innerText = 'P tag outside body';
document.documentElement.append(p);
}
return document;
}

Expand Down
25 changes: 22 additions & 3 deletions packages/dom/test/serialize-dom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ describe('serializeDOM', () => {
expect(serializeDOM()).toEqual({
html: jasmine.any(String),
warnings: jasmine.any(Array),
resources: jasmine.any(Array)
resources: jasmine.any(Array),
hints: jasmine.any(Array)
});
});

Expand All @@ -27,7 +28,7 @@ describe('serializeDOM', () => {

it('optionally returns a stringified response', () => {
expect(serializeDOM({ stringifyResponse: true }))
.toMatch('{"html":".*","warnings":\\[\\],"resources":\\[\\]}');
.toMatch('{"html":".*","warnings":\\[\\],"resources":\\[\\],"hints":\\[\\]}');
});

it('always has a doctype', () => {
Expand Down Expand Up @@ -68,7 +69,7 @@ describe('serializeDOM', () => {
expect($('h2.callback').length).toEqual(1);
});

it('applies dom transformations', () => {
it('applies default dom transformations', () => {
withExample('<img loading="lazy" src="http://some-url"/><iframe loading="lazy" src="">');

const result = serializeDOM();
Expand Down Expand Up @@ -318,4 +319,22 @@ describe('serializeDOM', () => {
expect(warnings).toEqual(['Could not transform the dom: test error']);
});
});

describe('with `reshuffleInvalidTags`', () => {
beforeEach(() => {
withExample('', { withShadow: false, invalidTagsOutsideBody: true });
});

it('does not reshuffle tags outside </body>', () => {
const result = serializeDOM();
expect(result.html).toContain('P tag outside body');
expect(result.hints).toEqual(['DOM elements found outside </body>']);
});

it('reshuffles tags outside </body>', () => {
const result = serializeDOM({ reshuffleInvalidTags: true });
expect(result.html).toContain('P tag outside body');
expect(result.hints).toEqual([]);
});
});
});

0 comments on commit acff601

Please sign in to comment.