Skip to content

Commit

Permalink
Merge pull request #164 from mnoble01/master
Browse files Browse the repository at this point in the history
Move test-support helpers to addon-test-support
  • Loading branch information
dgavey authored Sep 4, 2020
2 parents 2f29c0e + 608e218 commit c2f0327
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 146 deletions.
42 changes: 42 additions & 0 deletions addon-test-support/helpers/data-transfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import EmberObject from '@ember/object';

var c = EmberObject.extend({
getData: function() {
return this.get('payload');
},

setData: function(dataType,payload) {
this.set("data", {dataType: dataType, payload: payload});
}
});

c.reopenClass({
makeMockEvent: function(payload) {
var transfer = this.create({payload: payload});
var res = {dataTransfer: transfer};
res.preventDefault = function() {
console.log('prevent default');
};
res.stopPropagation = function() {
console.log('stop propagation');
};
return res;
},

createDomEvent: function(type) {
var event = document.createEvent("CustomEvent");
event.initCustomEvent(type, true, true, null);
event.dataTransfer = {
data: {},
setData: function(type, val){
this.data[type] = val;
},
getData: function(type){
return this.data[type];
}
};
return event;
}
});

export default c;
49 changes: 49 additions & 0 deletions addon-test-support/helpers/drag-drop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {find, triggerEvent} from '@ember/test-helpers';
import MockEvent from './mock-event';

async function dragOver(dropSelector, moves) {
moves = moves || [[{ clientX: 1, clientY: 1 }, dropSelector]];

for (const move of moves) {
const position = move[0] || false;
const selector = move[1] || false;
const event = new MockEvent(position);
await triggerEvent(selector || dropSelector, 'dragover', event);
}
}

async function drop(dragSelector, dragEvent, options) {
let { drop: dropSelector, dropEndOptions, dragOverMoves } = options;

let dropElement = await find(dropSelector);
if (!dropElement) {
throw(`There are no drop targets by the given selector: '${dropSelector}'`);
}

await dragOver(dropSelector, dragOverMoves);

if (options.beforeDrop) {
await options.beforeDrop.call();
}

let event = new MockEvent().useDataTransferData(dragEvent);
await triggerEvent(dropSelector, 'drop', event);

return await triggerEvent(dragSelector, 'dragend', dropEndOptions);
}

export async function drag(dragSelector, options = {}) {
let dragEvent = new MockEvent(options.dragStartOptions);

await triggerEvent(dragSelector, 'mouseover');

await triggerEvent(dragSelector, 'dragstart', dragEvent);

if (options.afterDrag) {
await options.afterDrag.call();
}

if (options.drop) {
await drop(dragSelector, dragEvent, options);
}
}
50 changes: 50 additions & 0 deletions addon-test-support/helpers/mock-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class DataTransfer {
constructor() {
this.data = {};
}

setData(type, value) {
this.data[type] = value;
return this;
}

getData(type = "Text") {
return this.data[type];
}

setDragImage() {
}
}

export default class MockEvent {
constructor(options = {}) {
this.dataTransfer = new DataTransfer();
this.dataTransfer.setData('Text', options.dataTransferData);
this.setProperties(options)
}

useDataTransferData(otherEvent) {
this.dataTransfer.setData('Text', otherEvent.dataTransfer.getData());
return this;
}

setProperties(props) {
for (let prop in props) {
this[prop] = props[prop];
}
return this;
}

preventDefault() {
}

stopPropagation() {
}
}

export function createDomEvent(type) {
let event = document.createEvent("CustomEvent");
event.initCustomEvent(type, true, true, null);
event.dataTransfer = new DataTransfer();
return event;
}
43 changes: 1 addition & 42 deletions test-support/helpers/data-transfer.js
Original file line number Diff line number Diff line change
@@ -1,42 +1 @@
import EmberObject from '@ember/object';

var c = EmberObject.extend({
getData: function() {
return this.get('payload');
},

setData: function(dataType,payload) {
this.set("data", {dataType: dataType, payload: payload});
}
});

c.reopenClass({
makeMockEvent: function(payload) {
var transfer = this.create({payload: payload});
var res = {dataTransfer: transfer};
res.preventDefault = function() {
console.log('prevent default');
};
res.stopPropagation = function() {
console.log('stop propagation');
};
return res;
},

createDomEvent: function(type) {
var event = document.createEvent("CustomEvent");
event.initCustomEvent(type, true, true, null);
event.dataTransfer = {
data: {},
setData: function(type, val){
this.data[type] = val;
},
getData: function(type){
return this.data[type];
}
};
return event;
}
});

export default c;
export { default } from 'ember-drag-drop/test-support/helpers/data-transfer';
52 changes: 2 additions & 50 deletions test-support/helpers/drag-drop.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,2 @@
import {find, triggerEvent} from '@ember/test-helpers';
import MockEvent from './mock-event';

async function dragOver(dropSelector, moves) {
moves = moves || [[{ clientX: 1, clientY: 1 }, dropSelector]];

for (const move of moves) {
const position = move[0] || false;
const selector = move[1] || false;
const event = new MockEvent(position);
await triggerEvent(selector || dropSelector, 'dragover', event);
}
}

async function drop(dragElement, dragEvent, options) {
let { drop: dropSelector, dropEndOptions, dragOverMoves } = options;

let dropElement = await find(dropSelector);
if (!dropElement) {
throw new Error(`There are no drop targets by the given selector: '${dropSelector}'`);
}

await dragOver(dropSelector, dragOverMoves);

if (options.beforeDrop) {
await options.beforeDrop.call();
}

let event = new MockEvent().useDataTransferData(dragEvent);
await triggerEvent(dropSelector, 'drop', event);

return await triggerEvent(dragElement, 'dragend', dropEndOptions);
}

export async function drag(dragSelector, options = {}) {
let dragEvent = new MockEvent(options.dragStartOptions);
const dragElement = document.querySelector(dragSelector);

await triggerEvent(dragSelector, 'mouseover');

await triggerEvent(dragSelector, 'dragstart', dragEvent);

if (options.afterDrag) {
await options.afterDrag.call();
}

if (options.drop) {
await drop(dragElement, dragEvent, options);
}
}
export { default } from 'ember-drag-drop/test-support/helpers/drag-drop';
export * from 'ember-drag-drop/test-support/helpers/drag-drop';
52 changes: 2 additions & 50 deletions test-support/helpers/mock-event.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,2 @@
class DataTransfer {
constructor() {
this.data = {};
}

setData(type, value) {
this.data[type] = value;
return this;
}

getData(type = "Text") {
return this.data[type];
}

setDragImage() {
}
}

export default class MockEvent {
constructor(options = {}) {
this.dataTransfer = new DataTransfer();
this.dataTransfer.setData('Text', options.dataTransferData);
this.setProperties(options)
}

useDataTransferData(otherEvent) {
this.dataTransfer.setData('Text', otherEvent.dataTransfer.getData());
return this;
}

setProperties(props) {
for (let prop in props) {
this[prop] = props[prop];
}
return this;
}

preventDefault() {
}

stopPropagation() {
}
}

export function createDomEvent(type) {
let event = document.createEvent("CustomEvent");
event.initCustomEvent(type, true, true, null);
event.dataTransfer = new DataTransfer();
return event;
}
export { default } from 'ember-drag-drop/test-support/helpers/mock-event';
export * from 'ember-drag-drop/test-support/helpers/mock-event';
2 changes: 1 addition & 1 deletion tests/integration/components/draggable-object-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { triggerEvent, find, render } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import MockEvent from '../../helpers/mock-event';
import MockEvent from 'ember-drag-drop/test-support/helpers/mock-event';

module('Integration | Component | draggable object', function(hooks){
setupRenderingTest(hooks);
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/components/helpers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import Coordinator from '../../../models/coordinator';
import { drag } from '../../helpers/drag-drop';
import { drag } from 'ember-drag-drop/test-support/helpers/drag-drop';
import { find, render } from '@ember/test-helpers';

module('Integration | Helpers', function(hooks) {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/components/sortable-objects-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { findAll, find, render } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { drag } from '../../helpers/drag-drop';
import { drag } from 'ember-drag-drop/test-support/helpers/drag-drop';
import { A } from '@ember/array';
import { w } from '@ember/string';

Expand Down
28 changes: 28 additions & 0 deletions tests/test-support-import-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { module, test } from 'qunit';
import * as oldDragDrop from 'dummy/tests/helpers/drag-drop';
import * as oldDataTransfer from 'dummy/tests/helpers/data-transfer';
import * as oldMockEvent from 'dummy/tests/helpers/mock-event';

import * as newDragDrop from 'ember-drag-drop/test-support/helpers/drag-drop';
import * as newDataTransfer from 'ember-drag-drop/test-support/helpers/data-transfer';
import * as newMockEvent from 'ember-drag-drop/test-support/helpers/mock-event';

module('test-support exports', function() {
test('helpers/drag-drop', function(assert) {
for (const [key, value] of Object.entries(oldDragDrop)) {
assert.deepEqual(value, newDragDrop[key]);
}
});

test('helpers/data-transfer', function(assert) {
for (const [key, value] of Object.entries(oldDataTransfer)) {
assert.deepEqual(value, newDataTransfer[key]);
}
});

test('helpers/mock-event', function(assert) {
for (const [key, value] of Object.entries(oldMockEvent)) {
assert.deepEqual(value, newMockEvent[key]);
}
});
});
2 changes: 1 addition & 1 deletion tests/unit/components/draggable-object-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { run } from '@ember/runloop';
import { A } from '@ember/array';
import { test, moduleForComponent } from 'ember-qunit';
import Coordinator from '../../../models/coordinator';
import MockEvent from '../../helpers/mock-event';
import MockEvent from 'ember-drag-drop/test-support/helpers/mock-event';
import { wrapper } from 'ember-drag-drop/utils/proxy-unproxy-objects';

const Thing = EmberObject.extend({});
Expand Down

0 comments on commit c2f0327

Please sign in to comment.