Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/composables/useExternalLink.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { computed } from 'vue'

import { electronAPI, isElectron } from '@/utils/envUtil'
import { useI18n } from 'vue-i18n'
import { i18n } from '@/i18n'

/**
* Composable for building docs.comfy.org URLs with automatic locale and platform detection
Expand All @@ -23,7 +23,7 @@ import { useI18n } from 'vue-i18n'
* ```
*/
export function useExternalLink() {
const { locale } = useI18n()
const locale = computed(() => String(i18n.global.locale.value))

const isChinese = computed(() => {
return locale.value === 'zh' || locale.value === 'zh-TW'
Expand Down
38 changes: 20 additions & 18 deletions tests-ui/tests/composables/useExternalLink.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { ref } from 'vue'

import { useExternalLink } from '@/composables/useExternalLink'

// Mock the environment utilities
vi.mock('@/utils/envUtil', () => ({
isElectron: vi.fn(),
electronAPI: vi.fn()
}))

// Mock vue-i18n
const mockLocale = ref('en')
vi.mock('vue-i18n', () => ({
useI18n: vi.fn(() => ({
locale: mockLocale
}))
// Provide a minimal i18n instance for the composable
const i18n = vi.hoisted(() => ({
global: {
locale: {
value: 'en'
}
}
}))
vi.mock('@/i18n', () => ({
i18n
}))

// Import after mocking to get the mocked versions
import { useExternalLink } from '@/composables/useExternalLink'
import { electronAPI, isElectron } from '@/utils/envUtil'

describe('useExternalLink', () => {
beforeEach(() => {
vi.clearAllMocks()
// Reset to default state
mockLocale.value = 'en'
i18n.global.locale.value = 'en'
vi.mocked(isElectron).mockReturnValue(false)
})

Expand Down Expand Up @@ -53,31 +55,31 @@ describe('useExternalLink', () => {

describe('buildDocsUrl', () => {
it('should build basic docs URL without locale', () => {
mockLocale.value = 'en'
i18n.global.locale.value = 'en'
const { buildDocsUrl } = useExternalLink()

const url = buildDocsUrl('/changelog')
expect(url).toBe('https://docs.comfy.org/changelog')
})

it('should build docs URL with Chinese (zh) locale when requested', () => {
mockLocale.value = 'zh'
i18n.global.locale.value = 'zh'
const { buildDocsUrl } = useExternalLink()

const url = buildDocsUrl('/changelog', { includeLocale: true })
expect(url).toBe('https://docs.comfy.org/zh-CN/changelog')
})

it('should build docs URL with Chinese (zh-TW) locale when requested', () => {
mockLocale.value = 'zh-TW'
i18n.global.locale.value = 'zh-TW'
const { buildDocsUrl } = useExternalLink()

const url = buildDocsUrl('/changelog', { includeLocale: true })
expect(url).toBe('https://docs.comfy.org/zh-CN/changelog')
})

it('should not include locale for English when requested', () => {
mockLocale.value = 'en'
i18n.global.locale.value = 'en'
const { buildDocsUrl } = useExternalLink()

const url = buildDocsUrl('/changelog', { includeLocale: true })
Expand All @@ -92,7 +94,7 @@ describe('useExternalLink', () => {
})

it('should add platform suffix when requested', () => {
mockLocale.value = 'en'
i18n.global.locale.value = 'en'
vi.mocked(isElectron).mockReturnValue(true)
vi.mocked(electronAPI).mockReturnValue({
getPlatform: () => 'darwin'
Expand All @@ -104,7 +106,7 @@ describe('useExternalLink', () => {
})

it('should add platform suffix with trailing slash', () => {
mockLocale.value = 'en'
i18n.global.locale.value = 'en'
vi.mocked(isElectron).mockReturnValue(true)
vi.mocked(electronAPI).mockReturnValue({
getPlatform: () => 'win32'
Expand All @@ -116,7 +118,7 @@ describe('useExternalLink', () => {
})

it('should combine locale and platform', () => {
mockLocale.value = 'zh'
i18n.global.locale.value = 'zh'
vi.mocked(isElectron).mockReturnValue(true)
vi.mocked(electronAPI).mockReturnValue({
getPlatform: () => 'darwin'
Expand All @@ -133,7 +135,7 @@ describe('useExternalLink', () => {
})

it('should not add platform when not desktop', () => {
mockLocale.value = 'en'
i18n.global.locale.value = 'en'
vi.mocked(isElectron).mockReturnValue(false)

const { buildDocsUrl } = useExternalLink()
Expand Down