Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/Article.vue
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ export default {
},
},
created() {
AppStore.setAvailableLocales(this.metadata.availableLocales);
AppStore.setAvailableLocales(
this.metadata.availableLanguages ?? this.metadata.availableLocales,
);
this.store.reset();
this.store.setReferences(this.references);
},
Expand All @@ -177,7 +179,10 @@ export default {
this.store.setReferences(references);
},
'metadata.availableLocales': function availableLocalesWatcher(availableLocales) {
AppStore.setAvailableLocales(availableLocales);
AppStore.setAvailableLocales(this.metadata?.availableLanguages ?? availableLocales);
},
'metadata.availableLanguages': function availableLanguagesWatcher(availableLanguages) {
AppStore.setAvailableLocales(availableLanguages);
Comment on lines 179 to +185
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could refactor this code by adding a computed property:

availableLanguages: ({ metadata: { availableLanguages, availableLocales } = {} }) => (
  availableLanguages ?? availableLocales
),

and then just watch that computed property like:

watch: {
  availableLanguages(newValue) {
    AppStore.setAvailableLocales(newValue);
  },
},

instead of watching 'metadata.availableLocales' and 'metadata.availableLanguages'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the existing logic could be refactored and improved to be better and more consistent, but I was mostly focused on making the minimal changes to minimize the surface area of changes overall.

Thanks for the suggestion! It might be good to make those changes afterwards.

},
},
SectionKind,
Expand Down
13 changes: 11 additions & 2 deletions src/components/DocumentationTopic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ export default {
required: false,
validator: v => Object.prototype.hasOwnProperty.call(StandardColors, v),
},
availableLanguages: {
type: Array,
required: false,
},
availableLocales: {
type: Array,
required: false,
Expand Down Expand Up @@ -624,6 +628,7 @@ export default {
conformance,
hasNoExpandedDocumentation,
modules,
availableLanguages,
availableLocales,
platforms,
required: isRequirement = false,
Expand Down Expand Up @@ -666,6 +671,7 @@ export default {
downloadNotAvailableSummary,
diffAvailability,
hasNoExpandedDocumentation,
availableLanguages,
availableLocales,
hierarchy,
role,
Expand Down Expand Up @@ -717,7 +723,7 @@ export default {
});
}

AppStore.setAvailableLocales(this.availableLocales || []);
AppStore.setAvailableLocales(this.availableLanguages ?? this.availableLocales);
this.store.reset();
this.store.setReferences(this.references);
},
Expand All @@ -729,8 +735,11 @@ export default {
references(references) {
this.store.setReferences(references);
},
availableLanguages(availableLanguages) {
AppStore.setAvailableLocales(availableLanguages);
},
availableLocales(availableLocales) {
AppStore.setAvailableLocales(availableLocales);
AppStore.setAvailableLocales(this.availableLanguages ?? availableLocales);
Comment on lines +738 to +742
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar feedback:

We could make a computed property like

resolvedAvailableLanguages({ availableLanguages, availableLocales }) {
  return availableLanguages ?? availableLocales;
}

and then have a watcher like

watch: {
  resolvedAvailableLanguages(newValue) {
    AppStore.setAvailableLocales(newValue);
  },
},

on created(), we could also simplify it by:

AppStore.setAvailableLocales(this.resolvedAvailableLanguages);

},
},
};
Expand Down
9 changes: 7 additions & 2 deletions src/components/Tutorial.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ export default {
},
},
created() {
AppStore.setAvailableLocales(this.metadata.availableLocales);
AppStore.setAvailableLocales(
this.metadata.availableLanguages ?? this.metadata.availableLocales,
);
this.store.reset();
this.store.setReferences(this.references);
},
Expand All @@ -145,7 +147,10 @@ export default {
this.store.setReferences(references);
},
'metadata.availableLocales': function availableLocalesWatcher(availableLocales) {
AppStore.setAvailableLocales(availableLocales);
AppStore.setAvailableLocales(this.metadata?.availableLanguages ?? availableLocales);
},
'metadata.availableLanguages': function availableLanguagesWatcher(availableLanguages) {
AppStore.setAvailableLocales(availableLanguages);
Comment on lines 147 to +153
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same feedback about the computed property as before

},
},
mounted() {
Expand Down
9 changes: 7 additions & 2 deletions src/components/TutorialsOverview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ export default {
};
},
created() {
AppStore.setAvailableLocales(this.metadata.availableLocales);
AppStore.setAvailableLocales(
this.metadata.availableLanguages ?? this.metadata.availableLocales,
);
this.store.reset();
this.store.setReferences(this.references);
},
Expand All @@ -111,7 +113,10 @@ export default {
this.store.setReferences(references);
},
'metadata.availableLocales': function availableLocalesWatcher(availableLocales) {
AppStore.setAvailableLocales(availableLocales);
AppStore.setAvailableLocales(this.metadata?.availableLanguages ?? availableLocales);
},
'metadata.availableLanguages': function availableLanguagesWatcher(availableLanguages) {
AppStore.setAvailableLocales(availableLanguages);
Comment on lines 113 to +119
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same feedback about the computed property as before

},
},
};
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/components/Article.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Hero from 'docc-render/components/Article/Hero.vue';
import NavigationBar from 'docc-render/components/Tutorial/NavigationBar.vue';
import TopicStore from 'docc-render/stores/TopicStore';
import { PortalTarget } from 'portal-vue';
import AppStore from 'docc-render/stores/AppStore';

const { SectionKind } = Article;

Expand Down Expand Up @@ -199,6 +200,36 @@ describe('Article', () => {
});
expect(wrapper.text()).toContain('Above Hero Text');
});

it('sets available langs/locales', async () => {
const locales = ['en-US', 'ja-JP'];
const langs = ['en', 'jp'];

await wrapper.setProps({
metadata: {
...propsData.metadata,
availableLocales: locales,
},
});
expect(AppStore.state.availableLocales).toEqual(locales);

await wrapper.setProps({
metadata: {
...propsData.metadata,
availableLanguages: langs,
},
});
expect(AppStore.state.availableLocales).toEqual(langs);

await wrapper.setProps({
metadata: {
...propsData.metadata,
availableLanguages: langs,
availableLocales: locales,
},
});
expect(AppStore.state.availableLocales).toEqual(langs);
});
});

describe('with `isTargetIDE`', () => {
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/components/DocumentationTopic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import OnThisPageNav from '@/components/OnThisPageNav.vue';
import OnThisPageStickyContainer
from '@/components/DocumentationTopic/OnThisPageStickyContainer.vue';
import Declaration from '@/components/DocumentationTopic/PrimaryContent/Declaration.vue';
import AppStore from '@/stores/AppStore';

const { ON_THIS_PAGE_CONTAINER_BREAKPOINT } = DocumentationTopic.constants;

Expand Down Expand Up @@ -1175,6 +1176,23 @@ describe('DocumentationTopic', () => {
expect(mockStore.setReferences).toHaveBeenCalledWith(newReferences);
});

it('sets available languages/locales', async () => {
const locales = ['en-US', 'ja-JP'];
const langs = ['en', 'jp'];

await wrapper.setProps({ availableLocales: locales });
expect(AppStore.state.availableLocales).toEqual(locales);

await wrapper.setProps({ availableLanguages: langs });
expect(AppStore.state.availableLocales).toEqual(langs);

await wrapper.setProps({
availableLanguages: langs,
availableLocales: locales,
});
expect(AppStore.state.availableLocales).toEqual(langs);
});

it('calls `store.updateReferences` when `indexState.includedArchiveIdentifiers` changes', async () => {
const store = {
state: { references: {} },
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/components/Tutorial.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Tutorial from 'docc-render/components/Tutorial.vue';
import SectionList from 'docc-render/components/Tutorial/SectionList.vue';
import NavigationBar from 'docc-render/components/Tutorial/NavigationBar.vue';
import TopicStore from 'docc-render/stores/TopicStore';
import AppStore from 'docc-render/stores/AppStore';

const { Section, BreakpointEmitter, PortalTarget } = Tutorial.components;

Expand Down Expand Up @@ -239,6 +240,36 @@ describe('Tutorial', () => {
expect(target.exists()).toBe(true);
expect(target.props()).toHaveProperty('name', 'modal-destination');
});

it('sets available langs/locales', async () => {
const locales = ['en-US', 'ja-JP'];
const langs = ['en', 'jp'];

await wrapper.setProps({
metadata: {
...propsData.metadata,
availableLocales: locales,
},
});
expect(AppStore.state.availableLocales).toEqual(locales);

await wrapper.setProps({
metadata: {
...propsData.metadata,
availableLanguages: langs,
},
});
expect(AppStore.state.availableLocales).toEqual(langs);

await wrapper.setProps({
metadata: {
...propsData.metadata,
availableLanguages: langs,
availableLocales: locales,
},
});
expect(AppStore.state.availableLocales).toEqual(langs);
});
});

describe('Tutorial without hero section', () => {
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/components/TutorialsOverview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import { shallowMount } from '@vue/test-utils';
import TutorialsOverview from 'docc-render/components/TutorialsOverview.vue';
import AppStore from 'docc-render/stores/AppStore';

const {
Hero,
Expand Down Expand Up @@ -118,4 +119,34 @@ describe('TutorialsOverview', () => {
});
expect(wrapper.text()).toContain('Above Hero Content');
});

it('sets available langs/locales', async () => {
const locales = ['en-US', 'ja-JP'];
const langs = ['en', 'jp'];

await wrapper.setProps({
metadata: {
...propsData.metadata,
availableLocales: locales,
},
});
expect(AppStore.state.availableLocales).toEqual(locales);

await wrapper.setProps({
metadata: {
...propsData.metadata,
availableLanguages: langs,
},
});
expect(AppStore.state.availableLocales).toEqual(langs);

await wrapper.setProps({
metadata: {
...propsData.metadata,
availableLanguages: langs,
availableLocales: locales,
},
});
expect(AppStore.state.availableLocales).toEqual(langs);
});
});