Skip to content

Commit decc763

Browse files
committed
additional tests
1 parent 2a7b32a commit decc763

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

packages/driver/cypress/e2e/e2e/service-worker.cy.js

+90
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,74 @@ describe('service workers', { defaultCommandTimeout: 1000, pageLoadTimeout: 1000
383383
})
384384
})
385385

386+
describe('multiple concurrent requests', () => {
387+
it('page request sent (handled by service worker) and then service worker request', () => {
388+
const script = () => {
389+
self.addEventListener('fetch', function (event) {
390+
const response = fetch(event.request)
391+
392+
// send a request from the service worker after the page request
393+
if (event.request.url.includes('timeout')) {
394+
fetch('/timeout').catch(() => {})
395+
}
396+
397+
event.respondWith(response)
398+
})
399+
}
400+
401+
cy.intercept('/fixtures/service-worker.js', (req) => {
402+
req.reply(`(${script})()`,
403+
{ 'Content-Type': 'application/javascript' })
404+
})
405+
406+
cy.visit('/fixtures/service-worker.html')
407+
cy.get('#output').should('have.text', 'done')
408+
})
409+
410+
it('service worker request sent and then page request (handled by service worker):', () => {
411+
const script = () => {
412+
self.addEventListener('fetch', function (event) {
413+
// send a request from the service worker before the page request
414+
if (event.request.url.includes('timeout')) {
415+
fetch('/timeout').catch(() => {})
416+
}
417+
418+
const response = fetch(event.request)
419+
420+
event.respondWith(response)
421+
})
422+
}
423+
424+
cy.intercept('/fixtures/service-worker.js', (req) => {
425+
req.reply(`(${script})()`,
426+
{ 'Content-Type': 'application/javascript' })
427+
})
428+
429+
cy.visit('/fixtures/service-worker.html')
430+
cy.get('#output').should('have.text', 'done')
431+
})
432+
433+
it('page request sent (NOT handled by service worker) and then service worker request:', () => {
434+
const script = () => {
435+
self.addEventListener('fetch', function (event) {
436+
if (event.request.url.includes('timeout')) {
437+
fetch('/timeout').catch(() => {})
438+
}
439+
440+
return
441+
})
442+
}
443+
444+
cy.intercept('/fixtures/service-worker.js', (req) => {
445+
req.reply(`(${script})()`,
446+
{ 'Content-Type': 'application/javascript' })
447+
})
448+
449+
cy.visit('/fixtures/service-worker.html')
450+
cy.get('#output').should('have.text', 'done')
451+
})
452+
})
453+
386454
it('supports aborted listeners', () => {
387455
const script = () => {
388456
const alreadyAborted = new AbortController()
@@ -525,4 +593,26 @@ describe('service workers', { defaultCommandTimeout: 1000, pageLoadTimeout: 1000
525593
cy.get('#output').should('have.text', 'done')
526594
validateFetchHandlers({ listenerCount: 1 })
527595
})
596+
597+
it('supports clients.claim', () => {
598+
const script = () => {
599+
self.addEventListener('activate', (event) => {
600+
event.waitUntil(self.clients.claim())
601+
})
602+
603+
self.addEventListener('fetch', function (event) {
604+
event.respondWith(fetch(event.request))
605+
})
606+
}
607+
608+
cy.intercept('/fixtures/service-worker.js', (req) => {
609+
req.reply(`(${script})()`,
610+
{ 'Content-Type': 'application/javascript' })
611+
})
612+
613+
cy.visit('fixtures/service-worker.html')
614+
615+
cy.get('#output').should('have.text', 'done')
616+
validateFetchHandlers({ listenerCount: 1 })
617+
})
528618
})

0 commit comments

Comments
 (0)