Skip to content

Commit 73f7914

Browse files
authored
Anchor links shouldn't overwrite query parameters (#914)
* Pass routers query to LinkableHeading For the Routerlink component to preserve the query, we need to pass the routers query as a query parameter to the "to" prop * Fix tests These tests were failing because they expect the router to have a query. By adding a mock we can fix that.
1 parent 01e5673 commit 73f7914

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

src/components/ContentNode/LinkableHeading.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
>
1616
<router-link
1717
v-if="shouldLink"
18-
:to="{ hash: `#${anchor}` }"
18+
:to="linkWithAnchor"
1919
:data-after-text="$t('accessibility.in-page-link')"
2020
class="header-anchor"
2121
@click="handleFocusAndScroll(anchor)"
@@ -64,6 +64,7 @@ export default {
6464
enableMinimized,
6565
isTargetIDE,
6666
}) => !!anchor && !enableMinimized && !isTargetIDE,
67+
linkWithAnchor: ({ anchor, $route }) => ({ hash: `#${anchor}`, query: $route.query }),
6768
},
6869
};
6970
</script>

tests/unit/components/ContentNode/LinkableHeading.spec.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@ describe('LinkableHeading', () => {
1717
});
1818
const stubs = { 'router-link': RouterLinkStub };
1919

20+
const mocks = {
21+
$route: {
22+
query: {
23+
context: 'foo',
24+
},
25+
},
26+
};
27+
2028
it('renders a default heading that is a h2 by default', () => {
2129
const wrapper = shallowMount(LinkableHeading, {
2230
stubs,
31+
mocks,
2332
slots: { default: 'Title' },
2433
});
2534
expect(wrapper.text()).toBe('Title');
@@ -29,6 +38,7 @@ describe('LinkableHeading', () => {
2938
it('renders a heading with a given level', () => {
3039
const wrapper = shallowMount(LinkableHeading, {
3140
stubs,
41+
mocks,
3242
propsData: {
3343
level: 3,
3444
},
@@ -39,6 +49,7 @@ describe('LinkableHeading', () => {
3949
it('renders a heading with a header anchor and an id on the wrapper', async () => {
4050
const wrapper = shallowMount(LinkableHeading, {
4151
stubs,
52+
mocks,
4253
propsData: {
4354
anchor: 'title',
4455
},
@@ -47,7 +58,7 @@ describe('LinkableHeading', () => {
4758
await wrapper.vm.$nextTick();
4859
expect(wrapper.attributes('id')).toBe('title');
4960
const headerAnchor = wrapper.find('.header-anchor');
50-
expect(headerAnchor.props('to')).toEqual({ hash: '#title' });
61+
expect(headerAnchor.props('to')).toEqual({ hash: '#title', query: mocks.$route.query });
5162
expect(headerAnchor.text()).toBe('Title');
5263
expect(headerAnchor.attributes('data-after-text')).toBe('accessibility.in-page-link');
5364
});
@@ -60,6 +71,7 @@ describe('LinkableHeading', () => {
6071
it('does not render anchor if target ide is true', () => {
6172
const wrapper = shallowMount(LinkableHeading, {
6273
stubs,
74+
mocks,
6375
propsData: {
6476
anchor: 'title',
6577
},
@@ -73,6 +85,7 @@ describe('LinkableHeading', () => {
7385
it('does not render anchor if `enableMinimized` is true', () => {
7486
const wrapper = shallowMount(LinkableHeading, {
7587
stubs,
88+
mocks,
7689
propsData: {
7790
anchor: 'title',
7891
},

tests/unit/components/DocumentationTopic/PrimaryContent/PropertyTable.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ const store = {
3434
},
3535
};
3636

37+
const mocks = {
38+
$route: {
39+
query: {
40+
context: 'foo',
41+
},
42+
},
43+
};
44+
3745
describe('PropertyTable', () => {
3846
const propsData = {
3947
kind: 'restResponses',
@@ -116,6 +124,7 @@ describe('PropertyTable', () => {
116124
return mount(PropertyTable, {
117125
stubs: ['ContentNode', 'router-link'],
118126
propsData,
127+
mocks,
119128
provide,
120129
...options,
121130
});

tests/unit/components/DocumentationTopic/PrimaryContent/RestParameters.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ describe('RestParameters', () => {
4545
},
4646
};
4747

48+
const mocks = {
49+
$route: {
50+
query: {
51+
context: 'foo',
52+
},
53+
},
54+
};
55+
4856
function mountComponent({ propsData: props, ...others } = {}) {
4957
return mount(RestParameters, {
5058
stubs: ['ContentNode', 'router-link'],
5159
propsData: {
5260
...propsData,
5361
...props,
5462
},
63+
mocks,
5564
provide,
5665
...others,
5766
});

tests/unit/components/DocumentationTopic/PrimaryContent/RestResponses.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,20 @@ describe('RestResponses', () => {
5454
},
5555
};
5656

57+
const mocks = {
58+
$route: {
59+
query: {
60+
context: 'foo',
61+
},
62+
},
63+
};
64+
5765
function mountComponent(options) {
5866
const wrapper = mount(RestResponses, {
5967
stubs: ['ContentNode', 'router-link'],
6068
propsData,
6169
provide,
70+
mocks,
6271
...options,
6372
});
6473
return wrapper;

0 commit comments

Comments
 (0)