54 lines
2.5 KiB
TypeScript
54 lines
2.5 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import { setCookEventTracker } from '../apps/web/app/utils/analytics'
|
|
import { getRecipeFeedbackLink, getRecipeLink, getRecipeVideoLink, openRecipe } from '../apps/web/app/utils/recipe'
|
|
|
|
describe('getRecipeLink', () => {
|
|
it('records the click before leaving, and analytics failures never block navigation', () => {
|
|
const open = vi.spyOn(window, 'open').mockReturnValue(null)
|
|
const send = vi.fn(() => {
|
|
throw new Error('blocked')
|
|
})
|
|
setCookEventTracker(send)
|
|
openRecipe({ name: '番茄炒蛋', bv: 'BV1xx' })
|
|
expect(send).toHaveBeenCalledExactlyOnceWith('click_recipe_video')
|
|
expect(open).toHaveBeenCalledExactlyOnceWith('https://search.bilibili.com/all?keyword=%E7%95%AA%E8%8C%84%E7%82%92%E8%9B%8B', '_blank')
|
|
setCookEventTracker(() => {})
|
|
open.mockRestore()
|
|
})
|
|
|
|
it('does nothing for a recipe without a name or video', () => {
|
|
const open = vi.spyOn(window, 'open').mockReturnValue(null)
|
|
const send = vi.fn()
|
|
setCookEventTracker(send)
|
|
openRecipe({})
|
|
expect(send).not.toHaveBeenCalled()
|
|
expect(open).not.toHaveBeenCalled()
|
|
setCookEventTracker(() => {})
|
|
open.mockRestore()
|
|
})
|
|
|
|
it('searches bilibili by recipe name', () => {
|
|
expect(getRecipeLink({ name: '番茄炒蛋', bv: 'BV1xx' })).toBe('https://search.bilibili.com/all?keyword=%E7%95%AA%E8%8C%84%E7%82%92%E8%9B%8B')
|
|
expect(getRecipeLink({ name: '番茄炒蛋', link: 'https://example.com/recipe' })).toBe('https://search.bilibili.com/all?keyword=%E7%95%AA%E8%8C%84%E7%82%92%E8%9B%8B')
|
|
})
|
|
|
|
it('keeps the recorded video as a secondary link', () => {
|
|
expect(getRecipeVideoLink({ link: 'https://example.com/recipe', bv: 'BV1xx' })).toBe('https://example.com/recipe')
|
|
expect(getRecipeVideoLink({ link: '', bv: 'BV1xx' })).toBe('https://www.bilibili.com/video/BV1xx')
|
|
expect(getRecipeVideoLink({ link: '', bv: '' })).toBe('')
|
|
})
|
|
|
|
it('falls back to the bilibili video without a name', () => {
|
|
expect(getRecipeLink({ name: '', link: '', bv: 'BV1xx' })).toBe('https://www.bilibili.com/video/BV1xx')
|
|
})
|
|
})
|
|
|
|
describe('getRecipeFeedbackLink', () => {
|
|
it('prefills the recipe issue form', () => {
|
|
const url = new URL(getRecipeFeedbackLink({ name: '番茄炒蛋', bv: 'BV1xx' }))
|
|
expect(url.searchParams.get('template')).toBe('recipe.yml')
|
|
expect(url.searchParams.get('name')).toBe('番茄炒蛋')
|
|
expect(url.searchParams.get('video')).toBe('https://www.bilibili.com/video/BV1xx')
|
|
})
|
|
})
|