dist/rpc.test.js
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { expect } from 'chai';
import { RPC } from './rpc';
describe('RPC', () => {
let rpc;
let messages;
beforeEach(() => {
messages = [];
rpc = new RPC({ postMessage: (data) => messages.push(data) }, '1.0');
});
afterEach(() => {
rpc.destroy();
});
const readyUp = () => __awaiter(this, void 0, void 0, function* () {
const promise = new Promise(resolve => {
rpc.expose('ready', (params) => {
expect(params.protocolVersion).to.equal('1.1');
resolve();
});
});
window.postMessage({
type: 'method',
method: 'ready',
serviceID: RPC.serviceID,
counter: 0,
params: {
protocolVersion: '1.1',
},
}, '*');
yield promise;
expect(rpc.remoteVersion()).to.equal('1.1');
});
it('should announce itself to the remote when created', () => {
expect(messages).to.deep.equal([
{
type: 'method',
serviceID: RPC.serviceID,
id: 0,
method: 'ready',
discard: true,
counter: 0,
params: { protocolVersion: '1.0' },
},
]);
});
it('should receive ready messages', () => __awaiter(this, void 0, void 0, function* () { return readyUp(); }));
it('should reject messages recieved from other services', () => __awaiter(this, void 0, void 0, function* () {
yield readyUp();
const promise = new Promise(resolve => {
rpc.expose('foo', (params) => {
expect(params.isInvalid).to.equal(false, 'expected to have rejected wrong service ID');
resolve();
});
});
window.postMessage({
type: 'method',
method: 'foo',
serviceID: 'invalid service ID',
counter: 1,
params: { isInvalid: true },
}, '*');
window.postMessage({
type: 'method',
method: 'foo',
serviceID: RPC.serviceID,
counter: 1,
params: { isInvalid: false },
}, '*');
yield promise;
}));
it('should reorder messages', () => __awaiter(this, void 0, void 0, function* () {
yield readyUp();
const sequence = [4, 2, 1, 3];
const promise = new Promise(resolve => {
let seen = 0;
rpc.expose('foo', (params) => {
seen++;
expect(params.counter).to.equal(seen);
if (seen === sequence.length) {
resolve();
}
});
});
sequence.forEach(counter => {
window.postMessage({
type: 'method',
method: 'foo',
serviceID: RPC.serviceID,
counter,
params: { counter },
}, '*');
});
yield promise;
}));
});