Skip to content

Commit 9944c07

Browse files
author
Dobromir Hristov
committed
refactor: cleanup the TopicsGrid implementation and merge with TopicsTable
1 parent 9fe3aca commit 9944c07

File tree

8 files changed

+57
-184
lines changed

8 files changed

+57
-184
lines changed

src/components/DocumentationTopic/Topics.vue

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,21 @@
1010

1111
<template>
1212
<TopicsTable
13-
v-if="shouldRenderList"
1413
anchor="topics"
1514
title="Topics"
1615
:isSymbolDeprecated="isSymbolDeprecated"
1716
:isSymbolBeta="isSymbolBeta"
1817
:sections="sections"
19-
/>
20-
<TopicsGrid
21-
v-else
22-
:sections="sections"
2318
:topicStyle="topicStyle"
2419
/>
2520
</template>
2621

2722
<script>
28-
import { TopicStyles } from 'docc-render/constants/TopicStyles';
29-
import TopicsGrid from './TopicsGrid.vue';
3023
import TopicsTable from './TopicsTable.vue';
3124
3225
export default {
3326
name: 'Topics',
34-
components: { TopicsGrid, TopicsTable },
27+
components: { TopicsTable },
3528
props: {
3629
isSymbolDeprecated: Boolean,
3730
isSymbolBeta: Boolean,
@@ -41,8 +34,5 @@ export default {
4134
required: true,
4235
},
4336
},
44-
computed: {
45-
shouldRenderList: ({ topicStyle }) => topicStyle === TopicStyles.list,
46-
},
4737
};
4838
</script>

src/components/DocumentationTopic/TopicsGrid.vue

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/components/DocumentationTopic/TopicsLinkCardGrid.vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ import TopicsLinkCardGridItem from './TopicsLinkCardGridItem.vue';
3030
export default {
3131
name: 'TopicsLinkCardGrid',
3232
components: { TopicsLinkCardGridItem, Column, Row },
33-
inject: ['references'],
3433
props: {
35-
identifiers: {
34+
items: {
3635
type: Array,
3736
required: true,
3837
},
@@ -42,9 +41,6 @@ export default {
4241
},
4342
},
4443
computed: {
45-
items() {
46-
return this.identifiers.map(i => this.references[i]);
47-
},
4844
compactCards: ({ topicStyle }) => topicStyle === TopicStyles.compactGrid,
4945
},
5046
};

src/components/DocumentationTopic/TopicsTable.vue

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,31 @@
2727
<template v-if="section.discussion" slot="discussion">
2828
<ContentNode :content="section.discussion.content" />
2929
</template>
30-
<TopicsLinkBlock
31-
v-for="topic in section.topics"
30+
<TopicsLinkCardGrid
31+
v-if="!shouldRenderList"
32+
:items="section.topics"
33+
:topicStyle="topicStyle"
3234
class="topic"
33-
:key="topic.identifier"
34-
:topic="topic"
35-
:isSymbolDeprecated="isSymbolDeprecated"
36-
:isSymbolBeta="isSymbolBeta"
3735
/>
36+
<template v-else>
37+
<TopicsLinkBlock
38+
v-for="topic in section.topics"
39+
class="topic"
40+
:key="topic.identifier"
41+
:topic="topic"
42+
:isSymbolDeprecated="isSymbolDeprecated"
43+
:isSymbolBeta="isSymbolBeta"
44+
/>
45+
</template>
3846
</ContentTableSection>
3947
</ContentTable>
4048
</template>
4149

4250
<script>
4351
import ContentNode from 'docc-render/components/DocumentationTopic/ContentNode.vue';
4452
import WordBreak from 'docc-render/components/WordBreak.vue';
53+
import { TopicStyles } from 'docc-render/constants/TopicStyles';
54+
import TopicsLinkCardGrid from 'docc-render/components/DocumentationTopic/TopicsLinkCardGrid.vue';
4555
import LinkableHeading from 'docc-render/components/ContentNode/LinkableHeading.vue';
4656
import ContentTable from './ContentTable.vue';
4757
import ContentTableSection from './ContentTableSection.vue';
@@ -57,6 +67,7 @@ export default {
5767
},
5868
},
5969
components: {
70+
TopicsLinkCardGrid,
6071
WordBreak,
6172
ContentTable,
6273
TopicsLinkBlock,
@@ -89,8 +100,13 @@ export default {
89100
type: Boolean,
90101
default: false,
91102
},
103+
topicStyle: {
104+
type: String,
105+
default: TopicStyles.list,
106+
},
92107
},
93108
computed: {
109+
shouldRenderList: ({ topicStyle }) => topicStyle === TopicStyles.list,
94110
sectionsWithTopics() {
95111
return this.sections.map(section => ({
96112
...section,

tests/unit/components/DocumentationTopic/Topics.spec.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
*
77
* See https://swift.org/LICENSE.txt for license information
88
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9-
*/
9+
*/
1010

1111
import { shallowMount } from '@vue/test-utils';
1212
import Topics from 'docc-render/components/DocumentationTopic/Topics.vue';
1313
import { TopicStyles } from '@/constants/TopicStyles';
14-
import TopicsGrid from '@/components/DocumentationTopic/TopicsGrid.vue';
1514

1615
const { TopicsTable } = Topics.components;
1716

@@ -33,26 +32,13 @@ describe('Topics', () => {
3332
sections: [],
3433
title: 'Topics',
3534
wrapTitle: false,
35+
topicStyle: TopicStyles.list,
3636
});
3737
table.setProps({ isSymbolDeprecated: true });
3838
expect(table.props('isSymbolDeprecated')).toBe(true);
3939
table.setProps({ isSymbolBeta: true });
4040
expect(table.props('isSymbolBeta')).toBe(true);
41-
});
42-
43-
it('renders a `TopicsGrid`, if the `topicStyle` is not `list`', () => {
44-
const wrapper = shallowMount(Topics, {
45-
propsData: {
46-
sections: [],
47-
topicStyle: TopicStyles.detailedGrid,
48-
},
49-
});
50-
expect(wrapper.find(TopicsTable).exists()).toBe(false);
51-
const grid = wrapper.find(TopicsGrid);
52-
expect(grid.exists()).toBe(true);
53-
expect(grid.props()).toEqual({
54-
sections: [],
55-
topicStyle: TopicStyles.detailedGrid,
56-
});
41+
table.setProps({ topicStyle: TopicStyles.compactGrid });
42+
expect(table.props('topicStyle')).toBe(TopicStyles.compactGrid);
5743
});
5844
});

tests/unit/components/DocumentationTopic/TopicsGrid.spec.js

Lines changed: 0 additions & 78 deletions
This file was deleted.

tests/unit/components/DocumentationTopic/TopicsLinkCardGrid.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* See https://swift.org/LICENSE.txt for license information
88
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9-
*/
9+
*/
1010

1111
import TopicsLinkCardGrid from '@/components/DocumentationTopic/TopicsLinkCardGrid.vue';
1212
import { shallowMount } from '@vue/test-utils';
@@ -19,16 +19,14 @@ const ref1 = { identifier: 'ref1' };
1919
const ref2 = { identifier: 'ref2' };
2020

2121
const defaultProps = {
22-
identifiers: [ref1.identifier, ref2.identifier],
22+
items: [ref1, ref2],
2323
};
24-
const references = { ref1, ref2 };
2524

2625
const createWrapper = ({ propsData, ...others } = {}) => shallowMount(TopicsLinkCardGrid, {
2726
propsData: {
2827
...defaultProps,
2928
...propsData,
3029
},
31-
provide: { references },
3230
...others,
3331
});
3432

@@ -37,6 +35,7 @@ describe('TopicsLinkCardGrid', () => {
3735
const wrapper = createWrapper();
3836
expect(wrapper.find(Row).props()).toEqual({
3937
columns: 3, // compact grid is a 3 column setup
38+
gap: null,
4039
});
4140
const cols = wrapper.findAll(Column);
4241
expect(cols).toHaveLength(2);
@@ -55,6 +54,7 @@ describe('TopicsLinkCardGrid', () => {
5554
});
5655
expect(wrapper.find(Row).props()).toEqual({
5756
columns: 2, // detailed grid is a 2 column setup
57+
gap: null,
5858
});
5959
expect(wrapper.find(TopicsLinkCardGridItem).props('compact')).toBe(false);
6060
});

tests/unit/components/DocumentationTopic/TopicsTable.spec.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
*
77
* See https://swift.org/LICENSE.txt for license information
88
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9-
*/
9+
*/
1010

1111
import { shallowMount } from '@vue/test-utils';
1212
import TopicsTable from 'docc-render/components/DocumentationTopic/TopicsTable.vue';
13+
import { TopicStyles } from '@/constants/TopicStyles';
14+
import TopicsLinkCardGrid from '@/components/DocumentationTopic/TopicsLinkCardGrid.vue';
15+
import reference from '@/components/ContentNode/Reference';
1316

1417
const {
1518
ContentTable, TopicsLinkBlock, ContentTableSection, ContentNode, WordBreak, LinkableHeading,
@@ -85,6 +88,7 @@ describe('TopicsTable', () => {
8588

8689
it('renders a `TopicsLinkBlock` for each topic with reference data in a section', () => {
8790
const sections = wrapper.findAll(ContentTableSection);
91+
expect(wrapper.findAll(TopicsLinkCardGrid)).toHaveLength(0);
8892

8993
const firstSectionBlocks = sections.at(0).findAll(TopicsLinkBlock);
9094
expect(firstSectionBlocks.length).toBe(1);
@@ -105,6 +109,26 @@ describe('TopicsTable', () => {
105109
});
106110
});
107111

112+
it('renders a `TopicsLinkCardGrid` if `topicStyle` is not `list`', () => {
113+
wrapper.setProps({ topicStyle: TopicStyles.compactGrid });
114+
expect(wrapper.findAll(TopicsLinkBlock)).toHaveLength(0);
115+
const sections = wrapper.findAll(ContentTableSection);
116+
117+
const firstGrid = sections.at(0).find(TopicsLinkCardGrid);
118+
expect(firstGrid.classes('topic')).toBe(true);
119+
expect(firstGrid.props()).toEqual({
120+
topicStyle: TopicStyles.compactGrid,
121+
items: [foo],
122+
});
123+
124+
const secondGrid = sections.at(1).find(TopicsLinkCardGrid);
125+
expect(secondGrid.classes('topic')).toBe(true);
126+
expect(secondGrid.props()).toEqual({
127+
topicStyle: TopicStyles.compactGrid,
128+
items: [baz],
129+
});
130+
});
131+
108132
it('can be passed `title` and `anchor` overrides', () => {
109133
wrapper.setProps({
110134
anchor: 'foo-bar',

0 commit comments

Comments
 (0)