| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /**
- * fontScale 工具函数单元测试
- * 覆盖:微信系统字号读取 / 用户设置优先级 / 值域钳制 / storage 互斥 / mp-html tag-style 生成
- */
- import {
- readSystemFontScale,
- getUserFontScale,
- setUserFontScale,
- getFontScale,
- syncFontScale,
- buildContentTagStyle
- } from '@/utils/fontScale.js'
- // 每个用例前清空 mock storage,并给出可控的 getAppBaseInfo
- beforeEach(() => {
- global.uni._storage = {}
- global.uni.getAppBaseInfo = jest.fn(() => ({}))
- })
- describe('readSystemFontScale', () => {
- it('优先返回 fontSizeScaleFactor', () => {
- global.uni.getAppBaseInfo = jest.fn(() => ({ fontSizeScaleFactor: 1.25, fontSizeSetting: 20 }))
- expect(readSystemFontScale()).toBe(1.25)
- })
- it('无 factor 时用 fontSizeSetting / 16 兜底', () => {
- global.uni.getAppBaseInfo = jest.fn(() => ({ fontSizeSetting: 20 }))
- expect(readSystemFontScale()).toBeCloseTo(1.25, 5)
- })
- it('两者都缺省时返回 1', () => {
- expect(readSystemFontScale()).toBe(1)
- })
- it('API 抛异常时返回 1', () => {
- global.uni.getAppBaseInfo = jest.fn(() => { throw new Error('low lib') })
- expect(readSystemFontScale()).toBe(1)
- })
- })
- describe('setUserFontScale / getUserFontScale', () => {
- it('设置后能读回,且保留两位小数', () => {
- setUserFontScale(1.1234)
- expect(getUserFontScale()).toBe(1.12)
- })
- it('超出上界钳制到 1.7', () => {
- expect(setUserFontScale(5)).toBe(1.7)
- })
- it('低于下界钳制到 0.9', () => {
- expect(setUserFontScale(0.1)).toBe(0.9)
- })
- it('未设置时返回 null', () => {
- expect(getUserFontScale()).toBeNull()
- })
- it('非法值(0 / NaN)返回 null', () => {
- global.uni.setStorageSync('userFontScale', 0)
- expect(getUserFontScale()).toBeNull()
- global.uni.setStorageSync('userFontScale', NaN)
- expect(getUserFontScale()).toBeNull()
- })
- })
- describe('getFontScale 优先级', () => {
- it('用户手动设置优先于系统字号', () => {
- global.uni.setStorageSync('fontSizeScaleFactor', 1.5)
- setUserFontScale(1)
- expect(getFontScale()).toBe(1)
- })
- it('用户未设置时使用缓存中的系统值', () => {
- global.uni.setStorageSync('fontSizeScaleFactor', 1.3)
- expect(getFontScale()).toBe(1.3)
- })
- it('缓存也没有时实时读系统', () => {
- global.uni.getAppBaseInfo = jest.fn(() => ({ fontSizeScaleFactor: 1.4 }))
- expect(getFontScale()).toBe(1.4)
- })
- it('全部缺失时返回 1', () => {
- expect(getFontScale()).toBe(1)
- })
- })
- describe('syncFontScale', () => {
- it('用户未设置时把系统值写入缓存', () => {
- global.uni.getAppBaseInfo = jest.fn(() => ({ fontSizeScaleFactor: 1.6 }))
- syncFontScale()
- expect(global.uni._storage.fontSizeScaleFactor).toBe(1.6)
- })
- it('用户已设置时不覆盖缓存', () => {
- setUserFontScale(1.2)
- global.uni.setStorageSync('fontSizeScaleFactor', 1.6)
- global.uni.getAppBaseInfo = jest.fn(() => ({ fontSizeScaleFactor: 1.6 }))
- syncFontScale()
- expect(global.uni._storage.fontSizeScaleFactor).toBe(1.6)
- })
- })
- describe('buildContentTagStyle', () => {
- it('按 scale 放大正文字号(rpx)', () => {
- const style = buildContentTagStyle(32, 1.25)
- expect(style.p).toBe('font-size:40rpx;color:#444;line-height:1.8;')
- expect(style.div).toBe('font-size:40rpx;color:#444;line-height:1.8;')
- expect(style.section).toBe('font-size:40rpx;color:#444;line-height:1.8;')
- expect(style.li).toBe('font-size:40rpx;color:#444;line-height:1.8;')
- })
- it('scale 缺省按 1 处理', () => {
- expect(buildContentTagStyle(28).p).toBe('font-size:28rpx;color:#444;line-height:1.8;')
- })
- it('包含常用块级标签', () => {
- const keys = Object.keys(buildContentTagStyle(28, 1))
- expect(keys).toEqual(expect.arrayContaining(['p', 'div', 'section', 'li', 'blockquote']))
- })
- })
|