Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: test Connection class in the browser #6269

Merged
merged 1 commit into from Jul 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 additions & 0 deletions test-browser/connection.spec.js
@@ -0,0 +1,61 @@
/**
* Copyright 2020 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Connection } from '../lib/esm/puppeteer/common/Connection.js';
import expect from '../node_modules/expect/build-es5/index.js';

/**
* A fake transport that echoes the message it got back and pretends to have got a result.
*
* In actual pptr code we expect that `result` is returned from the message
* being sent with some data, so we fake that in the `send` method.
*
* We don't define `onmessage` here because Puppeteer's Connection class will
* define an `onmessage` for us.
*/
class EchoTransport {
send(message) {
const object = JSON.parse(message);
const fakeMessageResult = {
result: `fake-test-result-${object.method}`,
};
this.onmessage(
JSON.stringify({
...object,
...fakeMessageResult,
})
);
}

close() {}
}

describe('Connection', () => {
it('can be created in the browser and send/receive messages', async () => {
let receivedOutput = '';
const connection = new Connection('fake-url', new EchoTransport());

/**
* Puppeteer increments a counter from 0 for each
* message it sends So we have to register a callback for the object with
* the ID of `1` as the message we send will be the first.
*/
connection._callbacks.set(1, {
resolve: (data) => (receivedOutput = data),
});
connection.send('Browser.getVersion');
expect(receivedOutput).toEqual('fake-test-result-Browser.getVersion');
});
});