mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-11-13 22:11:42 +08:00
feat: 新增加载更多组件,完善数据加载状态管理
1. 新增uh-data-loadmore加载更多组件,适配下拉加载场景 2. 扩展DataLoadingStatus类型,新增no-network状态 3. 重构useDataLoadingStatus hooks,新增加载更多状态管理 4. 优化分类页面加载逻辑,替换旧的加载更多实现 5. 调整维护页、加载提示文案细节 6. 重构轮播详情页,统一使用数据加载状态机 7. 删除过时的useDataLoading测试文件
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { defineComponent, h } from 'vue'
|
||||
import { useDataLoading } from './useDataLoading'
|
||||
|
||||
/**
|
||||
* 在 Vue 应用上下文中运行 composable。
|
||||
* composable 的 ref/computed 只能在 setup() 内使用,
|
||||
* withSetup 通过挂载一个临时组件来提供这个上下文。
|
||||
*/
|
||||
function withSetup<T>(composableFn: () => T): T {
|
||||
let result!: T
|
||||
const Comp = defineComponent({
|
||||
setup() {
|
||||
result = composableFn()
|
||||
return () => h('div')
|
||||
},
|
||||
})
|
||||
const wrapper = mount(Comp)
|
||||
wrapper.unmount()
|
||||
return result
|
||||
}
|
||||
|
||||
describe('useDataLoading', () => {
|
||||
it('初始状态:status=loading, data=undefined', () => {
|
||||
const fetcher = vi.fn().mockResolvedValue('data')
|
||||
const { status, data } = withSetup(() => useDataLoading(fetcher))
|
||||
|
||||
expect(status.value).toBe('loading')
|
||||
expect(data.value).toBeUndefined()
|
||||
})
|
||||
|
||||
it('run 成功:status 从 loading 流转到 success,data 更新为返回值并触发 onSuccess', async () => {
|
||||
const fetcher = vi.fn().mockResolvedValue('result')
|
||||
const onSuccess = vi.fn()
|
||||
const { status, data, run } = withSetup(() => useDataLoading(fetcher, { onSuccess }))
|
||||
|
||||
const promise = run()
|
||||
expect(status.value).toBe('loading')
|
||||
|
||||
await promise
|
||||
expect(status.value).toBe('success')
|
||||
expect(data.value).toBe('result')
|
||||
expect(onSuccess).toHaveBeenCalledWith('result')
|
||||
})
|
||||
|
||||
it('run 返回空数组:status 流转到 empty', async () => {
|
||||
const fetcher = vi.fn().mockResolvedValue([])
|
||||
const { status, run } = withSetup(() => useDataLoading(fetcher))
|
||||
|
||||
await run()
|
||||
expect(status.value).toBe('empty')
|
||||
})
|
||||
|
||||
it('isEmpty 自定义判空:返回 true 时 status 为 empty', async () => {
|
||||
const fetcher = vi.fn(async (): Promise<{ total: number, items: number[] }> => ({ total: 0, items: [1] }))
|
||||
const { status, run } = withSetup(() => useDataLoading(fetcher, { isEmpty: data => data.total === 0 }))
|
||||
|
||||
await run()
|
||||
expect(status.value).toBe('empty')
|
||||
})
|
||||
|
||||
it('run 失败:不向外抛出异常,status 流转到 error 并触发 onError', async () => {
|
||||
const err = new Error('network error')
|
||||
const onError = vi.fn()
|
||||
const fetcher = vi.fn().mockRejectedValue(err)
|
||||
const { status, data, run } = withSetup(() => useDataLoading(fetcher, { onError }))
|
||||
|
||||
await expect(run()).resolves.toBeUndefined()
|
||||
|
||||
expect(status.value).toBe('error')
|
||||
expect(data.value).toBeUndefined()
|
||||
expect(onError).toHaveBeenCalledWith(err)
|
||||
})
|
||||
|
||||
it('带参数 run:参数透传给 fetcher', async () => {
|
||||
const fetcher = vi.fn(async (name: string) => `hi ${name}`)
|
||||
const { data, run } = withSetup(() => useDataLoading(fetcher))
|
||||
|
||||
await run('world')
|
||||
expect(fetcher).toHaveBeenCalledWith('world')
|
||||
expect(data.value).toBe('hi world')
|
||||
})
|
||||
})
|
||||
@@ -2,7 +2,7 @@ import type { Ref } from 'vue'
|
||||
import { ref } from 'vue'
|
||||
|
||||
/** 数据加载四态(取值与旧 DataLoadingStatusEnum 一致,二者可互相兼容) */
|
||||
export type DataLoadingStatus = 'loading' | 'error' | 'empty' | 'success'
|
||||
export type DataLoadingStatus = 'loading' | 'error' | 'empty' | 'success' |'no-network'
|
||||
|
||||
export interface IUseDataLoadingOptions<T> {
|
||||
/** 自定义判空;默认:数组看长度、对象看键数、空值视为空 */
|
||||
|
||||
@@ -1,27 +1,82 @@
|
||||
import type { DataLoadingStatus } from './useDataLoading'
|
||||
|
||||
export const DataLoadingStatusEnum = {
|
||||
Loading: 'loading',
|
||||
Error: 'error',
|
||||
Empty: 'empty',
|
||||
Success: 'success',
|
||||
NoNetwork: 'noNetwork',
|
||||
} as const
|
||||
Loading: 'loading',
|
||||
Error: 'error',
|
||||
Empty: 'empty',
|
||||
Success: 'success',
|
||||
NoNetwork: 'noNetwork'
|
||||
} as const;
|
||||
|
||||
export function useDataLoadingStatus(status?: DataLoadingStatus) {
|
||||
const loadingStatus = ref<DataLoadingStatus>(status ?? DataLoadingStatusEnum.Loading)
|
||||
export type DataLoadingStatus = (typeof DataLoadingStatusEnum)[keyof typeof DataLoadingStatusEnum];
|
||||
|
||||
function resetLoadingStatus() {
|
||||
loadingStatus.value = DataLoadingStatusEnum.Loading
|
||||
}
|
||||
const LoadMoreStatusText = {
|
||||
loadMore: '哎嘿,往下还有数据哦~',
|
||||
loading: '稍等,正在加载中哦~',
|
||||
noMore: '呜呜,没有更多数据了~',
|
||||
error: '呜呜,加载失败了哦~',
|
||||
noNetwork: '呜呜,网络好像有点问题哦~'
|
||||
} as const;
|
||||
|
||||
function updateLoadingStatus(newStatus: DataLoadingStatus) {
|
||||
loadingStatus.value = newStatus
|
||||
}
|
||||
export type LoadMoreStatus = keyof typeof LoadMoreStatusText
|
||||
|
||||
return {
|
||||
loadingStatus,
|
||||
resetLoadingStatus,
|
||||
updateLoadingStatus,
|
||||
}
|
||||
export interface LoadMoreStatusOption {
|
||||
// 是否激活加载
|
||||
active?: boolean;
|
||||
status?: LoadMoreStatus;
|
||||
text?: string;
|
||||
hasNext?: boolean;
|
||||
}
|
||||
|
||||
export function useDataLoadingStatus(defaultStatus?: DataLoadingStatus) {
|
||||
const loadingStatus = ref<DataLoadingStatus>(defaultStatus ?? DataLoadingStatusEnum.Loading);
|
||||
|
||||
const _loadMoreStatus = ref<LoadMoreStatus>({
|
||||
active: false,
|
||||
hasNext: false,
|
||||
status: 'loadMore',
|
||||
text: ''
|
||||
});
|
||||
|
||||
const loadMoreStatus = computed(() => {
|
||||
const result = {
|
||||
..._loadMoreStatus.value
|
||||
};
|
||||
if (!_loadMoreStatus.value.text) {
|
||||
result.text = LoadMoreStatusText[_loadMoreStatus.value.status];
|
||||
}
|
||||
console.log('会触发吗?', _loadMoreStatus.value);
|
||||
console.log('会触发吗 result', result);
|
||||
return result;
|
||||
});
|
||||
|
||||
function resetLoadMoreStatus() {
|
||||
_loadMoreStatus.value.active = false;
|
||||
_loadMoreStatus.value.hasNext = false;
|
||||
_loadMoreStatus.value.status = 'loadMore';
|
||||
_loadMoreStatus.value.text = '';
|
||||
}
|
||||
|
||||
function resetLoadingStatus() {
|
||||
loadingStatus.value = DataLoadingStatusEnum.Loading;
|
||||
resetLoadMoreStatus();
|
||||
}
|
||||
|
||||
function updateLoadingStatus(status: DataLoadingStatus) {
|
||||
loadingStatus.value = status;
|
||||
}
|
||||
|
||||
function updateLoadMoreStatus(status: LoadMoreStatusOption) {
|
||||
_loadMoreStatus.value.active = status.active;
|
||||
_loadMoreStatus.value.status = status.status;
|
||||
_loadMoreStatus.value.hasNext = status.hasNext;
|
||||
_loadMoreStatus.value.text = status.text;
|
||||
}
|
||||
|
||||
return {
|
||||
loadingStatus,
|
||||
loadMoreStatus,
|
||||
resetLoadingStatus,
|
||||
resetLoadMoreStatus,
|
||||
updateLoadingStatus,
|
||||
updateLoadMoreStatus
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user