|
|
@@ -0,0 +1,127 @@
|
|
|
+/**
|
|
|
+ * 运动类型统一 + 后台可配置:utils/exerciseTypes.js 契约测试
|
|
|
+ *
|
|
|
+ * 背景:growth/exercise-checkin.vue 与 health/exercise-index.vue 各自写死一套
|
|
|
+ * exerciseTypes,value 分歧(ball vs ball_game),且后台无法配置。
|
|
|
+ * 本测试规定公共模块行为:
|
|
|
+ * 1) 标准并集默认值(全局一套)
|
|
|
+ * 2) 解析后台 sys_config 的 JSON 字符串响应
|
|
|
+ * 3) 历史 value 映射(ball_game → ball),保证老记录 label 不丢
|
|
|
+ * 4) 拉取失败/空值时回退默认(离线可用)
|
|
|
+ * 5) 加载后写入缓存复用
|
|
|
+ */
|
|
|
+describe('exerciseTypes 公共模块', () => {
|
|
|
+ let mod
|
|
|
+
|
|
|
+ beforeAll(() => {
|
|
|
+ mod = require('@/utils/exerciseTypes')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('默认值为标准并集(8 项,含 ball/strength/gymnastics,不含 ball_game)', () => {
|
|
|
+ const defs = mod.DEFAULT_EXERCISE_TYPES
|
|
|
+ const values = defs.map((x) => x.value)
|
|
|
+ expect(values).toEqual([
|
|
|
+ 'running', 'swimming', 'cycling', 'jump_rope',
|
|
|
+ 'ball', 'strength', 'gymnastics', 'other'
|
|
|
+ ])
|
|
|
+ expect(values).not.toContain('ball_game')
|
|
|
+ defs.forEach((x) => expect(x.label).toBeTruthy())
|
|
|
+ })
|
|
|
+
|
|
|
+ test('normalizeExerciseTypes 解析 JSON 数组字符串(后台 sys_config 的 data 字段)', () => {
|
|
|
+ const raw = JSON.stringify([
|
|
|
+ { label: '跑步', value: 'running' },
|
|
|
+ { label: '瑜伽', value: 'yoga' }
|
|
|
+ ])
|
|
|
+ expect(mod.normalizeExerciseTypes(raw).map((x) => x.value)).toEqual(['running', 'yoga'])
|
|
|
+ })
|
|
|
+
|
|
|
+ test('normalizeExerciseTypes 接受已解析的数组', () => {
|
|
|
+ expect(mod.normalizeExerciseTypes([{ label: '骑行', value: 'cycling' }])[0].label).toBe('骑行')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('normalizeExerciseTypes 空值 / 非法 JSON / 空数组 → 回退默认', () => {
|
|
|
+ expect(mod.normalizeExerciseTypes(null).length).toBe(mod.DEFAULT_EXERCISE_TYPES.length)
|
|
|
+ expect(mod.normalizeExerciseTypes('').length).toBe(mod.DEFAULT_EXERCISE_TYPES.length)
|
|
|
+ expect(mod.normalizeExerciseTypes('{bad json').length).toBe(mod.DEFAULT_EXERCISE_TYPES.length)
|
|
|
+ expect(mod.normalizeExerciseTypes('[]').length).toBe(mod.DEFAULT_EXERCISE_TYPES.length)
|
|
|
+ expect(mod.normalizeExerciseTypes([{ label: 'x' }]).length).toBe(mod.DEFAULT_EXERCISE_TYPES.length) // 缺 value
|
|
|
+ })
|
|
|
+
|
|
|
+ test('normalizeExerciseTypes 过滤 item.value 为空的脏数据', () => {
|
|
|
+ const raw = JSON.stringify([
|
|
|
+ { label: '跑步', value: 'running' },
|
|
|
+ { label: '', value: ' ' }
|
|
|
+ ])
|
|
|
+ expect(mod.normalizeExerciseTypes(raw).map((x) => x.value)).toEqual(['running'])
|
|
|
+ })
|
|
|
+
|
|
|
+ test('历史 value 映射:ball_game 归一为 ball', () => {
|
|
|
+ expect(mod.normalizeExerciseValue('ball_game')).toBe('ball')
|
|
|
+ expect(mod.normalizeExerciseValue('ball')).toBe('ball')
|
|
|
+ expect(mod.normalizeExerciseValue('running')).toBe('running')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('resolveExerciseTypeLabel 用历史 value 也能取到 label', () => {
|
|
|
+ const types = mod.DEFAULT_EXERCISE_TYPES
|
|
|
+ expect(mod.resolveExerciseTypeLabel('ball_game', types)).toBe('球类')
|
|
|
+ expect(mod.resolveExerciseTypeLabel('ball', types)).toBe('球类')
|
|
|
+ expect(mod.resolveExerciseTypeLabel('strength', types)).toBe('力量训练')
|
|
|
+ expect(mod.resolveExerciseTypeLabel('unknown_x', types)).toBe('其他')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('extractFromPublicConfigResp:解包 {code:200,data:"<json>"}', () => {
|
|
|
+ const resp = { code: 200, data: JSON.stringify([{ label: '瑜伽', value: 'yoga' }]) }
|
|
|
+ expect(mod.extractFromPublicConfigResp(resp).map((x) => x.value)).toEqual(['yoga'])
|
|
|
+ })
|
|
|
+
|
|
|
+ test('extractFromPublicConfigResp:code != 200 / 无 data → 默认', () => {
|
|
|
+ expect(mod.extractFromPublicConfigResp({ code: 500, data: '[]' }).length)
|
|
|
+ .toBe(mod.DEFAULT_EXERCISE_TYPES.length)
|
|
|
+ expect(mod.extractFromPublicConfigResp(null).length)
|
|
|
+ .toBe(mod.DEFAULT_EXERCISE_TYPES.length)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('loadExerciseTypes:fetcher 成功 → 返回解析结果并写缓存', async () => {
|
|
|
+ const fetcher = jest.fn().mockResolvedValue({
|
|
|
+ code: 200,
|
|
|
+ data: JSON.stringify([{ label: '瑜伽', value: 'yoga' }])
|
|
|
+ })
|
|
|
+ const store = {}
|
|
|
+ const result = await mod.loadExerciseTypes(fetcher, store)
|
|
|
+ expect(result.map((x) => x.value)).toEqual(['yoga'])
|
|
|
+ expect(fetcher).toHaveBeenCalledWith('exercise_types')
|
|
|
+ expect(store[mod.CACHE_KEY]).toBeTruthy()
|
|
|
+ })
|
|
|
+
|
|
|
+ test('loadExerciseTypes:fetcher 失败 → 回退缓存/默认,不抛错', async () => {
|
|
|
+ const fetcher = jest.fn().mockRejectedValue(new Error('network'))
|
|
|
+ const result = await mod.loadExerciseTypes(fetcher, {})
|
|
|
+ expect(result.length).toBe(mod.DEFAULT_EXERCISE_TYPES.length)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('loadExerciseTypes:已有缓存且 fetcher 失败 → 用缓存', async () => {
|
|
|
+ const cached = JSON.stringify([{ label: '旧配置', value: 'legacy_cached' }])
|
|
|
+ const fetcher = jest.fn().mockRejectedValue(new Error('network'))
|
|
|
+ const result = await mod.loadExerciseTypes(fetcher, { [mod.CACHE_KEY]: cached })
|
|
|
+ expect(result.map((x) => x.value)).toEqual(['legacy_cached'])
|
|
|
+ })
|
|
|
+
|
|
|
+ test('loadExerciseTypes:store 支持 getItem/setItem 适配器(uni storage)', async () => {
|
|
|
+ const mem = {}
|
|
|
+ const store = {
|
|
|
+ getItem: (k) => mem[k],
|
|
|
+ setItem: (k, v) => { mem[k] = v }
|
|
|
+ }
|
|
|
+ const fetcher = jest.fn().mockResolvedValue({
|
|
|
+ code: 200, data: JSON.stringify([{ label: '瑜伽', value: 'yoga' }])
|
|
|
+ })
|
|
|
+ const result = await mod.loadExerciseTypes(fetcher, store)
|
|
|
+ expect(result[0].value).toBe('yoga')
|
|
|
+ expect(mem[mod.CACHE_KEY]).toBeTruthy()
|
|
|
+ // 第二次 fetcher 失败 → 从 getItem 缓存读回
|
|
|
+ const failFetcher = jest.fn().mockRejectedValue(new Error('offline'))
|
|
|
+ const result2 = await mod.loadExerciseTypes(failFetcher, store)
|
|
|
+ expect(result2[0].value).toBe('yoga')
|
|
|
+ })
|
|
|
+})
|