Skip to content

Commit 86dac95

Browse files
committed
feat(testing-library-dom): add esm mirror of testing-library-dom
1 parent 7a66d06 commit 86dac95

File tree

10 files changed

+694
-30
lines changed

10 files changed

+694
-30
lines changed

karma.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = (config) => {
2828
babelConfig: {
2929
plugins: ['transform-node-env-inline'],
3030
},
31+
coverageExclude: ['projects/**/*'],
3132
},
3233
browsers: ['FirefoxHeadlessCustom'],
3334
customLaunchers: {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"prelerna-publish": "yarn get-ready && yarn custom-element-json",
6262
"lerna-publish": "lerna publish --message 'chore: release new versions'",
6363
"test": "yarn build && yarn test:ci",
64-
"test:build": "tsc --build test/tsconfig-test.json",
64+
"test:build": "tsc --build test/tsconfig-test.json && lerna exec --scope @bundled-es-modules/testing-library-dom -- yarn build",
6565
"test:ci": "yarn test:build && karma start --coverage",
6666
"test:start": "karma start",
6767
"test:watch": "run-p watch 'test:build -w' 'test:start --auto-watch=true --single-run=false'",

packages/actionbar/test/actionbar.test.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Actionbar } from '../';
1515
import { fixture, elementUpdated, html, expect } from '@open-wc/testing';
1616
import { waitForPredicate } from '../../../test/testing-helpers';
1717
import '../../shared/lib/focus-visible.js';
18+
import { getByLabelText } from '@bundled-es-modules/testing-library-dom';
1819

1920
describe('Actionbar', () => {
2021
it('loads', async () => {
@@ -23,7 +24,7 @@ describe('Actionbar', () => {
2324
<sp-actionbar open>
2425
<sp-checkbox indeterminate>228 Selected</sp-checkbox>
2526
<div class="spectrum-ButtonGroup">
26-
<sp-action-button quiet>
27+
<sp-action-button quiet label="Edit">
2728
<svg
2829
slot="icon"
2930
id="spectrum-icon-18-Edit"
@@ -54,8 +55,14 @@ describe('Actionbar', () => {
5455
await elementUpdated(el);
5556

5657
expect(el).to.not.be.undefined;
57-
expect(el).lightDom.to.equalSnapshot();
58-
expect(el).shadowDom.to.equalSnapshot();
58+
// expect(el).lightDom.to.equalSnapshot();
59+
// expect(el).shadowDom.to.equalSnapshot();
60+
61+
// const renderRoot = el.shadowRoot
62+
// ? (el.shadowRoot as unknown) as HTMLElement
63+
// : el as HTMLElement;
64+
const input = getByLabelText(el, 'Edit');
65+
expect(input).to.not.be.undefined;
5966
});
6067

6168
it('accepts variants', async () => {

packages/sidenav/test/sidenav-item.test.ts

+20-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ governing permissions and limitations under the License.
1313
import '../';
1414
import { SideNavItem } from '../';
1515
import { fixture, elementUpdated, html, expect } from '@open-wc/testing';
16+
import {
17+
getByText,
18+
queryByText,
19+
} from '@bundled-es-modules/testing-library-dom';
1620

1721
describe('Sidenav Item', () => {
1822
it('can exist disabled and with no parent', async () => {
@@ -68,24 +72,30 @@ describe('Sidenav Item', () => {
6872

6973
await elementUpdated(el);
7074

71-
expect(el.shadowRoot).to.exist;
72-
if (!el.shadowRoot) return;
73-
74-
let slot = el.shadowRoot.querySelector('slot');
75-
expect(slot).not.to.exist;
75+
let section1 = queryByText(el, 'Section 1');
76+
let section2 = queryByText(el, 'Section 2');
7677

7778
expect(el.expanded).to.be.false;
79+
expect(section1, 'section 1: closed initial').to.be.null;
80+
expect(section2, 'section 2: closed initial').to.be.null;
7881

7982
el.click();
80-
8183
await elementUpdated(el);
8284

8385
expect(el.expanded).to.be.true;
86+
section1 = getByText(el, 'Section 1');
87+
section2 = getByText(el, 'Section 2');
88+
expect(section1, 'section 1: opened').to.not.be.null;
89+
expect(section2, 'section 2: opened').to.not.be.null;
90+
91+
el.click();
92+
await elementUpdated(el);
8493

85-
slot = el.shadowRoot.querySelector('slot');
86-
expect(slot).to.exist;
87-
if (!slot) return;
94+
section1 = queryByText(el, 'Section 1');
95+
section2 = queryByText(el, 'Section 2');
8896

89-
expect(slot.assignedElements().length).to.equal(2);
97+
expect(el.expanded).to.be.false;
98+
expect(section1, 'section 1: closed').to.be.null;
99+
expect(section2, 'section 2: closed').to.be.null;
90100
});
91101
});
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dom.js
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TypeScript Version: 2.8
2+
import { getQueriesForElement } from '@testing-library/dom/typings/get-queries-for-element';
3+
import * as queries from '@testing-library/dom/typings/queries';
4+
import * as queryHelpers from '@testing-library/dom/typings/query-helpers';
5+
6+
declare const within: typeof getQueriesForElement;
7+
export { queries, queryHelpers, within };
8+
9+
export * from '@testing-library/dom/typings/queries';
10+
export * from '@testing-library/dom/typings/query-helpers';
11+
export * from '@testing-library/dom/typings/wait';
12+
export * from '@testing-library/dom/typings/wait-for-dom-change';
13+
export * from '@testing-library/dom/typings/wait-for-element';
14+
export * from '@testing-library/dom/typings/wait-for-element-to-be-removed';
15+
export * from '@testing-library/dom/typings/matches';
16+
export * from '@testing-library/dom/typings/get-node-text';
17+
export * from '@testing-library/dom/typings/events';
18+
export * from '@testing-library/dom/typings/get-queries-for-element';
19+
export * from '@testing-library/dom/typings/pretty-dom';
20+
export * from '@testing-library/dom/typings/config';

projects/testing-library-dom/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HTMLElement.prototype.querySelectorAllWithShadowDOM = function(query) {
2+
const isNotCustomElementParent = this.tagName.search('-') === -1;
3+
const children = [...(this.children || [])].filter(
4+
(child) => isNotCustomElementParent || !!child.assignedSlot
5+
);
6+
let results = children.filter((el) => el.matches(query));
7+
if (this.shadowRoot) {
8+
results = results.concat([
9+
...(this.shadowRoot.querySelectorAllWithShadowDOM(query) || []),
10+
]);
11+
}
12+
children.map((child) => {
13+
if (child.querySelectorAllWithShadowDOM) {
14+
results = results.concat([
15+
...(child.querySelectorAllWithShadowDOM(query) || []),
16+
]);
17+
}
18+
});
19+
return results;
20+
};
21+
22+
ShadowRoot.prototype.querySelectorAllWithShadowDOM = function(query) {
23+
const children = [...(this.children || [])];
24+
let results = children.filter((el) => el.matches(query));
25+
children.map((child) => {
26+
results = results.concat([
27+
...(child.querySelectorAllWithShadowDOM(query) || []),
28+
]);
29+
});
30+
return results;
31+
};
32+
33+
export * from './dom.js';
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@bundled-es-modules/testing-library-dom",
3+
"version": "5.0.0-alpha1",
4+
"description": "mirror of @testing-library/dom, bundled and exposed as ES module",
5+
"author": "",
6+
"main": "index.js",
7+
"publishConfig": {
8+
"access": "public"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git://github.com/bundled-es-modules/testing-library-dom.git"
13+
},
14+
"license": "Apache-2.0",
15+
"devDependencies": {
16+
"@rollup/plugin-alias": "^2.2.0",
17+
"@rollup/plugin-replace": "^2.2.1",
18+
"@testing-library/dom": "^5.0.0",
19+
"babel-core": "^6.26.3",
20+
"core-js": "^2.5.7",
21+
"rollup": "^1.0.0",
22+
"rollup-plugin-commonjs": "^9.0.0",
23+
"rollup-plugin-copy": "^3.0.0",
24+
"rollup-plugin-ignore": "^1.0.5",
25+
"rollup-plugin-node-builtins": "^2.1.2",
26+
"rollup-plugin-node-globals": "^1.4.0",
27+
"rollup-plugin-node-resolve": "^4.0.0",
28+
"rollup-plugin-replace": "^2.1.0",
29+
"semver": ">=4.3.2"
30+
},
31+
"scripts": {
32+
"build": "rollup -c ./rollup.config.js"
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import resolve from 'rollup-plugin-node-resolve';
2+
import commonjs from 'rollup-plugin-commonjs';
3+
import alias from '@rollup/plugin-alias';
4+
import replace from '@rollup/plugin-replace';
5+
6+
export default [
7+
{
8+
input: require.resolve(
9+
'@sheerun/mutationobserver-shim/MutationObserver.js'
10+
),
11+
output: {
12+
file: './lib/mutationobserver-shim/index.js',
13+
format: 'es',
14+
},
15+
plugins: [
16+
resolve({
17+
browser: true,
18+
preferBuiltins: false,
19+
}),
20+
commonjs(),
21+
],
22+
},
23+
{
24+
input: require.resolve(
25+
'@testing-library/dom/dist/@testing-library/dom.esm.js'
26+
),
27+
output: {
28+
file: './dom.js',
29+
format: 'es',
30+
},
31+
plugins: [
32+
alias({
33+
entries: {
34+
'@sheerun/mutationobserver-shim':
35+
'lib/mutationobserver-shim/index.js',
36+
},
37+
}),
38+
resolve({
39+
browser: true,
40+
preferBuiltins: false,
41+
}),
42+
commonjs(),
43+
replace({
44+
querySelectorAll: 'querySelectorAllWithShadowDOM',
45+
'process.env.DEBUG_PRINT_LIMIT':
46+
"(typeof process !== 'undefined' && process.env.DEBUG_PRINT_LIMIT)",
47+
'htmlElement.outerHTML.length > maxLength':
48+
'(htmlElement.outerHTML && htmlElement.outerHTML.length > maxLength)',
49+
}),
50+
],
51+
},
52+
];

0 commit comments

Comments
 (0)