-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
cri-client_spec.ts
123 lines (97 loc) · 3.54 KB
/
cri-client_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import chalk from 'chalk'
import EventEmitter from 'events'
import { create } from '../../../lib/browsers/cri-client'
const { expect, proxyquire, sinon } = require('../../spec_helper')
const DEBUGGER_URL = 'http://foo'
describe('lib/browsers/cri-client', function () {
let criClient: {
create: typeof create
}
let send: sinon.SinonStub
let criImport: sinon.SinonStub
let onError: sinon.SinonStub
let getClient: () => ReturnType<typeof create>
beforeEach(function () {
send = sinon.stub()
onError = sinon.stub()
criImport = sinon.stub()
.withArgs({
target: DEBUGGER_URL,
local: true,
})
.resolves({
send,
close: sinon.stub(),
_notifier: new EventEmitter(),
})
criClient = proxyquire('../lib/browsers/cri-client', {
'chrome-remote-interface': criImport,
})
getClient = () => criClient.create(DEBUGGER_URL, onError)
})
context('.create', function () {
it('returns an instance of the CRI client', async function () {
const client = await getClient()
expect(client.send).to.be.instanceOf(Function)
})
context('#send', function () {
it('calls cri.send with command and data', async function () {
send.resolves()
const client = await getClient()
client.send('Browser.getVersion', { baz: 'quux' })
expect(send).to.be.calledWith('Browser.getVersion', { baz: 'quux' })
})
it('rejects if cri.send rejects', async function () {
const err = new Error
send.rejects(err)
const client = await getClient()
await expect(client.send('Browser.getVersion', { baz: 'quux' }))
.to.be.rejectedWith(err)
})
context('retries', () => {
([
'WebSocket is not open',
// @see https://github.com/cypress-io/cypress/issues/7180
'WebSocket is already in CLOSING or CLOSED state',
]).forEach((msg) => {
it(`with '${msg}'`, async function () {
const err = new Error(msg)
send.onFirstCall().rejects(err)
send.onSecondCall().resolves()
const client = await getClient()
await client.send('Browser.getVersion', { baz: 'quux' })
expect(send).to.be.calledTwice
})
})
})
})
context('#ensureMinimumProtocolVersion', function () {
function withProtocolVersion (actual, test) {
if (actual) {
send.withArgs('Browser.getVersion')
.resolves({ protocolVersion: actual })
}
return getClient()
.then((client) => {
return client.ensureMinimumProtocolVersion(test)
})
}
it('resolves if protocolVersion = current', function () {
return expect(withProtocolVersion('1.3', '1.3')).to.be.fulfilled
})
it('resolves if protocolVersion > current', function () {
return expect(withProtocolVersion('1.4', '1.3')).to.be.fulfilled
})
it('rejects if Browser.getVersion not supported yet', function () {
send.withArgs('Browser.getVersion')
.rejects()
return expect(withProtocolVersion(null, '1.3')).to.be
.rejectedWith(`A minimum CDP version of ${chalk.yellow(`1.3`)} is required, but the current browser has an older version.`)
})
it('rejects if protocolVersion < current', function () {
return expect(withProtocolVersion('1.2', '1.3')).to.be
.rejectedWith(`A minimum CDP version of ${chalk.yellow(`1.3`)} is required, but the current browser has ${chalk.yellow(`1.2`)}.`)
})
})
})
})