Skip to content

Commit

Permalink
feat(tests): enhance useLinks tests for locale-based link generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Asuka committed Dec 9, 2024
1 parent 4325baa commit da27459
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 15 deletions.
141 changes: 133 additions & 8 deletions packages/ui/src/composables/__test__/useLinks.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,138 @@
import { describe } from 'vitest'
import type { Lang } from 'mqttx'
import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import { defineComponent, h, nextTick } from 'vue'
import { createI18n } from 'vue-i18n'
import useLinks from '../useLinks'

describe('useLinks', () => {
// it('should initialize linksMap correctly', () => {
// const { MQTTXSite } = useLinks()
const i18n = createI18n({
locale: 'en',
messages: {
en: {},
zh: {},
ja: {},
hu: {},
tr: {},
},
})

// const expectedLinksMap = ref({
// homepage: 'https://mqttx.app',
// })
const setupI18n = (locale: Lang) => {
i18n.global.locale = locale
return nextTick()
}

// expect(MQTTXSite.value).toEqual(expectedLinksMap.value)
// })
const createTestComponent = () => {
return defineComponent({
setup() {
return useLinks()
},
render() {
return h('div')
},
})
}

it('should return correct MQTTXSite based on locale', async () => {
await setupI18n('zh')
const wrapper = mount(createTestComponent(), { global: { plugins: [i18n] } })
expect(wrapper.vm.MQTTXSite).toBe('https://mqttx.app/zh')

await setupI18n('en')
expect(wrapper.vm.MQTTXSite).toBe('https://mqttx.app')

await setupI18n('ja')
expect(wrapper.vm.MQTTXSite).toBe('https://mqttx.app/ja')
})

it('should return correct EMQSite based on locale', async () => {
await setupI18n('zh')
const wrapper = mount(createTestComponent(), { global: { plugins: [i18n] } })
expect(wrapper.vm.EMQSite).toBe('https://emqx.com/zh')

await setupI18n('ja')
expect(wrapper.vm.EMQSite).toBe('https://emqx.com/ja')

await setupI18n('hu')
expect(wrapper.vm.EMQSite).toBe('https://emqx.com/en')
})

it('should return correct forumSite based on locale', async () => {
await setupI18n('zh')
const wrapper = mount(createTestComponent(), { global: { plugins: [i18n] } })
expect(wrapper.vm.forumSite).toBe('https://askemq.com/c/tools/11')

await setupI18n('en')
expect(wrapper.vm.forumSite).toBe('https://github.com/emqx/MQTTX/discussions')
})

it('should return correct leftBarLogo', async () => {
await setupI18n('zh')
const wrapper = mount(createTestComponent(), { global: { plugins: [i18n] } })
expect(wrapper.vm.leftBarLogo).toBe('https://mqttx.app/zh?utm_source=mqttx&utm_medium=referral&utm_campaign=logo-to-homepage')

await setupI18n('en')
expect(wrapper.vm.leftBarLogo).toBe('https://mqttx.app?utm_source=mqttx&utm_medium=referral&utm_campaign=logo-to-homepage')
})

it('should return correct empty links', async () => {
await setupI18n('zh')
const wrapper = mount(createTestComponent(), { global: { plugins: [i18n] } })
expect(wrapper.vm.empty).toEqual({
EMQX: 'https://emqx.com/zh/products/emqx?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-to-emqx',
EMQXCloud: 'https://emqx.com/zh/cloud?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-to-cloud',
})

await setupI18n('en')
expect(wrapper.vm.empty).toEqual({
EMQX: 'https://emqx.com/en/products/emqx?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-to-emqx',
EMQXCloud: 'https://emqx.com/en/cloud?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-to-cloud',
})
})

it('should return correct about links', async () => {
await setupI18n('zh')
window.__APP_VERSION__ = '2.0.0'
const wrapper = mount(createTestComponent(), { global: { plugins: [i18n] } })
expect(wrapper.vm.about).toEqual({
releases: 'https://mqttx.app/zh/changelogs/v2.0.0?utm_source=mqttx&utm_medium=referral&utm_campaign=about-to-release',
faq: 'https://mqttx.app/zh/docs/faq?utm_source=mqttx&utm_medium=referral&utm_campaign=about-to-faq',
MQTTX: 'https://mqttx.app/zh?utm_source=mqttx&utm_medium=referral&utm_campaign=about-to-mqttx',
EMQ: 'https://emqx.com/zh?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-to-homepage',
EMQXCloud: 'https://emqx.com/zh/cloud?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-to-cloud',
})

await setupI18n('en')
expect(wrapper.vm.about).toEqual({
releases: 'https://mqttx.app/changelogs/v2.0.0?utm_source=mqttx&utm_medium=referral&utm_campaign=about-to-release',
faq: 'https://mqttx.app/docs/faq?utm_source=mqttx&utm_medium=referral&utm_campaign=about-to-faq',
MQTTX: 'https://mqttx.app?utm_source=mqttx&utm_medium=referral&utm_campaign=about-to-mqttx',
EMQ: 'https://emqx.com/en?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-to-homepage',
EMQXCloud: 'https://emqx.com/en/cloud?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-to-cloud',
})
})

it('should return correct help links', async () => {
await setupI18n('zh')
const wrapper = mount(createTestComponent(), { global: { plugins: [i18n] } })
expect(wrapper.vm.help).toEqual({
docs: 'https://mqttx.app/zh/docs?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-help-to-docs',
forum: 'https://askemq.com/c/tools/11?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-help-to-forum',
learnMQTT: 'https://emqx.com/zh/mqtt-guide?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-help-to-learn-mqtt',
publicMqttBroker: 'https://emqx.com/zh/mqtt/public-mqtt5-broker',
mqtt5: 'https://emqx.com/zh/mqtt/mqtt5',
blogUtm: '?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-help-to-blog',
})

await setupI18n('en')
const wrapperEn = mount(createTestComponent(), { global: { plugins: [i18n] } })
expect(wrapperEn.vm.help).toEqual({
docs: 'https://mqttx.app/docs?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-help-to-docs',
forum: 'https://github.com/emqx/MQTTX/discussions?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-help-to-forum',
learnMQTT: 'https://emqx.com/en/mqtt-guide?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-help-to-learn-mqtt',
publicMqttBroker: 'https://emqx.com/en/mqtt/public-mqtt5-broker',
mqtt5: 'https://emqx.com/en/mqtt/mqtt5',
blogUtm: '?utm_source=mqttx&utm_medium=referral&utm_campaign=mqttx-help-to-blog',
})
})
})
7 changes: 6 additions & 1 deletion packages/ui/src/composables/useLinks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
export default function useLinks() {
const { locale } = useI18n()

const MQTTXSite = computed(() => locale.value === 'zh' ? 'https://mqttx.app/zh' : 'https://mqttx.app')
const MQTTXSite = computed(() => {
if (['zh', 'ja'].includes(locale.value)) {
return `https://mqttx.app/${locale.value}`
}
return 'https://mqttx.app'
})

const EMQSite = computed(() => {
if (['en', 'zh', 'ja'].includes(locale.value)) {
Expand Down
6 changes: 0 additions & 6 deletions scripts/gen-version.js

This file was deleted.

0 comments on commit da27459

Please sign in to comment.