mirror of
https://github.com/jlengrand/open-wc.git
synced 2026-03-10 08:31:19 +00:00
fix: properly apply prettier
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
module.exports = {
|
||||
extends: [
|
||||
'./packages/eslint-config/index.js',
|
||||
require.resolve('eslint-config-prettier'),
|
||||
],
|
||||
extends: ['./packages/eslint-config/index.js', require.resolve('eslint-config-prettier')],
|
||||
};
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
module.exports = {
|
||||
extends: [
|
||||
'@commitlint/config-conventional',
|
||||
'@commitlint/config-lerna-scopes',
|
||||
],
|
||||
extends: ['@commitlint/config-conventional', '@commitlint/config-lerna-scopes'],
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@ import { chaiDomEquals } from '../chai-dom-equals.js';
|
||||
if (afterEach) {
|
||||
afterEach(() => {
|
||||
if (cachedWrappers) {
|
||||
cachedWrappers.forEach((wrapper) => {
|
||||
cachedWrappers.forEach(wrapper => {
|
||||
document.body.removeChild(wrapper);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import './bdd-setup.js';
|
||||
import { expect } from '@bundled-es-modules/chai';
|
||||
import {
|
||||
fixture,
|
||||
defineCE,
|
||||
} from '@open-wc/testing-helpers';
|
||||
import { fixture, defineCE } from '@open-wc/testing-helpers';
|
||||
import { getOuterHtml, getCleanedShadowDom } from '../index.js';
|
||||
|
||||
describe('getOuterHtml', () => {
|
||||
@@ -18,113 +15,139 @@ describe('getOuterHtml', () => {
|
||||
});
|
||||
|
||||
it('supports shadow dom also on polyfilled browsers', async () => {
|
||||
const tag = defineCE(class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
const tag = defineCE(
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p>shadow content</p><slot>';
|
||||
}
|
||||
});
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p>shadow content</p><slot>';
|
||||
}
|
||||
},
|
||||
);
|
||||
// no line breaks here as it's a literal comparision e.g. line breaks are considered
|
||||
const el = await fixture(`<${tag}><span>light content</span></${tag}>`);
|
||||
expect(getOuterHtml(el)).to.equals(`<${tag}><span>light content</span></${tag}>`);
|
||||
|
||||
const el2 = await fixture(`<${tag} foo="bar" foo2="bar2"><span>light content</span></${tag}>`);
|
||||
expect(getOuterHtml(el2)).to.equals(`<${tag} foo="bar" foo2="bar2"><span>light content</span></${tag}>`);
|
||||
expect(getOuterHtml(el2)).to.equals(
|
||||
`<${tag} foo="bar" foo2="bar2"><span>light content</span></${tag}>`,
|
||||
);
|
||||
});
|
||||
|
||||
it('supports nested shadow roots', async () => {
|
||||
const innerTag = defineCE(class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
const innerTag = defineCE(
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p>inner shadow content</p><slot>';
|
||||
}
|
||||
});
|
||||
const tag = defineCE(class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p>inner shadow content</p><slot>';
|
||||
}
|
||||
},
|
||||
);
|
||||
const tag = defineCE(
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p>shadow content</p><slot>';
|
||||
}
|
||||
});
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p>shadow content</p><slot>';
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// no line breaks here as it's a literal comparision e.g. line breaks are considered
|
||||
const el = await fixture(`<${tag}><${innerTag}><span>inner light content</span></${innerTag}><span> light content</span></${tag}>`);
|
||||
expect(getOuterHtml(el)).to.equals(`<${tag}><${innerTag}><span>inner light content</span></${innerTag}><span> light content</span></${tag}>`);
|
||||
const el = await fixture(
|
||||
`<${tag}><${innerTag}><span>inner light content</span></${innerTag}><span> light content</span></${tag}>`,
|
||||
);
|
||||
expect(getOuterHtml(el)).to.equals(
|
||||
`<${tag}><${innerTag}><span>inner light content</span></${innerTag}><span> light content</span></${tag}>`,
|
||||
);
|
||||
|
||||
const el2 = await fixture(`<${tag} foo="bar" foo2="bar2"><${innerTag}><span>inner light content</span></${innerTag}><span> light content</span></${tag}>`);
|
||||
expect(getOuterHtml(el2)).to.equals(`<${tag} foo="bar" foo2="bar2"><${innerTag}><span>inner light content</span></${innerTag}><span> light content</span></${tag}>`);
|
||||
const el2 = await fixture(
|
||||
`<${tag} foo="bar" foo2="bar2"><${innerTag}><span>inner light content</span></${innerTag}><span> light content</span></${tag}>`,
|
||||
);
|
||||
expect(getOuterHtml(el2)).to.equals(
|
||||
`<${tag} foo="bar" foo2="bar2"><${innerTag}><span>inner light content</span></${innerTag}><span> light content</span></${tag}>`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCleanedShadowDom', () => {
|
||||
it('supports shadow dom also on polyfilled browsers', async () => {
|
||||
const tag = defineCE(class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
const tag = defineCE(
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p>shadow content</p><slot></slot>';
|
||||
}
|
||||
});
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p>shadow content</p><slot></slot>';
|
||||
}
|
||||
},
|
||||
);
|
||||
// no line breaks here as it's a literal comparision e.g. line breaks are considered
|
||||
const el = await fixture(`<${tag}></${tag}>`);
|
||||
expect(getCleanedShadowDom(el)).to.equals('<p>shadow content</p><slot></slot>');
|
||||
});
|
||||
|
||||
it('supports class attribute', async () => {
|
||||
const tag = defineCE(class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
const tag = defineCE(
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p class="foo">shadow content</p><slot></slot>';
|
||||
}
|
||||
});
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p class="foo">shadow content</p><slot></slot>';
|
||||
}
|
||||
},
|
||||
);
|
||||
// no line breaks here as it's a literal comparision e.g. line breaks are considered
|
||||
const el = await fixture(`<${tag}></${tag}>`);
|
||||
expect(getCleanedShadowDom(el)).to.equals('<p class="foo">shadow content</p><slot></slot>');
|
||||
});
|
||||
|
||||
it('supports nested shadow roots', async () => {
|
||||
const innerTag = defineCE(class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
const innerTag = defineCE(
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p>inner shadow content</p><slot></slot>';
|
||||
}
|
||||
});
|
||||
const tag = defineCE(class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p>inner shadow content</p><slot></slot>';
|
||||
}
|
||||
},
|
||||
);
|
||||
const tag = defineCE(
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = `<${innerTag} class="foo"></${innerTag}><p>shadow content</p><slot></slot>`;
|
||||
}
|
||||
});
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = `<${innerTag} class="foo"></${innerTag}><p>shadow content</p><slot></slot>`;
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// no line breaks here as it's a literal comparision e.g. line breaks are considered
|
||||
const el = await fixture(`<${tag}></${tag}>`);
|
||||
expect(getCleanedShadowDom(el)).to.equals(`<${innerTag} class="foo"></${innerTag}><p>shadow content</p><slot></slot>`);
|
||||
expect(getCleanedShadowDom(el)).to.equals(
|
||||
`<${innerTag} class="foo"></${innerTag}><p>shadow content</p><slot></slot>`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -142,16 +165,18 @@ describe('dom', () => {
|
||||
|
||||
describe('shadowDom', () => {
|
||||
it('can strict compare shadow dom nodes', async () => {
|
||||
const tag = defineCE(class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
const tag = defineCE(
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p> shadow content</p> <!-- comment --> <slot></slot>';
|
||||
}
|
||||
});
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p> shadow content</p> <!-- comment --> <slot></slot>';
|
||||
}
|
||||
},
|
||||
);
|
||||
const el = await fixture(`<${tag}><span> light content </span></${tag}>`);
|
||||
expect(el).dom.to.semantically.equal(`<${tag}><span>light content</span></${tag}>`);
|
||||
expect(el).shadowDom.to.equal('<p> shadow content</p> <!-- comment --> <slot></slot>');
|
||||
@@ -159,16 +184,18 @@ describe('shadowDom', () => {
|
||||
});
|
||||
|
||||
it('can semmantically compare shadow dom nodes', async () => {
|
||||
const tag = defineCE(class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
const tag = defineCE(
|
||||
class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this.attachShadow({ mode: 'open' });
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p> shadow content</p> <!-- comment --> <slot></slot>';
|
||||
}
|
||||
});
|
||||
connectedCallback() {
|
||||
this.shadowRoot.innerHTML = '<p> shadow content</p> <!-- comment --> <slot></slot>';
|
||||
}
|
||||
},
|
||||
);
|
||||
const el = await fixture(`<${tag}><span> light content </span></${tag}>`);
|
||||
expect(el).dom.to.semantically.equal(`<${tag}><span>light content</span></${tag}>`);
|
||||
expect(el).shadowDom.to.semantically.equal('<p>shadow content</p><slot>');
|
||||
|
||||
@@ -13,14 +13,16 @@ const PROMPTS = [
|
||||
];
|
||||
|
||||
function getClassName(tagName) {
|
||||
return tagName.split('-').reduce((previous, part) => previous + part.charAt(0).toUpperCase() + part.slice(1), '');
|
||||
return tagName
|
||||
.split('-')
|
||||
.reduce((previous, part) => previous + part.charAt(0).toUpperCase() + part.slice(1), '');
|
||||
}
|
||||
|
||||
module.exports = class GeneratorGetTagName extends Generator {
|
||||
prompting() {
|
||||
const done = this.async();
|
||||
|
||||
return this.prompt(PROMPTS).then((answers) => {
|
||||
return this.prompt(PROMPTS).then(answers => {
|
||||
this.props = answers;
|
||||
this.props.className = getClassName(this.props.tagName);
|
||||
done();
|
||||
|
||||
@@ -17,9 +17,7 @@ function getIdentifier(arg) {
|
||||
}
|
||||
|
||||
if (isAttribute(arg)) {
|
||||
return arg.value
|
||||
? `attribute [${arg.name}="${arg.value}"]`
|
||||
: `attribute [${arg.name}]`;
|
||||
return arg.value ? `attribute [${arg.name}="${arg.value}"]` : `attribute [${arg.name}]`;
|
||||
}
|
||||
|
||||
throw new Error(`Unknown arg: ${arg}`);
|
||||
|
||||
@@ -8,30 +8,36 @@ function filterNode(node, ignoredTags) {
|
||||
}
|
||||
|
||||
function sortAttributes(attrs) {
|
||||
return attrs
|
||||
// Sort attributes
|
||||
.map((attr) => {
|
||||
if (attr.name === 'class') {
|
||||
attr.value = attr.value.trim().split(/\s+/).sort().join(' ');
|
||||
}
|
||||
return (
|
||||
attrs
|
||||
// Sort attributes
|
||||
.map(attr => {
|
||||
if (attr.name === 'class') {
|
||||
attr.value = attr.value
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.sort()
|
||||
.join(' ');
|
||||
}
|
||||
|
||||
return attr;
|
||||
})
|
||||
// Sort classes
|
||||
.sort((attrA, attrB) => {
|
||||
const a = attrA.name.toLowerCase();
|
||||
const b = attrB.name.toLowerCase();
|
||||
return attr;
|
||||
})
|
||||
// Sort classes
|
||||
.sort((attrA, attrB) => {
|
||||
const a = attrA.name.toLowerCase();
|
||||
const b = attrB.name.toLowerCase();
|
||||
|
||||
if (a < b) {
|
||||
return -1;
|
||||
}
|
||||
if (a < b) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (a > b) {
|
||||
return 1;
|
||||
}
|
||||
if (a > b) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
return 0;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
export function sanitizeHtmlString(htmlString) {
|
||||
return htmlString
|
||||
// Remove space characters after opening tags
|
||||
.replace(/>\s+/g, '>')
|
||||
// Remove space characters before closing tags
|
||||
.replace(/\s+</g, '<')
|
||||
// remove lit-html expression markers
|
||||
.replace(/<!---->/g, '')
|
||||
// Remove leading and trailing whitespace
|
||||
.trim();
|
||||
return (
|
||||
htmlString
|
||||
// Remove space characters after opening tags
|
||||
.replace(/>\s+/g, '>')
|
||||
// Remove space characters before closing tags
|
||||
.replace(/\s+</g, '<')
|
||||
// remove lit-html expression markers
|
||||
.replace(/<!---->/g, '')
|
||||
// Remove leading and trailing whitespace
|
||||
.trim()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -75,7 +75,10 @@ describe('getSemanticDomDiff()', () => {
|
||||
|
||||
describe('multiple diffs', () => {
|
||||
it('returns the first diff', () => {
|
||||
const diff = getSemanticDomDiff('<div>foo</div><div foo="bar"></div>', '<div>bar</div><span foo="baz"></span>');
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div>foo</div><div foo="bar"></div>',
|
||||
'<div>bar</div><span foo="baz"></span>',
|
||||
);
|
||||
|
||||
expect(diff.message).to.equal('tag <div> was changed to tag <span>');
|
||||
expect(diff.path).to.equal('div');
|
||||
@@ -188,13 +191,19 @@ describe('getSemanticDomDiff()', () => {
|
||||
|
||||
describe('ordering', () => {
|
||||
it('attributes order', () => {
|
||||
const diff = getSemanticDomDiff('<div a="1" b="2" c="3"></div>', '<div c="3" a="1" b="2"></div>');
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div a="1" b="2" c="3"></div>',
|
||||
'<div c="3" a="1" b="2"></div>',
|
||||
);
|
||||
|
||||
expect(diff).to.equal(null);
|
||||
});
|
||||
|
||||
it('class order', () => {
|
||||
const diff = getSemanticDomDiff('<div class="foo bar"></div>', '<div class="bar foo"></div>');
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div class="foo bar"></div>',
|
||||
'<div class="bar foo"></div>',
|
||||
);
|
||||
|
||||
expect(diff).to.equal(null);
|
||||
});
|
||||
@@ -208,13 +217,19 @@ describe('getSemanticDomDiff()', () => {
|
||||
});
|
||||
|
||||
it('trailing whitespace in class', () => {
|
||||
const diff = getSemanticDomDiff('<div class="foo bar "></div>', '<div class="foo bar "></div>');
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div class="foo bar "></div>',
|
||||
'<div class="foo bar "></div>',
|
||||
);
|
||||
|
||||
expect(diff).to.equal(null);
|
||||
});
|
||||
|
||||
it('whitespace between classes', () => {
|
||||
const diff = getSemanticDomDiff('<div class="foo bar "></div>', '<div class="foo bar"></div>');
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div class="foo bar "></div>',
|
||||
'<div class="foo bar"></div>',
|
||||
);
|
||||
|
||||
expect(diff).to.equal(null);
|
||||
});
|
||||
@@ -226,7 +241,10 @@ describe('getSemanticDomDiff()', () => {
|
||||
});
|
||||
|
||||
it('whitespace in between nodes', () => {
|
||||
const diff = getSemanticDomDiff('<div> </div> foo <div> </div>', '<div></div>foo<div></div>');
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div> </div> foo <div> </div>',
|
||||
'<div></div>foo<div></div>',
|
||||
);
|
||||
|
||||
expect(diff).to.equal(null);
|
||||
});
|
||||
@@ -246,7 +264,10 @@ describe('getSemanticDomDiff()', () => {
|
||||
});
|
||||
|
||||
it('tabs in between nodes', () => {
|
||||
const diff = getSemanticDomDiff('<div>\t<div></div>\t \t \t</div>', '<div><div></div></div>');
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div>\t<div></div>\t \t \t</div>',
|
||||
'<div><div></div></div>',
|
||||
);
|
||||
|
||||
expect(diff).to.equal(null);
|
||||
});
|
||||
@@ -266,7 +287,10 @@ describe('getSemanticDomDiff()', () => {
|
||||
});
|
||||
|
||||
it('newlines in between nodes', () => {
|
||||
const diff = getSemanticDomDiff('<div>\n<div></div>\n \n \n</div>', '<div><div></div></div>');
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div>\n<div></div>\n \n \n</div>',
|
||||
'<div><div></div></div>',
|
||||
);
|
||||
|
||||
expect(diff).to.equal(null);
|
||||
});
|
||||
@@ -286,25 +310,34 @@ describe('getSemanticDomDiff()', () => {
|
||||
});
|
||||
|
||||
it('styles', () => {
|
||||
const diff = getSemanticDomDiff('<div>foo<style> .foo { color: blue; } </style></div>', '<div>foo</div>');
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div>foo<style> .foo { color: blue; } </style></div>',
|
||||
'<div>foo</div>',
|
||||
);
|
||||
|
||||
expect(diff).to.equal(null);
|
||||
});
|
||||
|
||||
it('script', () => {
|
||||
const diff = getSemanticDomDiff('<div>foo<script>console.log("foo");</script></div>', '<div>foo</div>');
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div>foo<script>console.log("foo");</script></div>',
|
||||
'<div>foo</div>',
|
||||
);
|
||||
|
||||
expect(diff).to.equal(null);
|
||||
});
|
||||
|
||||
it('ignored tags', () => {
|
||||
const diff = getSemanticDomDiff('<div><span>foo</span></div>', '<div><span>bar</span></div>', { ignoredTags: ['span'] });
|
||||
const diff = getSemanticDomDiff(
|
||||
'<div><span>foo</span></div>',
|
||||
'<div><span>bar</span></div>',
|
||||
{ ignoredTags: ['span'] },
|
||||
);
|
||||
|
||||
expect(diff).to.equal(null);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('template string', () => {
|
||||
it('differently formatted', () => {
|
||||
const a = `
|
||||
@@ -338,12 +371,17 @@ bar
|
||||
|
||||
describe('diff tree', () => {
|
||||
it('returns the left and right side normalized trees', () => {
|
||||
const diff = getSemanticDomDiff(`
|
||||
const diff = getSemanticDomDiff(
|
||||
`
|
||||
<div id="foo"> <div> <div class="foo bar "> <div>
|
||||
</div> </div>
|
||||
`, '<span></span>');
|
||||
`,
|
||||
'<span></span>',
|
||||
);
|
||||
|
||||
expect(diff.normalizedLeftHTML).to.equal('<div id="foo"><div><div class="bar foo"><div></div></div></div></div>');
|
||||
expect(diff.normalizedLeftHTML).to.equal(
|
||||
'<div id="foo"><div><div class="bar foo"><div></div></div></div></div>',
|
||||
);
|
||||
expect(diff.normalizedRightHTML).to.equal('<span></span>');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@ import { cachedWrappers } from '../fixture.js';
|
||||
if (afterEach) {
|
||||
afterEach(() => {
|
||||
if (cachedWrappers) {
|
||||
cachedWrappers.forEach((wrapper) => {
|
||||
cachedWrappers.forEach(wrapper => {
|
||||
document.body.removeChild(wrapper);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
import { expect } from '@bundled-es-modules/chai';
|
||||
import {
|
||||
defineCE,
|
||||
oneEvent,
|
||||
triggerFocusFor,
|
||||
triggerBlurFor,
|
||||
fixture,
|
||||
} from '../index.js';
|
||||
import { defineCE, oneEvent, triggerFocusFor, triggerBlurFor, fixture } from '../index.js';
|
||||
|
||||
describe('Helpers', () => {
|
||||
it('provides defineCE() to register a unique new element', async () => {
|
||||
@@ -34,15 +28,21 @@ describe('Helpers', () => {
|
||||
});
|
||||
|
||||
it('provides triggerFocusFor(), triggerBlurFor() to await a focus/blur event (for IE)', async () => {
|
||||
const tag = defineCE(class FocusElement extends HTMLElement {
|
||||
connectedCallback() {
|
||||
this.focusCount = 0;
|
||||
this.blurCount = 0;
|
||||
this.inputElement = this.querySelector('input');
|
||||
this.inputElement.addEventListener('focus', () => { this.focusCount += 1; });
|
||||
this.inputElement.addEventListener('blur', () => { this.blurCount += 1; });
|
||||
}
|
||||
});
|
||||
const tag = defineCE(
|
||||
class FocusElement extends HTMLElement {
|
||||
connectedCallback() {
|
||||
this.focusCount = 0;
|
||||
this.blurCount = 0;
|
||||
this.inputElement = this.querySelector('input');
|
||||
this.inputElement.addEventListener('focus', () => {
|
||||
this.focusCount += 1;
|
||||
});
|
||||
this.inputElement.addEventListener('blur', () => {
|
||||
this.blurCount += 1;
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
const el = await fixture(`<${tag}><input></${tag}>`);
|
||||
expect(el.focusCount).to.equal(0);
|
||||
expect(el.blurCount).to.equal(0);
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import { expect } from '@bundled-es-modules/chai';
|
||||
import {
|
||||
html,
|
||||
litFixture,
|
||||
unsafeStatic,
|
||||
} from '../index.js';
|
||||
import { html, litFixture, unsafeStatic } from '../index.js';
|
||||
|
||||
describe('html', () => {
|
||||
it('renders dynamic tags', async () => {
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
import { expect } from '@bundled-es-modules/chai';
|
||||
import {
|
||||
html,
|
||||
fixture,
|
||||
fixtureSync,
|
||||
litFixture,
|
||||
litFixtureSync,
|
||||
} from '../index.js';
|
||||
import { html, fixture, fixtureSync, litFixture, litFixtureSync } from '../index.js';
|
||||
|
||||
class TestComponent extends HTMLElement {}
|
||||
customElements.define('test-component', TestComponent);
|
||||
@@ -18,11 +12,19 @@ describe('fixtureSync & litFixtureSync & fixture & litFixture', () => {
|
||||
}
|
||||
[
|
||||
fixtureSync('<test-component>Text content</test-component>'),
|
||||
litFixtureSync(html`<test-component>Text content</test-component>`),
|
||||
litFixtureSync(
|
||||
html`
|
||||
<test-component>Text content</test-component>
|
||||
`,
|
||||
),
|
||||
].forEach(testElement);
|
||||
(await Promise.all([
|
||||
fixture('<test-component>Text content</test-component>'),
|
||||
litFixture(html`<test-component>Text content</test-component>`),
|
||||
litFixture(
|
||||
html`
|
||||
<test-component>Text content</test-component>
|
||||
`,
|
||||
),
|
||||
])).forEach(testElement);
|
||||
});
|
||||
|
||||
@@ -33,11 +35,19 @@ describe('fixtureSync & litFixtureSync & fixture & litFixture', () => {
|
||||
}
|
||||
[
|
||||
fixtureSync('<test-component></test-component>'),
|
||||
litFixtureSync(html`<test-component></test-component>`),
|
||||
litFixtureSync(
|
||||
html`
|
||||
<test-component></test-component>
|
||||
`,
|
||||
),
|
||||
].forEach(testElement);
|
||||
(await Promise.all([
|
||||
fixture('<test-component></test-component>'),
|
||||
litFixture(html`<test-component></test-component>`),
|
||||
litFixture(
|
||||
html`
|
||||
<test-component></test-component>
|
||||
`,
|
||||
),
|
||||
])).forEach(testElement);
|
||||
});
|
||||
|
||||
@@ -50,14 +60,30 @@ describe('fixtureSync & litFixtureSync & fixture & litFixture', () => {
|
||||
[
|
||||
fixtureSync('<test-component></test-component>'),
|
||||
fixtureSync('<test-component></test-component>'),
|
||||
litFixtureSync(html`<test-component></test-component>`),
|
||||
litFixtureSync(html`<test-component></test-component>`),
|
||||
litFixtureSync(
|
||||
html`
|
||||
<test-component></test-component>
|
||||
`,
|
||||
),
|
||||
litFixtureSync(
|
||||
html`
|
||||
<test-component></test-component>
|
||||
`,
|
||||
),
|
||||
].forEach(testElement);
|
||||
(await Promise.all([
|
||||
fixture('<test-component></test-component>'),
|
||||
fixture('<test-component></test-component>'),
|
||||
litFixture(html`<test-component></test-component>`),
|
||||
litFixture(html`<test-component></test-component>`),
|
||||
litFixture(
|
||||
html`
|
||||
<test-component></test-component>
|
||||
`,
|
||||
),
|
||||
litFixture(
|
||||
html`
|
||||
<test-component></test-component>
|
||||
`,
|
||||
),
|
||||
])).forEach(testElement);
|
||||
});
|
||||
|
||||
@@ -67,11 +93,19 @@ describe('fixtureSync & litFixtureSync & fixture & litFixture', () => {
|
||||
}
|
||||
[
|
||||
fixtureSync('<test-component/>'),
|
||||
litFixtureSync(html`<test-component/>`),
|
||||
litFixtureSync(
|
||||
html`
|
||||
<test-component />
|
||||
`,
|
||||
),
|
||||
].forEach(testElement);
|
||||
(await Promise.all([
|
||||
fixture('<test-component/>'),
|
||||
litFixture(html`<test-component/>`),
|
||||
litFixture(
|
||||
html`
|
||||
<test-component />
|
||||
`,
|
||||
),
|
||||
])).forEach(testElement);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
/* eslint-disable no-unused-expressions */
|
||||
|
||||
import { cachedWrappers } from '@open-wc/testing-helpers/fixture.js';
|
||||
import {
|
||||
fixture,
|
||||
expect,
|
||||
} from '../index.js';
|
||||
import { fixture, expect } from '../index.js';
|
||||
|
||||
describe('BDD', () => {
|
||||
it('can use fixture', async () => {
|
||||
|
||||
@@ -11,12 +11,16 @@ const regex = /import\.meta/g;
|
||||
* return import.meta.url;
|
||||
* // becomes: return ({ url: `${window.location.protocol}//${window.location.host}/relative/path/to/file.js` }).url;
|
||||
*/
|
||||
module.exports = function (source) {
|
||||
module.exports = function(source) {
|
||||
const relativePath = this.context.substring(
|
||||
this.context.indexOf(this.rootContext) + this.rootContext.length + 1,
|
||||
this.resource.lastIndexOf('/') + 1
|
||||
this.resource.lastIndexOf('/') + 1,
|
||||
);
|
||||
|
||||
const fileName = this.resource.substring(this.resource.lastIndexOf('/') + 1);
|
||||
return source.replace(regex, () => `({ url: \`\${window.location.protocol}//\${window.location.host}/${relativePath}/${fileName}\` })`);
|
||||
return source.replace(
|
||||
regex,
|
||||
() =>
|
||||
`({ url: \`\${window.location.protocol}//\${window.location.host}/${relativePath}/${fileName}\` })`,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -6,24 +6,30 @@ import compiler from './compiler.js';
|
||||
|
||||
const { expect } = chai;
|
||||
|
||||
const rules = [{
|
||||
test: /\.js$/,
|
||||
loader: path.resolve(__dirname, '../loaders/import-meta-url-loader.js'),
|
||||
}];
|
||||
const rules = [
|
||||
{
|
||||
test: /\.js$/,
|
||||
loader: path.resolve(__dirname, '../loaders/import-meta-url-loader.js'),
|
||||
},
|
||||
];
|
||||
|
||||
describe('import-meta-url-loader', () => {
|
||||
it('Replaces all instances of import.meta', async () => {
|
||||
const stats = await compiler('caseA/index.js', rules);
|
||||
const caseA = stats.toJson().modules[0].source;
|
||||
|
||||
expect(caseA).to.equal(''
|
||||
+ "export const foo = new URL('./', ({ url: `${window.location.protocol}//${window.location.host}/caseA/index.js` }).url);\n"
|
||||
+ "export const bar = new URL('./', ({ url: `${window.location.protocol}//${window.location.host}/caseA/index.js` }).url);\n");
|
||||
expect(caseA).to.equal(
|
||||
'' +
|
||||
"export const foo = new URL('./', ({ url: `${window.location.protocol}//${window.location.host}/caseA/index.js` }).url);\n" +
|
||||
"export const bar = new URL('./', ({ url: `${window.location.protocol}//${window.location.host}/caseA/index.js` }).url);\n",
|
||||
);
|
||||
|
||||
const statsReturn = await compiler('caseA/return.js', rules);
|
||||
const caseAreturn = statsReturn.toJson().modules[0].source;
|
||||
// eslint-disable-next-line quotes
|
||||
expect(caseAreturn).to.equal("export const foo = () => ({ url: `${window.location.protocol}//${window.location.host}/caseA/return.js` });\n");
|
||||
expect(caseAreturn).to.equal(
|
||||
'export const foo = () => ({ url: `${window.location.protocol}//${window.location.host}/caseA/return.js` });\n',
|
||||
);
|
||||
});
|
||||
|
||||
it('Replaces nested instances of import.meta', async () => {
|
||||
@@ -31,11 +37,15 @@ describe('import-meta-url-loader', () => {
|
||||
const caseB = stats.toJson().modules[0].source;
|
||||
const caseBsub = stats.toJson().modules[1].source;
|
||||
|
||||
expect(caseB).to.equal(''
|
||||
+ "import './caseBsub/caseBsub';\n"
|
||||
+ '\n'
|
||||
+ "window.foo = new URL('./', ({ url: `${window.location.protocol}//${window.location.host}/caseB/index.js` }).url);\n");
|
||||
expect(caseB).to.equal(
|
||||
'' +
|
||||
"import './caseBsub/caseBsub';\n" +
|
||||
'\n' +
|
||||
"window.foo = new URL('./', ({ url: `${window.location.protocol}//${window.location.host}/caseB/index.js` }).url);\n",
|
||||
);
|
||||
|
||||
expect(caseBsub).to.equal("window.bar = new URL('./', ({ url: `${window.location.protocol}//${window.location.host}/caseB/caseBsub/caseBsub.js` }).url);\n");
|
||||
expect(caseBsub).to.equal(
|
||||
"window.bar = new URL('./', ({ url: `${window.location.protocol}//${window.location.host}/caseB/caseBsub/caseBsub.js` }).url);\n",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user