fontScale.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * fontScale 工具函数单元测试
  3. * 覆盖:微信系统字号读取 / 用户设置优先级 / 值域钳制 / storage 互斥 / mp-html tag-style 生成
  4. */
  5. import {
  6. readSystemFontScale,
  7. getUserFontScale,
  8. setUserFontScale,
  9. getFontScale,
  10. syncFontScale,
  11. buildContentTagStyle
  12. } from '@/utils/fontScale.js'
  13. // 每个用例前清空 mock storage,并给出可控的 getAppBaseInfo
  14. beforeEach(() => {
  15. global.uni._storage = {}
  16. global.uni.getAppBaseInfo = jest.fn(() => ({}))
  17. })
  18. describe('readSystemFontScale', () => {
  19. it('优先返回 fontSizeScaleFactor', () => {
  20. global.uni.getAppBaseInfo = jest.fn(() => ({ fontSizeScaleFactor: 1.25, fontSizeSetting: 20 }))
  21. expect(readSystemFontScale()).toBe(1.25)
  22. })
  23. it('无 factor 时用 fontSizeSetting / 16 兜底', () => {
  24. global.uni.getAppBaseInfo = jest.fn(() => ({ fontSizeSetting: 20 }))
  25. expect(readSystemFontScale()).toBeCloseTo(1.25, 5)
  26. })
  27. it('两者都缺省时返回 1', () => {
  28. expect(readSystemFontScale()).toBe(1)
  29. })
  30. it('API 抛异常时返回 1', () => {
  31. global.uni.getAppBaseInfo = jest.fn(() => { throw new Error('low lib') })
  32. expect(readSystemFontScale()).toBe(1)
  33. })
  34. })
  35. describe('setUserFontScale / getUserFontScale', () => {
  36. it('设置后能读回,且保留两位小数', () => {
  37. setUserFontScale(1.1234)
  38. expect(getUserFontScale()).toBe(1.12)
  39. })
  40. it('超出上界钳制到 1.7', () => {
  41. expect(setUserFontScale(5)).toBe(1.7)
  42. })
  43. it('低于下界钳制到 0.9', () => {
  44. expect(setUserFontScale(0.1)).toBe(0.9)
  45. })
  46. it('未设置时返回 null', () => {
  47. expect(getUserFontScale()).toBeNull()
  48. })
  49. it('非法值(0 / NaN)返回 null', () => {
  50. global.uni.setStorageSync('userFontScale', 0)
  51. expect(getUserFontScale()).toBeNull()
  52. global.uni.setStorageSync('userFontScale', NaN)
  53. expect(getUserFontScale()).toBeNull()
  54. })
  55. })
  56. describe('getFontScale 优先级', () => {
  57. it('用户手动设置优先于系统字号', () => {
  58. global.uni.setStorageSync('fontSizeScaleFactor', 1.5)
  59. setUserFontScale(1)
  60. expect(getFontScale()).toBe(1)
  61. })
  62. it('用户未设置时使用缓存中的系统值', () => {
  63. global.uni.setStorageSync('fontSizeScaleFactor', 1.3)
  64. expect(getFontScale()).toBe(1.3)
  65. })
  66. it('缓存也没有时实时读系统', () => {
  67. global.uni.getAppBaseInfo = jest.fn(() => ({ fontSizeScaleFactor: 1.4 }))
  68. expect(getFontScale()).toBe(1.4)
  69. })
  70. it('全部缺失时返回 1', () => {
  71. expect(getFontScale()).toBe(1)
  72. })
  73. })
  74. describe('syncFontScale', () => {
  75. it('用户未设置时把系统值写入缓存', () => {
  76. global.uni.getAppBaseInfo = jest.fn(() => ({ fontSizeScaleFactor: 1.6 }))
  77. syncFontScale()
  78. expect(global.uni._storage.fontSizeScaleFactor).toBe(1.6)
  79. })
  80. it('用户已设置时不覆盖缓存', () => {
  81. setUserFontScale(1.2)
  82. global.uni.setStorageSync('fontSizeScaleFactor', 1.6)
  83. global.uni.getAppBaseInfo = jest.fn(() => ({ fontSizeScaleFactor: 1.6 }))
  84. syncFontScale()
  85. expect(global.uni._storage.fontSizeScaleFactor).toBe(1.6)
  86. })
  87. })
  88. describe('buildContentTagStyle', () => {
  89. it('按 scale 放大正文字号(rpx)', () => {
  90. const style = buildContentTagStyle(32, 1.25)
  91. expect(style.p).toBe('font-size:40rpx;color:#444;line-height:1.8;')
  92. expect(style.div).toBe('font-size:40rpx;color:#444;line-height:1.8;')
  93. expect(style.section).toBe('font-size:40rpx;color:#444;line-height:1.8;')
  94. expect(style.li).toBe('font-size:40rpx;color:#444;line-height:1.8;')
  95. })
  96. it('scale 缺省按 1 处理', () => {
  97. expect(buildContentTagStyle(28).p).toBe('font-size:28rpx;color:#444;line-height:1.8;')
  98. })
  99. it('包含常用块级标签', () => {
  100. const keys = Object.keys(buildContentTagStyle(28, 1))
  101. expect(keys).toEqual(expect.arrayContaining(['p', 'div', 'section', 'li', 'blockquote']))
  102. })
  103. })