-
Notifications
You must be signed in to change notification settings - Fork 15k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add tests for the service worker module
- Loading branch information
1 parent
4a0ec9c
commit 87baf6b
Showing
10 changed files
with
211 additions
and
382 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,62 @@ | ||
## Class: ServiceWorkers | ||
|
||
> Query and receive events from a sessions active service workers. | ||
Process: [Main](../glossary.md#main-process) | ||
|
||
Instances of the `ServiceWorkers` class are accessed by using `serviceWorkers` property of | ||
a `Session`. | ||
|
||
For example: | ||
|
||
```javascript | ||
const { session } = require('electron') | ||
|
||
// Get all service workers. | ||
console.log(session.defaultSession.serviceWorkers.getAllRunning()) | ||
|
||
// Handle logs and get service worker info | ||
session.defaultSession.serviceWorkers.on('console-message', (event, messageDetails) => { | ||
console.log( | ||
'Got service worker message', | ||
messageDetails, | ||
'from', | ||
session.defaultSession.serviceWorkers.getFromVersionID(messageDetails.versionId) | ||
) | ||
}) | ||
``` | ||
|
||
### Instance Events | ||
|
||
The following events are available on instances of `ServiceWorkers`: | ||
|
||
#### Event: 'console-message' | ||
|
||
* `event` Event | ||
* `messageDetails` Object - Information about the console message | ||
* `message` String - The actual console message | ||
* `versionId` Number - The version ID of the service worker that sent the log message | ||
* `source` String - The type of source for this message. Can be `javascript`, `xml`, `network`, `console-api`, `storage`, `app-cache`, `rendering`, `security`, `deprecation`, `worker`, `violation`, `intervention`, `recommendation` or `other`. | ||
* `level` Number - The log level, from 0 to 3. In order it matches `verbose`, `info`, `warning` and `error`. | ||
* `sourceUrl` String - The URL the message came from | ||
* `lineNumber` Number - The line number of the source that triggered this console message | ||
|
||
Emitted when a service worker logs something to the console. | ||
|
||
### Instance Methods | ||
|
||
The following methods are available on instances of `ServiceWorkers`: | ||
|
||
#### `serviceWorkers.getAllRunning()` | ||
|
||
Returns `Record<Number, ServiceWorkerInfo>` - A [ServiceWorkerInfo](structures/service-worker-info.md) object where the keys are the service worker version ID and the values are the information about that service worker. | ||
|
||
#### `serviceWorkers.getFromVersionID(versionId)` | ||
|
||
* `versionId` Number | ||
|
||
Returns [`ServiceWorkerInfo`](structures/service-worker-info.md) - Information about this service worker | ||
|
||
If the service worker does not exist or is not running this method will throw an exception. | ||
## Class: ServiceWorkers | ||
|
||
> Query and receive events from a sessions active service workers. | ||
Process: [Main](../glossary.md#main-process) | ||
|
||
Instances of the `ServiceWorkers` class are accessed by using `serviceWorkers` property of | ||
a `Session`. | ||
|
||
For example: | ||
|
||
```javascript | ||
const { session } = require('electron') | ||
|
||
// Get all service workers. | ||
console.log(session.defaultSession.serviceWorkers.getAllRunning()) | ||
|
||
// Handle logs and get service worker info | ||
session.defaultSession.serviceWorkers.on('console-message', (event, messageDetails) => { | ||
console.log( | ||
'Got service worker message', | ||
messageDetails, | ||
'from', | ||
session.defaultSession.serviceWorkers.getFromVersionID(messageDetails.versionId) | ||
) | ||
}) | ||
``` | ||
|
||
### Instance Events | ||
|
||
The following events are available on instances of `ServiceWorkers`: | ||
|
||
#### Event: 'console-message' | ||
|
||
Returns: | ||
|
||
* `event` Event | ||
* `messageDetails` Object - Information about the console message | ||
* `message` String - The actual console message | ||
* `versionId` Number - The version ID of the service worker that sent the log message | ||
* `source` String - The type of source for this message. Can be `javascript`, `xml`, `network`, `console-api`, `storage`, `app-cache`, `rendering`, `security`, `deprecation`, `worker`, `violation`, `intervention`, `recommendation` or `other`. | ||
* `level` Number - The log level, from 0 to 3. In order it matches `verbose`, `info`, `warning` and `error`. | ||
* `sourceUrl` String - The URL the message came from | ||
* `lineNumber` Number - The line number of the source that triggered this console message | ||
|
||
Emitted when a service worker logs something to the console. | ||
|
||
### Instance Methods | ||
|
||
The following methods are available on instances of `ServiceWorkers`: | ||
|
||
#### `serviceWorkers.getAllRunning()` | ||
|
||
Returns `Record<Number, ServiceWorkerInfo>` - A [ServiceWorkerInfo](structures/service-worker-info.md) object where the keys are the service worker version ID and the values are the information about that service worker. | ||
|
||
#### `serviceWorkers.getFromVersionID(versionId)` | ||
|
||
* `versionId` Number | ||
|
||
Returns [`ServiceWorkerInfo`](structures/service-worker-info.md) - Information about this service worker | ||
|
||
If the service worker does not exist or is not running this method will throw an exception. |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import * as fs from 'fs' | ||
import * as http from 'http' | ||
import * as path from 'path' | ||
import { session, BrowserWindow } from 'electron' | ||
import { expect } from 'chai' | ||
import { v4 } from 'uuid' | ||
import { AddressInfo } from 'net' | ||
import { closeWindow } from './window-helpers' | ||
import { emittedOnce, emittedNTimes } from './events-helpers' | ||
|
||
const partition = 'service-workers-spec' | ||
const uuid = v4() | ||
|
||
describe('session.serviceWorkers', () => { | ||
let ses: Electron.Session | ||
let server: http.Server | ||
let baseUrl: string | ||
let w: BrowserWindow | ||
|
||
before(async () => { | ||
ses = session.fromPartition(partition) | ||
await ses.clearStorageData() | ||
|
||
server = http.createServer((req, res) => { | ||
// /{uuid}/{file} | ||
const file = req.url!.split('/')[2]!; | ||
|
||
if (file.endsWith('.js')) { | ||
res.setHeader('Content-Type', 'application/javascript') | ||
} | ||
res.end(fs.readFileSync(path.resolve(__dirname, 'fixtures', 'api', 'service-workers', file))) | ||
}) | ||
await new Promise(r => { | ||
server.listen(0, '127.0.0.1', () => { | ||
baseUrl = `http://localhost:${(server.address() as AddressInfo).port}/${uuid}` | ||
r() | ||
}) | ||
}) | ||
}) | ||
|
||
beforeEach(() => { | ||
w = new BrowserWindow({ show: false, webPreferences: { session: ses } }) | ||
}) | ||
|
||
afterEach(async () => { | ||
await ses.clearStorageData() | ||
await closeWindow(w) | ||
w = null as any | ||
}) | ||
|
||
after(async () => { | ||
server.close() | ||
}) | ||
|
||
describe('getAllRunning()', () => { | ||
it('should initially report none are running', () => { | ||
expect(ses.serviceWorkers.getAllRunning()).to.deep.equal({}) | ||
}) | ||
|
||
it('should report one as running once you load a page with a service worker', async () => { | ||
await emittedOnce(ses.serviceWorkers, 'console-message', () => w.loadURL(`${baseUrl}/index.html`)) | ||
const workers = ses.serviceWorkers.getAllRunning() | ||
const ids = Object.keys(workers) as any[] as number[] | ||
expect(ids).to.have.lengthOf(1, 'should have one worker running') | ||
}) | ||
}) | ||
|
||
describe('getFromVersionID()', () => { | ||
it('should report the correct sciprt url and scope', async () => { | ||
const eventInfo = await emittedOnce(ses.serviceWorkers, 'console-message', () => w.loadURL(`${baseUrl}/index.html`)) | ||
const details: Electron.MessageDetails = eventInfo[1] | ||
const worker = ses.serviceWorkers.getFromVersionID(details.versionId) | ||
expect(worker).to.not.equal(null) | ||
expect(worker).to.have.property('scope', baseUrl + '/') | ||
expect(worker).to.have.property('scriptUrl', baseUrl + '/sw.js') | ||
}) | ||
}) | ||
|
||
describe('console-message event', () => { | ||
it('should correctly keep the source, message and level', async () => { | ||
const messages: Record<string, Electron.MessageDetails> = {} | ||
const events = await emittedNTimes(ses.serviceWorkers, 'console-message', 4, () => w.loadURL(`${baseUrl}/logs.html`)) | ||
for (const event of events) { | ||
messages[event[1].message] = event[1] | ||
|
||
expect(event[1]).to.have.property('source', 'console-api') | ||
} | ||
|
||
expect(messages).to.have.property('log log') | ||
expect(messages).to.have.property('info log') | ||
expect(messages).to.have.property('warn log') | ||
expect(messages).to.have.property('error log') | ||
expect(messages['log log']).to.have.property('level', 1) | ||
expect(messages['info log']).to.have.property('level', 1) | ||
expect(messages['warn log']).to.have.property('level', 2) | ||
expect(messages['error log']).to.have.property('level', 3) | ||
}) | ||
}) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body> | ||
<script> | ||
navigator.serviceWorker.register('sw.js', { | ||
scope: location.pathname.split('/').slice(0, 2).join('/') + '/' | ||
}) | ||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body> | ||
<script> | ||
navigator.serviceWorker.register('sw-logs.js', { | ||
scope: location.pathname.split('/').slice(0, 2).join('/') + '/' | ||
}) | ||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
self.addEventListener('install', function(event) { | ||
console.log('log log') | ||
console.info('info log') | ||
console.warn('warn log') | ||
console.error('error log') | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
self.addEventListener('install', function(event) { | ||
console.log('Installed') | ||
}) |
This file contains 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
Oops, something went wrong.