diff --git a/test/basic.test.ts b/test/basic.test.ts index d4039d31cd..510548ffb8 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -2619,6 +2619,70 @@ describe.skipIf(isWindows)('useAsyncData', () => { }) }) +describe('useAsyncData regression with immediate:false', () => { + it('shared data, pending', async () => { + const page = await createPage('/useAsyncData/shared') + await page.waitForLoadState('networkidle') + await page.waitForFunction( + () => window.useNuxtApp?.()._route.fullPath === '/useAsyncData/shared' + ) + + expect(await page.locator('#pageWithSharedAsyncData__data').innerText()).toContain('default data') + expect(await page.locator('#pageWithSharedAsyncData__pending').innerText()).toContain('true') + + expect(await page.locator('#componentWithSharedUseAsyncData__data').innerText()).toContain( + 'default data' + ) + expect(await page.locator('#componentWithSharedUseAsyncData__pending').innerText()).toContain( + 'true' + ) + + await page.click('#pageWithSharedAsyncData__execute') + + expect(await page.locator('#pageWithSharedAsyncData__data').innerText()).toContain('some data') + expect(await page.locator('#pageWithSharedAsyncData__pending').innerText()).toContain('false') + + expect(await page.locator('#componentWithSharedUseAsyncData__data').innerText()).toContain( + 'some data' + ) + expect(await page.locator('#componentWithSharedUseAsyncData__pending').innerText()).toContain( + 'false' + ) + }) + + it('watch', async () => { + const page = await createPage('/useAsyncData/shared') + await page.waitForLoadState('networkidle') + await page.waitForFunction( + () => window.useNuxtApp?.()._route.fullPath === '/useAsyncData/shared' + ) + + expect(await page.locator('#pageWithSharedAsyncData__data').innerText()).toContain('default data') + expect(await page.locator('#pageWithSharedAsyncData__pending').innerText()).toContain('true') + + expect(await page.locator('#componentWithSharedUseAsyncData__data').innerText()).toContain( + 'default data' + ) + expect(await page.locator('#componentWithSharedUseAsyncData__pending').innerText()).toContain( + 'true' + ) + + await page.click('#pageWithSharedAsyncData__changeQuery') + + expect(await page.locator('#pageWithSharedAsyncData__data').innerText()).toContain('some data') + expect(await page.locator('#pageWithSharedAsyncData__pending').innerText()).toContain('false') + + expect(await page.locator('#componentWithSharedUseAsyncData__data').innerText()).toContain( + 'some data' + ) + expect(await page.locator('#componentWithSharedUseAsyncData__pending').innerText()).toContain( + 'false' + ) + + await page.close() + }) +}) + describe.runIf(isDev())('component testing', () => { it('should work', async () => { const comp1 = await $fetchComponent('components/Counter.vue', { multiplier: 2 }) diff --git a/test/fixtures/basic/components/ComponentWithSharedUseAsyncData.vue b/test/fixtures/basic/components/ComponentWithSharedUseAsyncData.vue new file mode 100644 index 0000000000..e2bc99d72b --- /dev/null +++ b/test/fixtures/basic/components/ComponentWithSharedUseAsyncData.vue @@ -0,0 +1,15 @@ + + + diff --git a/test/fixtures/basic/composables/asyncDataTests.ts b/test/fixtures/basic/composables/asyncDataTests.ts index 07dd078ae8..c687fdc979 100644 --- a/test/fixtures/basic/composables/asyncDataTests.ts +++ b/test/fixtures/basic/composables/asyncDataTests.ts @@ -5,3 +5,23 @@ export const useSleep = () => useAsyncData('sleep', async () => { }) export const useCounter = () => useFetch('/api/useAsyncData/count') + +export const useSharedAsyncData = async () => { + const route = useRoute() + + const { data, execute, pending } = await useAsyncData( + 'sharedAsyncData', + () => Promise.resolve('some data'), + { + default: () => 'default data', + immediate: false, + watch: [() => route.query] + } + ) + + return { + data, + execute, + pending + } +} diff --git a/test/fixtures/basic/pages/useAsyncData/shared.vue b/test/fixtures/basic/pages/useAsyncData/shared.vue new file mode 100644 index 0000000000..c1a12a8f2b --- /dev/null +++ b/test/fixtures/basic/pages/useAsyncData/shared.vue @@ -0,0 +1,43 @@ + + +