forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag_n_drop_spec.js
151 lines (130 loc) · 4.58 KB
/
drag_n_drop_spec.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
// This recipe shows how to use cy.trigger to test
// drag-n-drop interactions
// There are 2 different games tested:
// one utilizing mouse events and
// one utilizing drag events
describe('Drag n Drop', function () {
// This tests a puzzle that uses dragula.js, which, under the hood,
// binds to mousedown, mousemove, and mouseup events
describe('puzzle using mouse events', function () {
// A drag and drop action is made up of a mousedown event,
// multiple mousemove events, and a mouseup event
// (we can get by with just one mousemove event for our test,
// even though there would be dozens in a normal interaction)
//
// For the mousedown, we specify { which: 1 } because dragula will
// ignore a mousedown if it's not a left click
//
// On mousemove, we need to specify where we're moving
// with clientX and clientY
function movePiece (number, x, y) {
cy.get(`.piece-${number}`)
.trigger('mousedown', { which: 1 })
.trigger('mousemove', { clientX: x, clientY: y })
.trigger('mouseup', { force: true })
}
function completePuzzle (correctly) {
movePiece(1, 340, correctly ? 130 : 200)
movePiece(2, 410, 130)
movePiece(3, 480, 130)
movePiece(4, 340, correctly ? 200 : 130)
movePiece(5, 410, 200)
movePiece(6, 480, 200)
movePiece(7, 340, 270)
movePiece(8, 410, 270)
movePiece(9, 480, 270)
}
beforeEach(function () {
cy.viewport(550, 350)
cy.visit('/puzzle.html')
})
it('moves the piece when dragged to valid place', function () {
movePiece(1, 340, 130)
cy.get('.pieces li').eq(3).find('span').should('not.exist')
cy.get('.places li').eq(0).find('span').should('have.class', 'piece-1')
})
it('does not move piece when dragged to invalid place', function () {
movePiece(1, 0, 0)
cy.get('.pieces li').eq(3).find('span').should('have.class', 'piece-1')
})
it('does not move piece when dragged to occupied place', function () {
movePiece(1, 340, 130)
movePiece(2, 340, 130)
cy.get('.places li').eq(0).find('span').should('have.class', 'piece-1')
cy.get('.pieces li').eq(7).find('span').should('have.class', 'piece-2')
})
it('allows moving piece back to open spot', function () {
movePiece(1, 340, 130)
movePiece(1, 70, 180)
cy.get('.pieces li').eq(3).find('span').should('have.class', 'piece-1')
})
it('shows error message when puzzle is completed incorrectly', function () {
completePuzzle(false)
cy.get('.notice')
.should('have.class', 'error')
.and('have.text', 'Not quite right. Please try again')
})
it('shows success message when puzzle is completed correctly', function () {
completePuzzle(true)
cy.get('.notice')
.should('have.class', 'success')
.and('have.text', 'Success!')
})
})
// This tests a simple game that utilizes the native drag events
// like dragstart, dragenter, dragleave, and drop
describe('game using drag events', function () {
function dropBallInHoop (index) {
cy.get('.balls img').first()
.trigger('dragstart')
cy.get('.hoop')
.trigger('drop')
}
beforeEach(function () {
cy.clock()
cy.viewport(400, 350)
cy.visit('/basketball.html')
})
it('shows error message when time runs out', function () {
cy.tick(10000)
cy.get('main')
.should('have.class', 'error')
.find('p')
.should('have.text', 'Time\'s up!')
})
it('highlights hoop when ball is dragged over it', function () {
cy.get('.hoop')
.trigger('dragenter')
.should('have.class', 'over')
})
it('unhighlights hoop when ball is dragged out of it', function () {
cy.get('.hoop')
.trigger('dragenter')
.should('have.class', 'over')
.trigger('dragleave')
.should('not.have.class', 'over')
})
it('unhighlights hoop when ball is dropped in it', function () {
cy.get('.hoop')
.trigger('dragenter')
.should('have.class', 'over')
dropBallInHoop()
cy.get('.hoop')
.should('not.have.class', 'over')
})
it('removes ball when it is dropped in hoop', function () {
dropBallInHoop()
cy.get('.balls img').should('have.length', 3)
})
it('shows success message when all the balls are dropped in hoop', function () {
dropBallInHoop()
dropBallInHoop()
dropBallInHoop()
dropBallInHoop()
cy.get('main')
.should('have.class', 'success')
.find('p')
.should('have.text', 'Success!')
})
})
})