Skip to content

Commit 41032a7

Browse files
authored
Merge pull request #3 from github/tests
Add tests for event listeners and attach
2 parents 3127666 + 06902e6 commit 41032a7

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

test/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"parser": "espree",
3+
"parserOptions": {
4+
"ecmaVersion": 8
5+
},
36
"env": {
47
"mocha": true
58
},

test/test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,59 @@ describe('file-attachment', function() {
5656
assert.equal('/s3/saved.txt', attachment.href)
5757
})
5858
})
59+
60+
describe('element', function() {
61+
let fileAttachment
62+
beforeEach(function() {
63+
document.body.innerHTML = `<file-attachment></file-attachment>`
64+
65+
fileAttachment = document.querySelector('file-attachment')
66+
})
67+
68+
afterEach(function() {
69+
document.body.innerHTML = ''
70+
})
71+
72+
it('attaches files via .attach', async function() {
73+
const listener = once('file-attachment-accepted')
74+
75+
const dataTransfer = new DataTransfer()
76+
const file = new File(['hubot'], 'test.txt', {type: 'text/plain'})
77+
dataTransfer.items.add(file)
78+
fileAttachment.attach(dataTransfer)
79+
80+
const event = await listener
81+
assert.equal('test.txt', event.detail.attachments[0].file.name)
82+
})
83+
84+
it('attaches files via drop', async function() {
85+
const listener = once('file-attachment-accepted')
86+
87+
const dataTransfer = new DataTransfer()
88+
const file = new File(['hubot'], 'test.txt', {type: 'text/plain'})
89+
dataTransfer.items.add(file)
90+
const dropEvent = new DragEvent('drop', {bubbles: true, dataTransfer})
91+
fileAttachment.dispatchEvent(dropEvent)
92+
93+
const event = await listener
94+
assert.equal('test.txt', event.detail.attachments[0].file.name)
95+
})
96+
97+
it('attaches images via paste', async function() {
98+
const listener = once('file-attachment-accepted')
99+
100+
const dataTransfer = new DataTransfer()
101+
const file = new File(['hubot'], 'test.png', {type: 'image/png'})
102+
dataTransfer.items.add(file)
103+
const dropEvent = new ClipboardEvent('paste', {bubbles: true, clipboardData: dataTransfer})
104+
fileAttachment.dispatchEvent(dropEvent)
105+
106+
const event = await listener
107+
assert.equal('test.png', event.detail.attachments[0].file.name)
108+
})
109+
})
59110
})
111+
112+
function once(eventName) {
113+
return new Promise(resolve => document.addEventListener(eventName, resolve, {once: true}))
114+
}

0 commit comments

Comments
 (0)