Skip to content

Commit c5867d6

Browse files
committed
feat(testing-library__dom): monkey patched testing-library__dom mirror
1 parent 00aa940 commit c5867d6

File tree

9 files changed

+653
-48
lines changed

9 files changed

+653
-48
lines changed

packages/actionbar/test/actionbar.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import { fixture, elementUpdated, html, expect } from '@open-wc/testing';
1313

1414
import '../sp-actionbar.js';
1515
import { Actionbar } from '../';
16-
import { Default } from '../stories/actionbar.stories.js';
16+
import { Default } from '../stories/actionbar.stories';
17+
import { getByLabelText } from 'testing-library__dom';
1718

1819
describe('Actionbar', () => {
1920
it('loads', async () => {
@@ -24,6 +25,8 @@ describe('Actionbar', () => {
2425
expect(el).to.not.be.undefined;
2526

2627
await expect(el).to.be.accessible();
28+
const input = getByLabelText(el, 'Edit');
29+
expect(input).to.not.be.undefined;
2730
});
2831

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

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

+17-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import '../sp-sidenav.js';
1414
import '../sp-sidenav-item.js';
1515
import { SideNavItem } from '../';
1616
import { fixture, elementUpdated, html, expect } from '@open-wc/testing';
17+
import { getByText, queryByText } from 'testing-library__dom';
1718

1819
describe('Sidenav Item', () => {
1920
it('can exist disabled and with no parent', async () => {
@@ -69,29 +70,31 @@ describe('Sidenav Item', () => {
6970

7071
await elementUpdated(el);
7172

72-
expect(el.shadowRoot).to.exist;
73-
if (!el.shadowRoot) return;
74-
75-
let slot: HTMLSlotElement | null = el.shadowRoot.querySelector(
76-
'slot:not([name])'
77-
);
78-
expect(slot).not.to.exist;
73+
let section1 = queryByText(el, 'Section 1');
74+
let section2 = queryByText(el, 'Section 2');
7975

8076
expect(el.expanded).to.be.false;
77+
expect(section1, 'section 1: closed initial').to.be.null;
78+
expect(section2, 'section 2: closed initial').to.be.null;
8179

8280
el.click();
83-
8481
await elementUpdated(el);
8582

8683
expect(el.expanded).to.be.true;
84+
section1 = getByText(el, 'Section 1');
85+
section2 = getByText(el, 'Section 2');
86+
expect(section1, 'section 1: opened').to.not.be.null;
87+
expect(section2, 'section 2: opened').to.not.be.null;
8788

88-
slot = el.shadowRoot.querySelector(
89-
'slot:not([name])'
90-
) as HTMLSlotElement;
91-
expect(slot).to.exist;
92-
if (!slot) return;
89+
el.click();
90+
await elementUpdated(el);
91+
92+
section1 = queryByText(el, 'Section 1');
93+
section2 = queryByText(el, 'Section 2');
9394

94-
expect(slot.assignedElements().length).to.equal(2);
95+
expect(el.expanded).to.be.false;
96+
expect(section1, 'section 1: closed').to.be.null;
97+
expect(section2, 'section 2: closed').to.be.null;
9598
});
9699

97100
it('populated `aria-current`', async () => {
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dom.js

projects/testing-library__dom/README.md

Whitespace-only changes.
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// TypeScript Version: 2.8
2+
export * from '@testing-library/dom';
+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": "testing-library__dom",
3+
"version": "7.20.1",
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": "^7.20.1",
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,25 @@
1+
import resolve from 'rollup-plugin-node-resolve';
2+
import commonjs from 'rollup-plugin-commonjs';
3+
import replace from '@rollup/plugin-replace';
4+
5+
export default [
6+
{
7+
input: require.resolve(
8+
'@testing-library/dom/dist/@testing-library/dom.esm.js'
9+
),
10+
output: {
11+
file: './dom.js',
12+
format: 'es',
13+
},
14+
plugins: [
15+
resolve({
16+
browser: true,
17+
preferBuiltins: false,
18+
}),
19+
commonjs(),
20+
replace({
21+
querySelectorAll: 'querySelectorAllWithShadowDOM',
22+
}),
23+
],
24+
},
25+
];

0 commit comments

Comments
 (0)