Skip to content

Commit 8e4e47f

Browse files
author
Dobromir Hristov
authored
Move the references setting back to the DocTopic component (#693)
resolves rdar://109856608 * refactor: move the references setting * fix: update references in store, if they update and add tests * refactor: add watchers to the references on top Component type pages
1 parent 2e579a4 commit 8e4e47f

File tree

8 files changed

+60
-62
lines changed

8 files changed

+60
-62
lines changed

src/components/Article.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ export default {
169169
this.store.reset();
170170
this.store.setReferences(this.references);
171171
},
172+
watch: {
173+
// update the references in the store, in case they update, but the component is not re-created
174+
references(references) {
175+
this.store.setReferences(references);
176+
},
177+
},
172178
SectionKind,
173179
};
174180
</script>

src/components/DocumentationTopic.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,13 @@ export default {
622622
}
623623
624624
this.store.reset();
625+
this.store.setReferences(this.references);
626+
},
627+
watch: {
628+
// update the references in the store, in case they update, but the component is not re-created
629+
references(references) {
630+
this.store.setReferences(references);
631+
},
625632
},
626633
};
627634
</script>

src/components/Navigator/QuickNavigationPreview.vue

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,6 @@ export default {
108108
};
109109
},
110110
},
111-
watch: {
112-
json: {
113-
immediate: true,
114-
async handler(json) {
115-
const { references = {} } = json || {};
116-
await this.$nextTick();
117-
PreviewStore.setReferences(references);
118-
},
119-
},
120-
},
121111
};
122112
</script>
123113

src/components/Tutorial.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ export default {
138138
this.store.reset();
139139
this.store.setReferences(this.references);
140140
},
141+
watch: {
142+
// update the references in the store, in case they update, but the component is not re-created
143+
references(references) {
144+
this.store.setReferences(references);
145+
},
146+
},
141147
mounted() {
142148
this.$bridge.on('codeColors', this.handleCodeColorsChange);
143149
this.$bridge.send({ type: 'requestCodeColors' });

src/components/TutorialsOverview.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ export default {
103103
this.store.reset();
104104
this.store.setReferences(this.references);
105105
},
106+
watch: {
107+
// update the references in the store, in case they update, but the component is not re-created
108+
references(references) {
109+
this.store.setReferences(references);
110+
},
111+
},
106112
};
107113
</script>
108114

src/views/DocumentationTopic.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ export default {
382382
this.$nextTick(() => {
383383
// Send a 'rendered' message to the host when new data has been patched onto the DOM.
384384
this.newContentMounted();
385-
this.store.setReferences(this.topicProps.references);
386385
});
387386
},
388387
},

tests/unit/components/DocumentationTopic.spec.js

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,18 @@ const propsData = {
160160
describe('DocumentationTopic', () => {
161161
/** @type {import('@vue/test-utils').Wrapper} */
162162
let wrapper;
163-
163+
const mockStore = {
164+
state: { onThisPageSections: [], references: {} },
165+
reset: jest.fn(),
166+
setReferences: jest.fn(),
167+
};
164168
beforeEach(() => {
169+
jest.clearAllMocks();
165170
wrapper = shallowMount(DocumentationTopic, {
166171
propsData,
167172
provide: {
168173
isTargetIDE: false,
169-
store: {
170-
state: { onThisPageSections: [], references: {} },
171-
reset: jest.fn(),
172-
},
174+
store: mockStore,
173175
},
174176
});
175177
});
@@ -232,10 +234,7 @@ describe('DocumentationTopic', () => {
232234
propsData,
233235
provide: {
234236
isTargetIDE: true,
235-
store: {
236-
state: { onThisPageSections: [], references: {} },
237-
reset: jest.fn(),
238-
},
237+
store: mockStore,
239238
},
240239
});
241240

@@ -316,6 +315,9 @@ describe('DocumentationTopic', () => {
316315
role: 'symbol',
317316
symbolKind: 'protocol',
318317
},
318+
provide: {
319+
store: mockStore,
320+
},
319321
});
320322
const hero = wrapper.find(DocumentationHero);
321323
expect(hero.props()).toEqual({
@@ -641,12 +643,7 @@ describe('DocumentationTopic', () => {
641643
it('renders a `LanguageSwitcher` if TargetIDE', () => {
642644
const provide = {
643645
isTargetIDE: true,
644-
store: {
645-
state: {
646-
references: {},
647-
},
648-
reset: jest.fn(),
649-
},
646+
store: mockStore,
650647
};
651648
wrapper = shallowMount(DocumentationTopic, { propsData, provide });
652649
const switcher = wrapper.find(LanguageSwitcher);
@@ -781,10 +778,7 @@ describe('DocumentationTopic', () => {
781778
SeeAlso: stubSection('see-also'),
782779
},
783780
provide: {
784-
store: {
785-
state: { onThisPageSections: [], references: {} },
786-
reset: jest.fn(),
787-
},
781+
store: mockStore,
788782
},
789783
});
790784
const sections = wrapper.findAll('.section-stub');
@@ -866,10 +860,7 @@ describe('DocumentationTopic', () => {
866860
'above-title': '<div class="above-title">Above Title Content</div>',
867861
},
868862
provide: {
869-
store: {
870-
state: { onThisPageSections: [], references: {} },
871-
reset: jest.fn(),
872-
},
863+
store: mockStore,
873864
},
874865
});
875866
expect(wrapper.find(DocumentationHero).contains('.above-title')).toBe(true);
@@ -885,10 +876,7 @@ describe('DocumentationTopic', () => {
885876
DocumentationHero,
886877
},
887878
provide: {
888-
store: {
889-
state: { onThisPageSections: [], references: {} },
890-
reset: jest.fn(),
891-
},
879+
store: mockStore,
892880
},
893881
});
894882
expect(wrapper.contains('.above-hero-content')).toBe(true);
@@ -937,29 +925,38 @@ describe('DocumentationTopic', () => {
937925
expect(wrapper.vm.disableMetadata).toBe(true);
938926
});
939927

928+
it('sets the references, when they update in the store', () => {
929+
expect(mockStore.setReferences).toHaveBeenCalledTimes(1);
930+
const newReferences = {
931+
foo: {},
932+
};
933+
wrapper.setProps({
934+
references: newReferences,
935+
});
936+
expect(mockStore.setReferences).toHaveBeenCalledTimes(2);
937+
expect(mockStore.setReferences).toHaveBeenCalledWith(newReferences);
938+
});
939+
940940
describe('lifecycle hooks', () => {
941941
it('calls `store.reset()`', () => {
942-
const store = {
943-
reset: jest.fn(),
944-
state: { onThisPageSections: [], apiChanges: null, references: {} },
945-
};
942+
jest.clearAllMocks();
946943
wrapper = shallowMount(DocumentationTopic, {
947944
propsData,
948-
provide: { store },
945+
provide: { store: mockStore },
949946
});
950-
expect(store.reset).toBeCalled();
947+
expect(mockStore.reset).toBeCalled();
948+
expect(mockStore.setReferences).toHaveBeenCalledTimes(1);
949+
expect(mockStore.setReferences).toHaveBeenCalledWith(propsData.references);
951950
});
952951

953952
it('routes to the objc variant of a page if that is the preferred language', async () => {
954953
const $route = { query: {} };
955954
const $router = { replace: jest.fn() };
956955
const store = {
957-
reset: () => {},
956+
...mockStore,
958957
state: {
959-
apiChanges: null,
960-
onThisPageSections: [],
958+
...mockStore.state,
961959
preferredLanguage: Language.objectiveC.key.url,
962-
references: {},
963960
},
964961
};
965962
wrapper = shallowMount(DocumentationTopic, {

tests/unit/components/Navigator/QuickNavigationPreview.spec.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import { shallowMount } from '@vue/test-utils';
1212
import DocumentationTopic from 'docc-render/components/DocumentationTopic.vue';
1313
import QuickNavigationPreview from 'docc-render/components/Navigator/QuickNavigationPreview.vue';
14-
import { flushPromises } from '../../../../test-utils';
1514

1615
const { PreviewState, PreviewStore } = QuickNavigationPreview.constants;
1716

@@ -53,7 +52,7 @@ describe('QuickNavigationPreview', () => {
5352
expect(topic.props('abstract')).toEqual(json.abstract);
5453
});
5554

56-
it('provides a new store, setting the references when the JSON changes', async () => {
55+
it('provides a PreviewStore', async () => {
5756
const json = {
5857
abstract: [{ type: 'text', text: 'a' }],
5958
identifier: {
@@ -76,18 +75,6 @@ describe('QuickNavigationPreview', () => {
7675
});
7776
// eslint-disable-next-line no-underscore-dangle
7877
expect(wrapper.vm._provided).toHaveProperty('store', PreviewStore);
79-
await flushPromises();
80-
expect(PreviewStore.state.references).toEqual(json.references);
81-
wrapper.setProps({
82-
json: {
83-
...json,
84-
references: {
85-
c: 'c',
86-
},
87-
},
88-
});
89-
await flushPromises();
90-
expect(PreviewStore.state.references).toEqual({ c: 'c' });
9178
});
9279

9380
it('renders a "Preview unavailable" message for errors', () => {

0 commit comments

Comments
 (0)