/
commands.cy.js
170 lines (137 loc) · 4.79 KB
/
commands.cy.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const { _ } = Cypress
describe('src/cy/commands/commands', () => {
beforeEach(() => {
cy.visit('/fixtures/dom.html')
})
it('can invoke commands by name', () => {
const body = cy.$$('body')
cy
.get('body').then(($body) => {
expect($body.get(0)).to.eq(body.get(0))
})
.command('get', 'body').then(($body) => {
expect($body.get(0)).to.eq(body.get(0))
})
})
it('can invoke child commands by name', () => {
const div = cy.$$('body>div:first')
cy
.get('body').find('div:first').then(($div) => {
expect($div.get(0)).to.eq(div.get(0))
})
.get('body').command('find', 'div:first').then(($div) => {
expect($div.get(0)).to.eq(div.get(0))
})
})
it('does not add cmds to cy.commands queue', () => {
cy.command('get', 'body').then(() => {
const names = cy.queue.names()
expect(names).to.deep.eq(['visit', 'get', 'then'])
})
})
context('custom commands', () => {
beforeEach(() => {
Cypress.Commands.add('dashboard.selectWindows', () => {
cy
.get('[contenteditable]')
.first()
})
Cypress.Commands.add('login', { prevSubject: true }, (subject, email) => {
cy
.wrap(subject.find('input:first'))
.type(email)
})
})
it('works with custom commands', () => {
const input = cy.$$('input:first')
cy
.get('input:first')
.parent()
.command('login', 'brian@foo.com')
.then(($input) => {
expect($input.get(0)).to.eq(input.get(0))
})
})
it('works with namespaced commands', () => {
const ce = cy.$$('[contenteditable]').first()
cy
.command('dashboard.selectWindows').then(($ce) => {
expect($ce.get(0)).to.eq(ce.get(0))
})
})
it('throws when attempting to add an existing command', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.eq('`Cypress.Commands.add()` is used to create new commands, but `get` is an existing Cypress command.\n\nPlease use `Cypress.Commands.overwrite()` if you would like to overwrite an existing command.\n')
expect(err.docsUrl).to.eq('https://on.cypress.io/custom-commands')
done()
})
Cypress.Commands.add('get', () => {
cy
.get('[contenteditable]')
.first()
})
})
it('throws when attempting to add an existing query', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.eq('`Cypress.Commands.addQuery()` is used to create new queries, but `get` is an existing Cypress command or query, or is reserved internally by Cypress.\n\n If you want to override an existing command or query, use `Cypress.Commands.overrideQuery()` instead.')
expect(err.docsUrl).to.eq('https://on.cypress.io/api/custom-queries')
done()
})
Cypress.Commands.addQuery('get', () => {
cy
.get('[contenteditable]')
.first()
})
})
it('allows calling .add with hover / mount', () => {
let calls = 0
Cypress.Commands.add('hover', () => {
calls++
})
Cypress.Commands.add('mount', () => {
calls++
})
cy.mount()
cy.hover()
cy.then(() => {
expect(calls).to.eq(2)
})
})
it('throws when attempting to add a command with the same name as an internal function', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.eq('`Cypress.Commands.add()` cannot create a new command named `addCommand` because that name is reserved internally by Cypress.')
expect(err.docsUrl).to.eq('https://on.cypress.io/custom-commands')
done()
})
Cypress.Commands.add('addCommand', () => {
cy
.get('[contenteditable]')
.first()
})
})
it('throws when attempting to add a query with the same name as an internal function', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.eq('`Cypress.Commands.addQuery()` cannot create a new query named `addCommand` because that name is reserved internally by Cypress.')
expect(err.docsUrl).to.eq('https://on.cypress.io/api/custom-queries')
done()
})
Cypress.Commands.addQuery('addCommand', () => {
cy
.get('[contenteditable]')
.first()
})
})
})
context('errors', () => {
it('throws when cannot find command by name', (done) => {
cy.on('fail', (err) => {
const cmds = _.keys(Cypress.Chainer.prototype)
expect(cmds.length).to.be.gt(1)
expect(err.message).to.eq(`Could not find a command for: \`fooDoesNotExist\`.\n\nAvailable commands are: \`${cmds.join('`, `')}\`.\n`)
expect(err.docsUrl).to.eq('https://on.cypress.io/api')
done()
})
cy.get('body').command('fooDoesNotExist', 'bar', 'baz')
})
})
})