Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dulnan committed Nov 15, 2024
1 parent 2282c59 commit 4f0d10f
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
19 changes: 18 additions & 1 deletion playground/app/components/Navbar.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<template>
<nav class="menu">
<div class="menu-label">Main Menu</div>
<ul class="menu-list">
<li v-for="route in routes" :key="route.name">
<NuxtLink :to="route.path" :id="'route-' + route.name">{{
route.name
}}</NuxtLink>
</li>
</ul>
<div class="menu-label">User Management</div>
<ul class="menu-list">
<li v-for="user in users">
Expand All @@ -10,11 +18,20 @@
</template>

<script lang="ts" setup>
import { useAsyncData } from '#imports'
import { useAsyncData, useRouter } from '#imports'
const { data: users } = await useAsyncData('navbar', () => {
return $fetch('/api/getUsers').then((v) => {
return v
})
})
const router = useRouter()
const routes = router.getRoutes().map((route) => {
return {
name: route.name?.toString() || '',
path: route.path,
}
})
</script>
23 changes: 23 additions & 0 deletions playground/app/pages/useCachedAsyncData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
</li>
</ul>
</div>

<NuxtLink to="/" id="go-to-home">Go to Home</NuxtLink>

<div id="not-cached-data">{{ notCachedData }}</div>
<div id="no-max-age">{{ noMaxAge }}</div>
</div>
</template>

Expand All @@ -35,6 +40,24 @@ const { data, refresh } = await useCachedAsyncData(
},
)
const { data: notCachedData } = await useCachedAsyncData(
'not-cached-data',
() => Promise.resolve(Date.now().toString()),
{
serverMaxAge: 0,
clientMaxAge: 0,
},
)
const { data: noMaxAge } = await useCachedAsyncData(
'no-max-age',
() => Promise.resolve(Date.now().toString()),
{
serverMaxAge: undefined,
clientMaxAge: undefined,
},
)
function onClick() {
refresh()
}
Expand Down
69 changes: 69 additions & 0 deletions test/useCachedAsyncData.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,73 @@ describe('The useCachedAsyncData composable', () => {
`)
expect(item.data.expires).toBeTruthy()
})

test('treats a max age of 5 as cacheable on the client', async () => {
await purgeAll()

const page = await createPage('/useCachedAsyncData')
const number1 = await page.locator('#time').innerText()

await page.locator('#go-to-home').click()
await page.locator('#route-useCachedAsyncData').click()
const number2 = await page.locator('#time').innerText()

expect(number1).toEqual(number2)
})

test('treats a max age of 0 as uncacheable on the server', async () => {
await purgeAll()

const page1 = await createPage('/useCachedAsyncData')

const data: any = await getDataCacheItems()
expect(data.rows).toHaveLength(1)

const number1 = await page1.locator('#not-cached-data').innerText()

const page2 = await createPage('/useCachedAsyncData')
const number2 = await page2.locator('#not-cached-data').innerText()
expect(number1).not.toEqual(number2)
})

test('treats a max age of 0 as uncacheable on the client', async () => {
await purgeAll()

const page = await createPage('/useCachedAsyncData')
const number1 = await page.locator('#not-cached-data').innerText()

await page.locator('#go-to-home').click()
await page.locator('#route-useCachedAsyncData').click()
const number2 = await page.locator('#not-cached-data').innerText()

expect(number1).not.toEqual(number2)
})

test('treats no max age as uncacheable on the server', async () => {
await purgeAll()

const page1 = await createPage('/useCachedAsyncData')

const data: any = await getDataCacheItems()
expect(data.rows).toHaveLength(1)

const number1 = await page1.locator('#no-max-age').innerText()

const page2 = await createPage('/useCachedAsyncData')
const number2 = await page2.locator('#no-max-age').innerText()
expect(number1).not.toEqual(number2)
})

test('treats no max age as uncacheable on the client', async () => {
await purgeAll()

const page = await createPage('/useCachedAsyncData')
const number1 = await page.locator('#no-max-age').innerText()

await page.locator('#go-to-home').click()
await page.locator('#route-useCachedAsyncData').click()
const number2 = await page.locator('#no-max-age').innerText()

expect(number1).not.toEqual(number2)
})
})

0 comments on commit 4f0d10f

Please sign in to comment.