Skip to content

Commit

Permalink
test: Simplify tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Oct 16, 2024
1 parent fe68b8e commit 1ace85d
Showing 1 changed file with 11 additions and 34 deletions.
45 changes: 11 additions & 34 deletions test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,15 +588,13 @@ describe('Router', () => {

const clickHandler = sinon.fake(e => e.preventDefault());

const Route = sinon.fake(
() => (
<div>
<a href="/app">Internal Link</a>
<a href="/app/deeper">Internal Deeper Link</a>
<a href="/site">External Link</a>
<a href="/site/deeper">External Deeper Link</a>
</div>
)
const Links = () => (
<>
<a href="/app">Internal Link</a>
<a href="/app/deeper">Internal Deeper Link</a>
<a href="/site">External Link</a>
<a href="/site/deeper">External Deeper Link</a>
</>
);

let pushState;
Expand All @@ -612,34 +610,28 @@ describe('Router', () => {
});

beforeEach(async () => {
Route.resetHistory();
clickHandler.resetHistory();
pushState.resetHistory();
});

it('should intercept clicks on links matching the `scope` props (string)', async () => {
render(
<LocationProvider scope="/app">
<Router>
<Route default />
</Router>
<Links />
<ShallowLocation />
</LocationProvider>,
scratch
);
Route.resetHistory();
await sleep(10);

for (const url of shouldIntercept) {
const el = scratch.querySelector(`a[href="${url}"]`);
el.click();
await sleep(1);
expect(loc).to.deep.include({ url });
expect(Route).to.have.been.calledOnce;
expect(pushState).to.have.been.calledWith(null, '', url);
expect(clickHandler).to.have.been.called;

Route.resetHistory();
pushState.resetHistory();
clickHandler.resetHistory();
}
Expand All @@ -648,25 +640,20 @@ describe('Router', () => {
it('should allow default browser navigation for links not matching the `limit` props (string)', async () => {
render(
<LocationProvider scope="app">
<Router>
<Route default />
</Router>
<Links />
<ShallowLocation />
</LocationProvider>,
scratch
);
Route.resetHistory();
await sleep(10);

for (const url of shouldNavigate) {
const el = scratch.querySelector(`a[href="${url}"]`);
el.click();
await sleep(1);
expect(Route).not.to.have.been.called;
expect(pushState).not.to.have.been.called;
expect(clickHandler).to.have.been.called;

Route.resetHistory();
pushState.resetHistory();
clickHandler.resetHistory();
}
Expand All @@ -675,26 +662,21 @@ describe('Router', () => {
it('should intercept clicks on links matching the `limit` props (regex)', async () => {
render(
<LocationProvider scope={/^\/app/}>
<Router>
<Route default />
</Router>
<Links />
<ShallowLocation />
</LocationProvider>,
scratch
);
Route.resetHistory();
await sleep(10);

for (const url of shouldIntercept) {
const el = scratch.querySelector(`a[href="${url}"]`);
el.click();
await sleep(1);
expect(loc).to.deep.include({ url });
expect(Route).to.have.been.calledOnce;
expect(pushState).to.have.been.calledWith(null, '', url);
expect(clickHandler).to.have.been.called;

Route.resetHistory();
pushState.resetHistory();
clickHandler.resetHistory();
}
Expand All @@ -703,25 +685,20 @@ describe('Router', () => {
it('should allow default browser navigation for links not matching the `limit` props (regex)', async () => {
render(
<LocationProvider scope={/^\/app/}>
<Router>
<Route default />
</Router>
<Links />
<ShallowLocation />
</LocationProvider>,
scratch
);
Route.resetHistory();
await sleep(10);

for (const url of shouldNavigate) {
const el = scratch.querySelector(`a[href="${url}"]`);
el.click();
await sleep(1);
expect(Route).not.to.have.been.called;
expect(pushState).not.to.have.been.called;
expect(clickHandler).to.have.been.called;

Route.resetHistory();
pushState.resetHistory();
clickHandler.resetHistory();
}
Expand Down

0 comments on commit 1ace85d

Please sign in to comment.