chat-font.service.spec.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { TestBed } from '@angular/core/testing';
  2. import { ChatFontService, CHAT_FONT_SIZES, DEFAULT_CHAT_FONT } from './chat-font.service';
  3. import { SessionService } from './session.service';
  4. describe('ChatFontService(会话字号偏好)', () => {
  5. let svc: ChatFontService;
  6. let session: SessionService;
  7. /*
  8. * effect() 由 Angular 的变更检测调度,测试里必须手动 flush 才会跑。
  9. * 不 flush 的话读到的永远是初始值(实测:默认 15 时读到的还是 CSS 兜底的 14)。
  10. */
  11. const flush = () => TestBed.flushEffects();
  12. beforeEach(() => {
  13. localStorage.clear();
  14. document.documentElement.style.removeProperty('--chat-read-size');
  15. TestBed.configureTestingModule({});
  16. svc = TestBed.inject(ChatFontService);
  17. session = TestBed.inject(SessionService);
  18. session.setSession('tok', { id: 'u1', name: '测试', companyId: 'c1' } as never);
  19. svc.load();
  20. });
  21. afterEach(() => {
  22. localStorage.clear();
  23. document.documentElement.style.removeProperty('--chat-read-size');
  24. });
  25. it('未保存过时为默认值', () => {
  26. expect(svc.size()).toBe(DEFAULT_CHAT_FONT);
  27. });
  28. it('设置后写进 DOM 变量(正文即时生效)', () => {
  29. svc.set(17);
  30. flush();
  31. expect(document.documentElement.style.getPropertyValue('--chat-read-size')).toBe('17px');
  32. });
  33. it('默认值也会写进 DOM 变量(不依赖 CSS 兜底)', () => {
  34. flush();
  35. expect(document.documentElement.style.getPropertyValue('--chat-read-size')).toBe(
  36. `${DEFAULT_CHAT_FONT}px`,
  37. );
  38. });
  39. it('非法值吸附到最近的合法档位', () => {
  40. svc.set(16.4);
  41. expect(svc.size()).toBe(16);
  42. svc.set(100);
  43. expect(svc.size()).toBe(CHAT_FONT_SIZES[CHAT_FONT_SIZES.length - 1]);
  44. svc.set(-5);
  45. expect(svc.size()).toBe(CHAT_FONT_SIZES[0]);
  46. });
  47. it('持久化:重载后保持', () => {
  48. svc.set(18);
  49. // 模拟刷新
  50. (svc as unknown as { loadedFor: string | null }).loadedFor = null;
  51. svc.load();
  52. expect(svc.size()).toBe(18);
  53. });
  54. it('按用户隔离', () => {
  55. svc.set(18);
  56. session.setSession('tok2', { id: 'u2', name: '乙', companyId: 'c1' } as never);
  57. (svc as unknown as { loadedFor: string | null }).loadedFor = null;
  58. svc.load();
  59. expect(svc.size()).toBe(DEFAULT_CHAT_FONT);
  60. });
  61. });