forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.cy.js
95 lines (77 loc) · 2.48 KB
/
spec.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
/// <reference types="cypress" />
import 'cypress-wait-until'
import { plan } from 'cypress-expect-n-assertions'
describe('Window confirm', () => {
// NOTE: skipping this test because it does not wait correctly for assertion to execute
it.skip('calls window confirm', () => {
cy.visit('index.html')
cy.on('window:confirm', (message) => {
expect(message).to.equal('Are you sure?')
})
cy.get('#click').click()
})
it('waits for window confirm to happen using stub', () => {
cy.visit('index.html')
cy.on('window:confirm', cy.stub().as('confirm'))
cy.get('#click').click()
// test automatically waits for the stub
cy.get('@confirm').should('have.been.calledWith', 'Are you sure?')
})
it('waits for window confirm to happen using variable', () => {
cy.visit('index.html')
let called
cy.on('window:confirm', (message) => {
expect(message).to.equal('Are you sure?')
called = true
})
cy.get('#click').click()
// test automatically waits for the variable "called"
cy.wrap(null).should(() => {
expect(called).to.be.true
})
})
it('waits for window confirm to happen using promises', () => {
cy.visit('index.html')
let calledPromise = new Promise((resolve) => {
cy.on('window:confirm', (message) => {
expect(message).to.equal('Are you sure?')
resolve()
})
})
// test automatically waits for the promise
cy.get('#click').click().then(() => calledPromise)
})
it('waits for window confirm to happen using cy.waitUntil', () => {
cy.visit('index.html')
let called
cy.on('window:confirm', (message) => {
expect(message).to.equal('Are you sure?')
called = true
})
cy.get('#click').click()
// see https://github.com/NoriSte/cypress-wait-until
cy.waitUntil(() => called)
})
context('plan', () => {
it('waits for window confirm to happen using waitUntil', () => {
plan(1)
cy.visit('index.html')
let called
cy.on('window:confirm', (message) => {
expect(message).to.equal('Are you sure?')
called = true
})
cy.get('#click').click()
// see https://github.com/NoriSte/cypress-wait-until
cy.waitUntil(() => called)
})
it('waits for planned number of assertion to run', () => {
plan(1)
cy.visit('index.html')
cy.on('window:confirm', (message) => {
expect(message).to.equal('Are you sure?')
})
cy.get('#click').click()
})
})
})