From cdf1407b81f108e0d8d709cd73a9dfad231613c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Wed, 18 Dec 2024 13:41:29 +0000 Subject: [PATCH] chore: add test --- test/unit/purge_cache.js | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) 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) +})