Skip to content

Commit e346f5a

Browse files
committed
Use @web/dev-server-import-maps to mock query-selector-shadow-dom
1 parent 813d160 commit e346f5a

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@typescript-eslint/eslint-plugin": "^4.25.0",
4848
"@typescript-eslint/parser": "^4.25.0",
4949
"@web/dev-server-esbuild": "^0.2.12",
50+
"@web/dev-server-import-maps": "0.0.5",
5051
"@web/test-runner": "^0.13.5",
5152
"banner-cli": "^0.14.1",
5253
"dotenv": "^10.0.0",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { stub } from 'sinon';
2+
export const querySelectorDeep = stub();

tests/router-component-tests.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import sinon from 'sinon';
22
import { expect, fixture, html, nextFrame } from '@open-wc/testing';
33
// eslint-disable-next-line no-unused-vars
44
import { extractPathParams, RouterComponent } from '../src/router-component';
5+
import { querySelectorDeep as realQuerySelectorDeep } from 'query-selector-shadow-dom';
56

67
const origDocTitle = document.title;
78
const originalPathName = document.location.pathname;
89

910
describe('<router-component>', async () => {
1011
let consoleWarn;
12+
let querySelectorDeep: sinon.SinonStub;
1113
beforeEach(() => {
14+
querySelectorDeep = realQuerySelectorDeep as sinon.SinonStub;
1215
consoleWarn = sinon.stub(console, 'warn');
16+
querySelectorDeep.reset();
1317
});
1418

1519
afterEach(() => {
@@ -473,10 +477,9 @@ describe('<router-component>', async () => {
473477
});
474478

475479
it('scrolls to the element on the route that matches the id of the hash after popstate has been called', async () => {
480+
const hashedElement = document.createElement('div');
481+
querySelectorDeep.returns(hashedElement);
476482
window.history.pushState({}, document.title, '/page1#test');
477-
const hashedElement = router.querySelector(
478-
'first-page div[id="test"]'
479-
);
480483
const popstate = new PopStateEvent('popstate', { state: {} });
481484
const scrollIntoViewStub = sinon.spy(
482485
hashedElement,

web-test-runner.config.mjs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import { esbuildPlugin } from '@web/dev-server-esbuild';
2+
import { importMapsPlugin } from '@web/dev-server-import-maps';
23

34
export default {
4-
files: 'tests/**/*.ts',
5-
plugins: [esbuildPlugin({ ts: true, target: 'auto' })],
5+
files: 'tests/*.ts',
6+
plugins: [
7+
esbuildPlugin({ ts: true, target: 'auto' }),
8+
importMapsPlugin({
9+
inject: {
10+
importMap: {
11+
imports: {
12+
'query-selector-shadow-dom':
13+
'/tests/mocks/query-selector-shadow-dom.ts',
14+
},
15+
},
16+
},
17+
}),
18+
],
619
nodeResolve: true,
720
};

0 commit comments

Comments
 (0)