Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion test/unit/purge_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ test.serial('Throws if the API response does not have a successful status code',
body: (payload) => {
const data = JSON.parse(payload)

t.is(data.cache_tags, undefined)
t.is(data.site_id, mockSiteID)
},
headers: { Authorization: `Bearer ${mockToken}` },
Expand Down Expand Up @@ -104,7 +105,7 @@ test.serial('Ignores purgeCache if in local dev with no token or site', async (t
const mockAPI = new MockFetch().post({
body: () => {
t.fail()
}
},
})
const myFunction = async () => {
await purgeCache()
Expand All @@ -116,3 +117,40 @@ test.serial('Ignores purgeCache if in local dev with no token or site', async (t

t.is(response, undefined)
})

test.serial('Calls the purge API endpoint with an empty array of cache tags', async (t) => {
if (!hasFetchAPI) {
console.warn('Skipping test requires the fetch API')

return t.pass()
}

const mockSiteID = '123456789'
const mockToken = '1q2w3e4r5t6y7u8i9o0p'

process.env.NETLIFY_PURGE_API_TOKEN = mockToken
process.env.SITE_ID = mockSiteID

const mockAPI = new MockFetch().post({
body: (payload) => {
const data = JSON.parse(payload)

t.deepEqual(data.cache_tags, [])
t.is(data.site_id, mockSiteID)
},
headers: { Authorization: `Bearer ${mockToken}` },
method: 'post',
response: new Response(null, { status: 202 }),
url: `https://api.netlify.com/api/v1/purge`,
})
const myFunction = async () => {
await purgeCache({ tags: [] })
}

globalThis.fetch = mockAPI.fetcher

const response = await invokeLambda(myFunction)

t.is(response, undefined)
t.true(mockAPI.fulfilled)
})
Loading