qazaq-legacy-foundation-web/src/components/contact-form-consent.test.ts

58 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { createElement } from 'react'
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it, vi } from 'vitest'
import { getDict } from '@/i18n'
import { ContactForm } from './ContactForm'
vi.mock('@/app/actions/inquiry', () => ({ submitInquiry: async () => ({ status: 'idle' }) }))
const render = (l: 'ru' | 'kk' | 'en', privacyLink: boolean) =>
renderToStaticMarkup(createElement(ContactForm, { t: getDict(l), locale: l, email: 'a@example.com', privacyLink }))
describe('Сценарий: фраза согласия в форме', () => {
it.each(['ru', 'kk', 'en'] as const)('язык %s: политики нет — без упоминания и ссылки', (l) => {
const html = render(l, false)
expect(html).toContain(getDict(l).form.consent)
expect(html).not.toContain('/privacy')
expect(html).not.toMatch(/политик|саясат|policy/i)
})
it.each(['ru', 'kk', 'en'] as const)('язык %s: политика есть — ссылка на неё', (l) => {
const html = render(l, true)
expect(html).toContain(`href="/${l}/privacy"`)
expect(html).toContain(getDict(l).form.consentPolicyLink)
})
})
const visibleText = (html: string) => html.replace(/<[^>]*>/g, '')
describe('QLFW-38: согласие на казахском', () => {
it('Политика опубликована: согласие от первого лица со ссылкой', () => {
const html = render('kk', true)
const label = html.match(/<label[^>]*><input[^>]*name="consent"[\s\S]*?<\/label>/)?.[0]
expect(label).toBeDefined()
expect(visibleText(label!)).toBe('Жеке деректерімді өңдеуге құпиялылық саясатына сәйкес келісемін.')
expect(label).toContain('href="/kk/privacy"')
expect(label).not.toContain('келісесіз')
})
it('Политика отсутствует: согласие от первого лица без ссылки', () => {
const html = render('kk', false)
expect(visibleText(html)).toContain('Жеке деректерімді өңдеуге келісемін.')
expect(html).not.toContain('/privacy')
})
})
describe('QLFW-73: подпись о политике под кнопкой отправки', () => {
it.each([
['ru', 'Отправляя обращение, вы соглашаетесь с политикой конфиденциальности.', 'политикой конфиденциальности'],
['kk', 'Өтінішті жіберу арқылы сіз құпиялылық саясатымен келісесіз.', 'құпиялылық саясатымен'],
['en', 'By sending a message, you agree to our Privacy Policy.', 'Privacy Policy'],
] as const)('Политика опубликована на языке %s', (locale, expected, expectedLinkText) => {
const html = render(locale, true)
const note = html.match(/<button[^>]*type="submit"[^>]*>[^<]*<\/button>(<p[^>]*>.*?<\/p>)/)?.[1]
expect(visibleText(note ?? '')).toBe(expected)
expect(note).toMatch(new RegExp(`<a[^>]*href="/${locale}/privacy"[^>]*>${expectedLinkText}</a>`))
expect(html).toMatch(/<input[^>]*required[^>]*name="consent"/)
})
})