useAnnouncements.test.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { describe, it, expect } from 'vitest';
  2. import { announcementText, isAllowedAnnouncementLink } from '../../hooks/useAnnouncements';
  3. import type { Announcement } from '../../api/client';
  4. const a = (texts: Announcement['texts']): Announcement => ({
  5. id: 'x', level: 'info', texts, link_url: null, published_at: null, expires_at: null, read: false,
  6. });
  7. describe('announcementText', () => {
  8. const texts = {
  9. en: { title: 'Hello', body: 'b' },
  10. de: { title: 'Hallo', body: 'b' },
  11. 'pt-BR': { title: 'Olá', body: 'b' },
  12. 'zh-CN': { title: '你好', body: 'b' },
  13. };
  14. it.each([
  15. ['de', 'Hallo'],
  16. ['pt-BR', 'Olá'],
  17. ['pt-br', 'Olá'],
  18. ['pt', 'Olá'],
  19. ['zh-TW', '你好'],
  20. ['fr', 'Hello'],
  21. ['en-US', 'Hello'],
  22. ])('%s -> %s', (lang, title) => {
  23. expect(announcementText(a(texts), lang).title).toBe(title);
  24. });
  25. });
  26. describe('isAllowedAnnouncementLink', () => {
  27. it.each([
  28. ['https://wiki.bambuddy.cool/x', true],
  29. ['https://bambuddy.cool', true],
  30. ['https://github.com/maziggy/bambuddy', true],
  31. ['http://github.com/', false],
  32. ['https://github.com.evil.example/', false],
  33. ['https://user:pw@github.com/', false],
  34. ['https://github.com:8443/', false],
  35. ['javascript:alert(1)', false],
  36. ['not a url', false],
  37. [null, false],
  38. ])('%s -> %s', (url, allowed) => {
  39. expect(isAllowedAnnouncementLink(url)).toBe(allowed);
  40. });
  41. });