fix(service-worker): listen for messages on the right event source (#19954)

Currently, the SwUpdate service doesn't receive messages from the SW.
This is because it attempts to subscribe to the 'message' event on
ServiceWorkerRegistration, when really messages are emitted by the
ServiceWorkerContainer.

This change moves to listening on ServiceWorkerContainer and changes
the mocks to reflect the way the browser actually works.

PR Close #19954
This commit is contained in:
Alex Rickabaugh
2017-10-26 10:29:36 -07:00
committed by Matias Niemelä
parent 47bc6f105d
commit 5cfd9c6020
4 changed files with 28 additions and 35 deletions

View File

@ -44,11 +44,9 @@ export function main() {
});
describe('SwPush', () => {
let push: SwPush;
let reg: MockServiceWorkerRegistration;
beforeEach((done: DoneFn) => {
beforeEach(() => {
push = new SwPush(comm);
mock.setupSw();
mock.mockRegistration.then(r => reg = r).then(() => done());
});
it('receives push messages', (done: DoneFn) => {
push.messages.subscribe(msg => {
@ -57,7 +55,7 @@ export function main() {
});
done();
});
reg.sendMessage({
mock.sendMessage({
type: 'PUSH',
data: {
message: 'this was a push message',
@ -76,11 +74,9 @@ export function main() {
});
describe('SwUpdate', () => {
let update: SwUpdate;
let reg: MockServiceWorkerRegistration;
beforeEach((done: DoneFn) => {
beforeEach(() => {
update = new SwUpdate(comm);
mock.setupSw();
mock.mockRegistration.then(r => reg = r).then(() => done());
});
it('processes update availability notifications when sent', (done: DoneFn) => {
update.available.subscribe(event => {
@ -89,7 +85,7 @@ export function main() {
expect(event.type).toEqual('UPDATE_AVAILABLE');
done();
});
reg.sendMessage({
mock.sendMessage({
type: 'UPDATE_AVAILABLE',
current: {
version: 'A',
@ -106,7 +102,7 @@ export function main() {
expect(event.type).toEqual('UPDATE_ACTIVATED');
done();
});
reg.sendMessage({
mock.sendMessage({
type: 'UPDATE_ACTIVATED',
previous: {
version: 'A',
@ -119,7 +115,7 @@ export function main() {
it('activates updates when requested', (done: DoneFn) => {
mock.messages.subscribe((msg: {action: string, statusNonce: number}) => {
expect(msg.action).toEqual('ACTIVATE_UPDATE');
reg.sendMessage({
mock.sendMessage({
type: 'STATUS',
nonce: msg.statusNonce,
status: true,
@ -130,7 +126,7 @@ export function main() {
it('reports activation failure when requested', (done: DoneFn) => {
mock.messages.subscribe((msg: {action: string, statusNonce: number}) => {
expect(msg.action).toEqual('ACTIVATE_UPDATE');
reg.sendMessage({
mock.sendMessage({
type: 'STATUS',
nonce: msg.statusNonce,
status: false,

View File

@ -85,7 +85,7 @@ export function main() {
driver = new Driver(scope, scope, new CacheDatabase(scope, scope));
scope.clients.add('default');
scope.clients.getMock('default') !.queue.subscribe(msg => { reg.sendMessage(msg); });
scope.clients.getMock('default') !.queue.subscribe(msg => { mock.sendMessage(msg); });
mock.messages.subscribe(msg => { scope.handleMessage(msg, 'default'); });