Skip to content

Add tests for event listeners and attach #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"parser": "espree",
"parserOptions": {
"ecmaVersion": 8
},
"env": {
"mocha": true
},
Expand Down
55 changes: 55 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,59 @@ describe('file-attachment', function() {
assert.equal('/s3/saved.txt', attachment.href)
})
})

describe('element', function() {
let fileAttachment
beforeEach(function() {
document.body.innerHTML = `<file-attachment></file-attachment>`

fileAttachment = document.querySelector('file-attachment')
})

afterEach(function() {
document.body.innerHTML = ''
})

it('attaches files via .attach', async function() {
const listener = once('file-attachment-accepted')

const dataTransfer = new DataTransfer()
const file = new File(['hubot'], 'test.txt', {type: 'text/plain'})
dataTransfer.items.add(file)
fileAttachment.attach(dataTransfer)

const event = await listener
assert.equal('test.txt', event.detail.attachments[0].file.name)
})

it('attaches files via drop', async function() {
const listener = once('file-attachment-accepted')

const dataTransfer = new DataTransfer()
const file = new File(['hubot'], 'test.txt', {type: 'text/plain'})
dataTransfer.items.add(file)
const dropEvent = new DragEvent('drop', {bubbles: true, dataTransfer})
fileAttachment.dispatchEvent(dropEvent)

const event = await listener
assert.equal('test.txt', event.detail.attachments[0].file.name)
})

it('attaches images via paste', async function() {
const listener = once('file-attachment-accepted')

const dataTransfer = new DataTransfer()
const file = new File(['hubot'], 'test.png', {type: 'image/png'})
dataTransfer.items.add(file)
const dropEvent = new ClipboardEvent('paste', {bubbles: true, clipboardData: dataTransfer})
fileAttachment.dispatchEvent(dropEvent)

const event = await listener
assert.equal('test.png', event.detail.attachments[0].file.name)
})
})
})

function once(eventName) {
return new Promise(resolve => document.addEventListener(eventName, resolve, {once: true}))
}