This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it('should register a service worker and cache file on install', function() { | |
| // Mocha can handle promises, so as long as the promise doesn’t reject | |
| // this test will pass. | |
| return navigator.serviceWorker.register('/test/static/my-first-sw.js') | |
| .then((reg) => { | |
| return window.__waitForSWState(reg, 'installed'); | |
| }) | |
| .then(() => { | |
| return caches.match('/__test/example') | |
| .then((response) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| self.addEventListener('install', (event) => { | |
| const promiseChain = caches.open('test-cache') | |
| .then((openCache) => { | |
| return openCache.put( | |
| new Request('/__test/example'), | |
| new Response('Hello, World!') | |
| ); | |
| }); | |
| event.waitUntil(promiseChain); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| describe('Service Worker Suite', function() { | |
| beforeEach(function() { | |
| return navigator.serviceWorker.getRegistrations() | |
| .then((registrations) => { | |
| const unregisterPromises = registrations.map((registration) => { | |
| return registration.unregister(); | |
| }); | |
| return Promise.all(unregisterPromises); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it('should register a service worker and cache file on install', function() { | |
| // 1: Register service worker. | |
| // 2: Wait for service worker to install. | |
| // 3: Check cache was performed correctly. | |
| return navigator.serviceWorker.register('/test/static/my-first-sw.js') | |
| .then((reg) => { | |
| return window.__waitForSWState(reg, 'installed'); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- This Projects Libraries / Pieces of Code Here --> | |
| <script src="/test/utils/wait-for-sw-state.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| window.__waitForSWState = (registration, desiredState) => { | |
| return new Promise((resolve, reject) => { | |
| let serviceWorker = registration.installing; | |
| if (!serviceWorker) { | |
| return reject(new Error('The service worker is not installing. ' + | |
| 'Is the test environment clean?')); | |
| } | |
| const stateListener = (evt) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it(‘should register a service worker and cache file on install’, function() { | |
| // 1: Register service worker. | |
| // 2: Wait for service worker to install. | |
| // 3: Check cache was performed correctly. | |
| return navigator.serviceWorker.register(‘/test/static/my-first-sw.js’); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- This Projects Unit Tests Here --> | |
| <script src="/test/browser/my-first-test.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| describe(‘Service Worker Suite’, function() { | |
| it(‘should register a service worker and cache file on install’, function() { | |
| // 1: Register service worker. | |
| // 2: Wait for service worker to install. | |
| // 3: Check cache was performed correctly. | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Mocha Tests</title> | |
| <link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" /> | |
| </head> | |
| <body> | |
| <!-- Mocha Requires a <div> with ID Mocha to Inject UI --> | |
| <div id="mocha"></div> |