-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
tests: improve drag-and-drop coverage #9314
Changes from 4 commits
4138987
5ec04a4
ca09df1
159ed39
e77e069
03f67ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,8 +7,6 @@ | |||||
|
||||||
/* eslint-env jest */ | ||||||
|
||||||
const assert = require('assert'); | ||||||
|
||||||
const testHelpers = require('./test-helpers.js'); | ||||||
|
||||||
// Called before other src import so code that relies on `document` and | ||||||
|
@@ -17,8 +15,12 @@ testHelpers.setupJsDomGlobals(); | |||||
|
||||||
const DragAndDrop = require('../app/src/drag-and-drop.js'); | ||||||
|
||||||
function assertUIReset() { | ||||||
assert.ok(!document.querySelector('.drop_zone').classList.contains('dropping')); | ||||||
function createCustomEvent(type, key, value) { | ||||||
const customDropEvent = new window.CustomEvent(type); | ||||||
if (key && value) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right now it's a bit odd that the callers do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think removing this fn entirely would be better.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah ignore what I was saying, let's go with @connorjclark ! |
||||||
customDropEvent[key] = value; | ||||||
} | ||||||
return customDropEvent; | ||||||
} | ||||||
|
||||||
describe('DragAndDrop', () => { | ||||||
|
@@ -29,31 +31,59 @@ describe('DragAndDrop', () => { | |||||
|
||||||
afterEach(testHelpers.cleanupJsDomGlobals); | ||||||
|
||||||
// TODO: test drop event on document. Callback is not getting called | ||||||
// because jsdom doesn't support clipboard API: https://github.com/tmpvar/jsdom/issues/1568/. | ||||||
it.skip('document responds to drag and drop events', done => { | ||||||
const callback = _ => { | ||||||
assert.ok(true, 'file change callback is called after drop event'); | ||||||
done(); | ||||||
}; | ||||||
it('document responds to drop event with file', () => { | ||||||
const mockCallback = jest.fn(); | ||||||
new DragAndDrop(mockCallback); | ||||||
|
||||||
// create custom drop event with mock files in dataTransfer | ||||||
document.dispatchEvent(createCustomEvent('drop', 'dataTransfer', { | ||||||
files: ['mock file'], | ||||||
})); | ||||||
expect(mockCallback).toBeCalledWith('mock file'); | ||||||
}); | ||||||
|
||||||
it('document responds to drop event without file', () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will fix it. thanks for suggestion. |
||||||
const mockCallback = jest.fn(); | ||||||
new DragAndDrop(mockCallback); | ||||||
|
||||||
document.dispatchEvent(createCustomEvent('drop', 'dataTransfer', null)); | ||||||
expect(mockCallback).not.toBeCalled(); | ||||||
}); | ||||||
|
||||||
it('document responds to dragover event with file', () => { | ||||||
const mockCallback = jest.fn(); | ||||||
new DragAndDrop(mockCallback); | ||||||
|
||||||
const dragoverEvent = createCustomEvent('dragover', 'dataTransfer', { | ||||||
files: ['mock file'], | ||||||
}); | ||||||
document.dispatchEvent(dragoverEvent); | ||||||
expect(dragoverEvent.dataTransfer.dropEffect).toEqual('copy'); | ||||||
}); | ||||||
|
||||||
new DragAndDrop(callback); | ||||||
it('document responds to dragover event without file', () => { | ||||||
const mockCallback = jest.fn(); | ||||||
new DragAndDrop(mockCallback); | ||||||
|
||||||
document.dispatchEvent(new window.CustomEvent('drop')); | ||||||
const dragoverEvent = createCustomEvent('dragover', 'dataTransfer', null); | ||||||
document.dispatchEvent(dragoverEvent); | ||||||
expect(dragoverEvent.dataTransfer).toBeUndefined(); | ||||||
}); | ||||||
|
||||||
it('document responds to drag and drop events', () => { | ||||||
// eslint-disable-next-line no-unused-vars | ||||||
const dragAndDrop = new DragAndDrop(); | ||||||
it('document responds to mouseleave event when not dragging', () => { | ||||||
new DragAndDrop(jest.fn); | ||||||
|
||||||
document.dispatchEvent(new window.CustomEvent('mouseleave')); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we align on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, I will made the change align it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. follow up with #9314 (comment) |
||||||
assertUIReset(); | ||||||
expect(document.querySelector('.drop_zone').classList.contains('dropping')).toBeFalsy(); | ||||||
}); | ||||||
|
||||||
it('document responds to mouseleave and dragenter events', () => { | ||||||
new DragAndDrop(jest.fn); | ||||||
|
||||||
document.dispatchEvent(new window.CustomEvent('dragenter')); | ||||||
assert.ok(document.querySelector('.drop_zone').classList.contains('dropping')); | ||||||
expect(document.querySelector('.drop_zone').classList.contains('dropping')).toBeTruthy(); | ||||||
|
||||||
// TODO: see note above about drop event testing. | ||||||
// document.dispatchEvent(new window.CustomEvent('drop')); | ||||||
// assertUIReset(); | ||||||
document.dispatchEvent(new window.CustomEvent('mouseleave')); | ||||||
expect(document.querySelector('.drop_zone').classList.contains('dropping')).toBeFalsy(); | ||||||
}); | ||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: WDYT about the caller passing
{key: value}
to make it a little clearer belowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you mean add a comment here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's ignore this comment in favor of #9314 (comment)