Compare commits
No commits in common. "d5f474768cf6c037c22d74544f43c0d57ddc8a09" and "37f32cce3aca65a9c478c7f944c418ffb4e40fff" have entirely different histories.
d5f474768c
...
37f32cce3a
|
|
@ -25,7 +25,6 @@ const createSelf = () =>
|
||||||
clients: {
|
clients: {
|
||||||
claim: jest.fn(() => Promise.resolve()),
|
claim: jest.fn(() => Promise.resolve()),
|
||||||
openWindow: jest.fn(() => Promise.resolve()),
|
openWindow: jest.fn(() => Promise.resolve()),
|
||||||
matchAll: jest.fn(() => Promise.resolve([])),
|
|
||||||
},
|
},
|
||||||
}) as unknown as ServiceWorkerGlobalScope
|
}) as unknown as ServiceWorkerGlobalScope
|
||||||
|
|
||||||
|
|
@ -33,16 +32,12 @@ let originalWindow: Window & typeof globalThis
|
||||||
|
|
||||||
describe("service worker", () => {
|
describe("service worker", () => {
|
||||||
let self: ServiceWorkerGlobalScope
|
let self: ServiceWorkerGlobalScope
|
||||||
let controlledClients: Client[]
|
|
||||||
let uncontrolledClients: Client[]
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
originalWindow = global.window
|
originalWindow = global.window
|
||||||
global.window = undefined as any
|
global.window = undefined as any
|
||||||
indexedDB = new IDBFactory()
|
indexedDB = new IDBFactory()
|
||||||
self = createSelf()
|
self = createSelf()
|
||||||
controlledClients = []
|
|
||||||
uncontrolledClients = []
|
|
||||||
|
|
||||||
fetchMock.mockResponse((req) => {
|
fetchMock.mockResponse((req) => {
|
||||||
if (
|
if (
|
||||||
|
|
@ -99,71 +94,28 @@ describe("service worker", () => {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("on notification click", () => {
|
test("closes notification on click", async () => {
|
||||||
test("the notification is closed", async () => {
|
initWorker(self)
|
||||||
initWorker(self)
|
|
||||||
|
|
||||||
const notificationHandler = getHandler("notificationclick")
|
const notificationHandler = getHandler("notificationclick")
|
||||||
|
|
||||||
const event = createNotificationEvent("tackupnow.com")
|
const event = createNotificationEvent("tackupnow.com")
|
||||||
notificationHandler(event)
|
notificationHandler(event)
|
||||||
await waitUntilCalls(event)
|
await waitUntilCalls(event)
|
||||||
|
|
||||||
expect(event.notification.close).toHaveBeenCalled()
|
expect(event.notification.close).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
test("opens tack up now if no clients match the destination", async () => {
|
test("opens tack up now on click", async () => {
|
||||||
initWorker(self)
|
initWorker(self)
|
||||||
|
|
||||||
const notificationHandler = getHandler("notificationclick")
|
const notificationHandler = getHandler("notificationclick")
|
||||||
|
|
||||||
const event = createNotificationEvent("tackupnow.com")
|
const event = createNotificationEvent("tackupnow.com")
|
||||||
notificationHandler(event)
|
notificationHandler(event)
|
||||||
await waitUntilCalls(event)
|
await waitUntilCalls(event)
|
||||||
|
|
||||||
expect(self.clients.openWindow).toHaveBeenCalledWith("tackupnow.com")
|
expect(self.clients.openWindow).toHaveBeenCalledWith("tackupnow.com")
|
||||||
})
|
|
||||||
|
|
||||||
test("focuses first matching client", async () => {
|
|
||||||
addClient("https://tackupnow.com/place", "window", true)
|
|
||||||
const matchingClient = addClient(
|
|
||||||
"https://tackupnow.com/otherplace",
|
|
||||||
"window",
|
|
||||||
true
|
|
||||||
)
|
|
||||||
addClient("https://tackupnow.com/yetanotherotherplace", "window", true)
|
|
||||||
|
|
||||||
initWorker(self)
|
|
||||||
|
|
||||||
const notificationHandler = getHandler("notificationclick")
|
|
||||||
|
|
||||||
const event = createNotificationEvent("https://tackupnow.com/otherplace")
|
|
||||||
notificationHandler(event)
|
|
||||||
await waitUntilCalls(event)
|
|
||||||
|
|
||||||
expect(matchingClient.focus).toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
test("focuses uncontrolled matching clients", async () => {
|
|
||||||
addClient("https://derpatious.world", "window", false)
|
|
||||||
addClient("https://tackupnow.com/1", "window", false)
|
|
||||||
const matchingClient = addClient(
|
|
||||||
"https://tackupnow.com/2",
|
|
||||||
"window",
|
|
||||||
false
|
|
||||||
)
|
|
||||||
addClient("https://tackupnow.com/controlled", "window", true)
|
|
||||||
|
|
||||||
initWorker(self)
|
|
||||||
|
|
||||||
const notificationHandler = getHandler("notificationclick")
|
|
||||||
|
|
||||||
const event = createNotificationEvent("https://tackupnow.com/2")
|
|
||||||
notificationHandler(event)
|
|
||||||
await waitUntilCalls(event)
|
|
||||||
|
|
||||||
expect(matchingClient.focus).toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test("claims on activate", async () => {
|
test("claims on activate", async () => {
|
||||||
|
|
@ -335,31 +287,6 @@ describe("service worker", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
function addClient(url: string, type: ClientTypes, controlled: boolean) {
|
|
||||||
const matchAllMock = self.clients.matchAll as jest.Mock
|
|
||||||
|
|
||||||
matchAllMock.mockImplementation(
|
|
||||||
(args: { type: string; includeUncontrolled?: boolean }) => {
|
|
||||||
const clientList = args.includeUncontrolled
|
|
||||||
? [...uncontrolledClients, ...controlledClients]
|
|
||||||
: controlledClients
|
|
||||||
return clientList.filter((client) => client.type === args.type)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
const clientList = controlled ? controlledClients : uncontrolledClients
|
|
||||||
|
|
||||||
const client: WindowClient = {
|
|
||||||
url,
|
|
||||||
type,
|
|
||||||
focus: jest.fn(() => Promise.resolve(client)) as WindowClient["focus"],
|
|
||||||
} as WindowClient
|
|
||||||
|
|
||||||
clientList.push(client)
|
|
||||||
|
|
||||||
return client
|
|
||||||
}
|
|
||||||
|
|
||||||
function getHandler(eventName: string) {
|
function getHandler(eventName: string) {
|
||||||
expect(self.addEventListener).toHaveBeenCalledWith(
|
expect(self.addEventListener).toHaveBeenCalledWith(
|
||||||
eventName,
|
eventName,
|
||||||
|
|
|
||||||
|
|
@ -31,25 +31,9 @@ export default function start(self: ServiceWorkerGlobalScope) {
|
||||||
|
|
||||||
self.addEventListener("notificationclick", function (event) {
|
self.addEventListener("notificationclick", function (event) {
|
||||||
event.notification.close()
|
event.notification.close()
|
||||||
const destination = event.notification.data.destination
|
|
||||||
|
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
(async () => {
|
self.clients.openWindow(event.notification.data.destination)
|
||||||
const clients = await self.clients.matchAll({
|
|
||||||
type: "window",
|
|
||||||
includeUncontrolled: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
const existingClient = clients.find(
|
|
||||||
(client) => client.url === event.notification.data.destination
|
|
||||||
)
|
|
||||||
|
|
||||||
if (existingClient === undefined) {
|
|
||||||
await self.clients.openWindow(destination)
|
|
||||||
} else {
|
|
||||||
await existingClient.focus()
|
|
||||||
}
|
|
||||||
})()
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue