Browse Source

fix(common-ui): fetch api options after late binding

pull/8371/head
kilisamemarisaaa 4 weeks ago
parent
commit
f4f08614fc
  1. 5
      .changeset/calm-api-selects-load.md
  2. 26
      packages/effects/common-ui/src/components/api-component/__tests__/api-component.test.ts
  3. 6
      packages/effects/common-ui/src/components/api-component/api-component.vue

5
.changeset/calm-api-selects-load.md

@ -0,0 +1,5 @@
---
'@vben/common-ui': patch
---
fix(@vben/common-ui): fetch ApiComponent options when an API is provided after mount

26
packages/effects/common-ui/src/components/api-component/__tests__/api-component.test.ts

@ -1,3 +1,5 @@
import type { ApiComponentProps } from '../types';
import { mount } from '@vue/test-utils';
import { defineComponent, h, markRaw, nextTick, ref } from 'vue';
@ -128,4 +130,28 @@ describe('api-component.vue', () => {
expect(handleUpdate).toHaveBeenCalledWith('selected');
expect(input.props('modelValue')).toBe('selected');
});
it('fetches when the api is provided after mount', async () => {
const api = vi
.fn()
.mockResolvedValue([{ label: 'Loaded', value: 'loaded' }]);
const apiRef = ref<ApiComponentProps['api']>();
const Harness = defineComponent({
setup() {
return () =>
h(ApiComponent, {
api: apiRef.value,
component: markRaw(ModelValueInput),
});
},
});
mount(Harness);
expect(api).not.toHaveBeenCalled();
apiRef.value = api;
await nextTick();
expect(api).toHaveBeenCalledTimes(1);
});
});

6
packages/effects/common-ui/src/components/api-component/api-component.vue

@ -200,9 +200,9 @@ const mergedParams = computed(() => {
});
watch(
mergedParams,
(value, oldValue) => {
if (isEqual(value, oldValue)) {
[() => props.api, mergedParams],
([api, value], [oldApi, oldValue]) => {
if (api === oldApi && isEqual(value, oldValue)) {
return;
}
fetchApi();

Loading…
Cancel
Save