Skip to content

Instantly share code, notes, and snippets.

View gauntface's full-sized avatar
🤓
Mon-Fri: Working. Sat-Sun: Not Working.

Matt Gaunt-Seo gauntface

🤓
Mon-Fri: Working. Sat-Sun: Not Working.
View GitHub Profile
importScripts(‘/<Path to library file>.js’);
self.addEventListener('push', (event) => {
self._handlePushEvent(event);
});
self._handlePushEvent = (event) => {
const data = event.data.json();
const promiseChain = self.registration.showNotification(data.title, {
body: data.body,
});
event.waitUntil(promiseChain);
};
self.addEventListener('push', (event) => {
const data = event.data.json();
const promiseChain = self.registration.showNotification(data.title, {
body: data.body,
});
event.waitUntil(promiseChain);
});
const event = new NotificationEvent('notificationclose', {
notification: notifications[0],
});
const event = new NotificationEvent('notificationclick', {
notification: notifications[0],
});
return new Promise((resolve, reject) => {
const event = new FetchEvent('fetch', {
request: new Request('/index.html'),
});
event.respondWith = (promiseChain) => {
if (promiseChain) {
// Check if promise was returned - otherise
// it could be a response
if (promiseChain instanceof Promise) {
promiseChain.then(resolve, reject);
const event = new FetchEvent('fetch', {
request: new Request('/index.html'),
});
@gauntface
gauntface / full-fake-push-test.js
Created March 6, 2017 22:13
Example of testing a fake push event.
// This could be in our test directly or imported using importScripts();
self.addEventListener('push', (event) => {
const data = event.data.json();
const promiseChain = self.registration.showNotification(data.title, data);
event.waitUntil(promiseChain);
});
describe('First SW Test Suite', function() {
it('should be able to test a push event', function() {
@gauntface
gauntface / test-fake-push-event.js
Created March 6, 2017 22:12
Testing a fake push event.
it('should be able to test a push event', function() {
const pushData = {
title: 'Example Title.',
body: 'Example Body.',
};
return new Promise((resolve, reject) => {
const fakePushEvent = new PushEvent('push', {
data: JSON.stringify(pushData),
});
self.dispatchEvent(fakePushEvent);