diff options
author | 2022-10-07 16:13:51 +0200 | |
---|---|---|
committer | 2022-10-07 10:13:51 -0400 | |
commit | 92b27e9c9253cea3d00f1f81223de19ff75c2c74 (patch) | |
tree | d0012f6888379759144637aa48b1c0632c6f88c9 /packages/integrations/prefetch/test/custom-selectors.test.js | |
parent | 358ffb541d53911d3996eca6317748d1f4a7da3e (diff) | |
download | astro-92b27e9c9253cea3d00f1f81223de19ff75c2c74.tar.gz astro-92b27e9c9253cea3d00f1f81223de19ff75c2c74.tar.zst astro-92b27e9c9253cea3d00f1f81223de19ff75c2c74.zip |
[@astrojs/prefetch]: Prevent prefetching current page (#5009)
* Check that removal of url.hash breaks no tests
* test if status-quo is as expected
* Adapt tests to fail
* Adapt the shouldPreload function to skip same path
* Add changeset
Diffstat (limited to 'packages/integrations/prefetch/test/custom-selectors.test.js')
-rw-r--r-- | packages/integrations/prefetch/test/custom-selectors.test.js | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/packages/integrations/prefetch/test/custom-selectors.test.js b/packages/integrations/prefetch/test/custom-selectors.test.js index 64ac5fc63..d57ac3b90 100644 --- a/packages/integrations/prefetch/test/custom-selectors.test.js +++ b/packages/integrations/prefetch/test/custom-selectors.test.js @@ -25,20 +25,24 @@ test.describe('Custom prefetch selectors', () => { test.describe('prefetches links by custom selector', () => { test('only prefetches /contact', async ({ page, astro }) => { - const requests = new Set(); + const requests = []; - page.on('request', async (request) => requests.add(request.url())); + page.on('request', async (request) => requests.push(request.url())); await page.goto(astro.resolveUrl('/')); await page.waitForLoadState('networkidle'); - await expect(requests.has(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy(); - await expect( - requests.has(astro.resolveUrl('/contact')), + expect(requests.includes(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy(); + expect( + requests.includes(astro.resolveUrl('/contact')), '/contact was prefetched' ).toBeTruthy(); - await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); + expect(requests.includes(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); + expect( + requests.filter((r) => r === astro.resolveUrl('/')).length === 1, + '/ was skipped by prefetch and only queried once' + ).toBeTruthy(); }); }); }); @@ -58,20 +62,24 @@ test.describe('Custom prefetch selectors', () => { test.describe('prefetches links by custom selector', () => { test('only prefetches /contact', async ({ page, astro }) => { - const requests = new Set(); + const requests = []; - page.on('request', async (request) => requests.add(request.url())); + page.on('request', async (request) => requests.push(request.url())); await page.goto(astro.resolveUrl('/')); await page.waitForLoadState('networkidle'); - await expect(requests.has(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy(); - await expect( - requests.has(astro.resolveUrl('/contact')), + expect(requests.includes(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy(); + expect( + requests.includes(astro.resolveUrl('/contact')), '/contact was prefetched' ).toBeTruthy(); - await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); + expect(requests.includes(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); + expect( + requests.filter((r) => r === astro.resolveUrl('/')).length === 1, + '/ was skipped by prefetch and only queried once' + ).toBeTruthy(); }); }); }); |