forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.cy.js
123 lines (106 loc) · 4.06 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
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
/// <reference types="Cypress" />
describe('Browser notifications', () => {
it('are supported by the test browser', () => {
cy.visit('index.html')
cy.window().should('have.property', 'Notification').should('be.a', 'function')
})
it('shows alert if the browser does not support notifications', () => {
cy.visit('index.html', {
onBeforeLoad (win) {
delete win.Notification
},
})
cy.on('window:alert', cy.stub().as('alerted'))
cy.get('button').click()
cy.get('@alerted').should('have.been.calledOnce')
.and('have.been.calledWith', 'This browser does not support desktop notification')
})
it('creates Notification if was previously granted', () => {
// see cy.visit options in https://on.cypress.io/visit
cy.visit('index.html', {
onBeforeLoad (win) {
// https://on.cypress.io/stub
cy.stub(win.Notification, 'permission', 'granted')
cy.stub(win, 'Notification').as('Notification')
},
})
cy.get('button').click()
cy.get('@Notification')
.should('have.been.calledWithNew')
.and('have.been.calledWithExactly', 'Permission was granted before')
})
it('asks for permission first, then shows notification if granted', () => {
cy.visit('index.html', {
onBeforeLoad (win) {
cy.stub(win.Notification, 'permission', 'unknown')
cy.stub(win.Notification, 'requestPermission').resolves('granted').as('ask')
cy.stub(win, 'Notification').as('Notification')
},
})
cy.get('button').click()
cy.get('@ask')
.should('have.been.calledOnce')
.and('have.been.calledBefore', cy.get('@Notification'))
})
it('asks for permission first, does nothing if denied', () => {
cy.visit('index.html', {
onBeforeLoad (win) {
cy.stub(win.Notification, 'permission', 'unknown')
cy.stub(win.Notification, 'requestPermission').resolves('denied').as('ask')
cy.stub(win, 'Notification').as('Notification')
},
})
cy.get('button').click()
cy.get('@ask').should('have.been.calledOnce')
cy.get('@Notification').should('not.have.been.called')
})
it('does not show notification if permission was denied before', () => {
cy.visit('index.html', {
onBeforeLoad (win) {
cy.stub(win.Notification, 'permission', 'denied')
cy.stub(win.Notification, 'requestPermission').resolves('denied').as('ask')
cy.stub(win, 'Notification').as('Notification')
},
})
cy.get('button').click()
cy.get('@Notification').should('not.have.been.called')
})
it('spying on Notification', () => {
cy.visit('index.html', {
onBeforeLoad (win) {
// the application checks if the permission was granted
// using the property Notification.permission === 'granted'
// which Sinon still supports, although it is marked deprecated
cy.stub(win.Notification, 'permission', 'granted')
cy.spy(win, 'Notification').as('Notification')
},
})
cy.get('button').click()
cy.get('@Notification')
.should('have.been.calledWithNew')
.and('have.been.calledWithExactly', 'Permission was granted before')
})
it('spying on Notification via a workaround', () => {
cy.visit('index.html', {
onBeforeLoad (win) {
// let's wrap Notification constructor
// to make sure it is always called with "new" keyword
const _Notification = win.Notification
win.Notification = function MockNotification (text) {
// use "new" when calling true Notification
return new _Notification(text)
}
// and copy the rest of the important properties
win.Notification.requestPermission = _Notification.requestPermission
win.Notification.permission = 'granted'
// now spy on the wrapped Notification method
cy.spy(win, 'Notification').as('Notification')
},
})
cy.get('button').click()
cy.get('@Notification')
.should('have.been.calledWithNew')
.and('have.been.calledWithExactly', 'Permission was granted before')
.and('have.been.calledWithNew')
})
})