2023-04-25 02:00:04 +03:00
|
|
|
import assert from 'assert'
|
2022-06-14 22:40:52 +03:00
|
|
|
|
|
|
|
describe('Service Worker', () => {
|
|
|
|
before(() => {
|
|
|
|
// Fail fast if there is no Service Worker on this build.
|
|
|
|
cy.request({ url: '/service-worker.js', headers: { 'Service-Worker': 'script' } }).then((response) => {
|
|
|
|
const isValid = isValidServiceWorker(response)
|
|
|
|
if (!isValid) {
|
|
|
|
throw new Error(
|
|
|
|
'\n' +
|
|
|
|
'Service Worker tests must be run on a production-like build\n' +
|
2023-05-17 22:10:28 +03:00
|
|
|
'To test, build with `yarn build` and serve with `yarn serve`'
|
2022-06-14 22:40:52 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function isValidServiceWorker(response: Cypress.Response<any>) {
|
|
|
|
const contentType = response.headers['content-type']
|
|
|
|
return !(response.status === 404 || (contentType != null && contentType.indexOf('javascript') === -1))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-17 22:10:28 +03:00
|
|
|
function unregisterServiceWorker() {
|
|
|
|
return cy.log('unregisters service worker').then(async () => {
|
2022-06-14 22:40:52 +03:00
|
|
|
const sw = await window.navigator.serviceWorker.getRegistration(Cypress.config().baseUrl ?? undefined)
|
|
|
|
await sw?.unregister()
|
|
|
|
})
|
|
|
|
}
|
2023-05-17 22:10:28 +03:00
|
|
|
before(unregisterServiceWorker)
|
|
|
|
after(unregisterServiceWorker)
|
2022-06-14 22:40:52 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-05-17 22:10:28 +03:00
|
|
|
cy.intercept('https://api.uniswap.org/v1/amplitude-proxy', (req) => {
|
|
|
|
const body = JSON.stringify(req.body)
|
|
|
|
const serviceWorkerStatus = body.match(/"service_worker":"(\w+)"/)?.[1]
|
|
|
|
if (serviceWorkerStatus) {
|
|
|
|
req.alias = `ServiceWorker:${serviceWorkerStatus}`
|
2022-06-14 22:40:52 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-05-17 22:10:28 +03:00
|
|
|
it('installs a ServiceWorker and reports the uninstalled status to analytics', () => {
|
2022-06-14 22:40:52 +03:00
|
|
|
cy.visit('/', { serviceWorker: true })
|
2023-05-17 22:10:28 +03:00
|
|
|
cy.wait('@ServiceWorker:uninstalled')
|
|
|
|
cy.window().should(
|
|
|
|
'have.nested.property',
|
|
|
|
// The parent is checked instead of the AUT because it is on the same origin,
|
|
|
|
// and the AUT will not be considered "activated" until the parent is idle.
|
|
|
|
'parent.navigator.serviceWorker.controller.state',
|
|
|
|
'activated'
|
|
|
|
)
|
2022-06-14 22:40:52 +03:00
|
|
|
})
|
|
|
|
|
2023-05-17 22:10:28 +03:00
|
|
|
describe('cache hit', () => {
|
|
|
|
it('reports the hit to analytics', () => {
|
|
|
|
cy.visit('/', { serviceWorker: true })
|
|
|
|
cy.wait('@ServiceWorker:hit')
|
|
|
|
})
|
2022-06-14 22:40:52 +03:00
|
|
|
})
|
|
|
|
|
2023-05-17 22:10:28 +03:00
|
|
|
describe('cache miss', () => {
|
|
|
|
let cache: Cache | undefined
|
|
|
|
let request: Request | undefined
|
|
|
|
let response: Response | undefined
|
|
|
|
before(() => {
|
|
|
|
// Mocks the index.html in the cache to force a cache miss.
|
|
|
|
cy.visit('/', { serviceWorker: true }).then(async () => {
|
|
|
|
const cacheKeys = await window.caches.keys()
|
|
|
|
const cacheKey = cacheKeys.find((key) => key.match(/precache/))
|
|
|
|
assert(cacheKey)
|
|
|
|
|
|
|
|
cache = await window.caches.open(cacheKey)
|
|
|
|
const keys = await cache.keys()
|
|
|
|
request = keys.find((key) => key.url.match(/index/))
|
|
|
|
assert(request)
|
2022-06-14 22:40:52 +03:00
|
|
|
|
2023-05-17 22:10:28 +03:00
|
|
|
response = await cache.match(request)
|
|
|
|
assert(response)
|
2022-06-14 22:40:52 +03:00
|
|
|
|
2023-05-17 22:10:28 +03:00
|
|
|
await cache.put(request, new Response())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
after(() => {
|
|
|
|
// Restores the index.html in the cache so that re-runs behave as expected.
|
|
|
|
// This is necessary because the Service Worker will not re-populate the cache.
|
|
|
|
cy.then(async () => {
|
|
|
|
if (cache && request && response) {
|
|
|
|
await cache.put(request, response)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it('reports the miss to analytics', () => {
|
|
|
|
cy.visit('/', { serviceWorker: true })
|
|
|
|
cy.wait('@ServiceWorker:miss')
|
2022-06-14 22:40:52 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|