diff --git a/test/unit/purge_cache.js b/test/unit/purge_cache.js index 6fd0ef7f..8204ec71 100644 --- a/test/unit/purge_cache.js +++ b/test/unit/purge_cache.js @@ -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}` }, @@ -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() @@ -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) +})