-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #339 from ro0gr/native-dom
Add native-dom mode
- Loading branch information
Showing
14 changed files
with
332 additions
and
40 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
addon/-private/execution_context/acceptance-native-events.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import ExecutionContext from './native-events-context'; | ||
import wait from 'ember-test-helpers/wait'; | ||
|
||
import { | ||
visit | ||
} from 'ember-native-dom-helpers'; | ||
|
||
export default function AcceptanceNativeEventsExecutionContext(pageObjectNode) { | ||
ExecutionContext.call(this, pageObjectNode); | ||
} | ||
|
||
AcceptanceNativeEventsExecutionContext.prototype = Object.create(ExecutionContext.prototype); | ||
|
||
AcceptanceNativeEventsExecutionContext.prototype.visit = function() { | ||
return visit(...arguments); | ||
}; | ||
|
||
AcceptanceNativeEventsExecutionContext.prototype.runAsync = function(cb) { | ||
(window.wait || wait)().then(() => { | ||
cb(this); | ||
}); | ||
|
||
return this.pageObjectNode; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
addon/-private/execution_context/integration-native-events.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ExecutionContext from './native-events-context'; | ||
|
||
import Ember from 'ember'; | ||
const { run } = Ember; | ||
|
||
export default function IntegrationNativeEventsExecutionContext(pageObjectNode, testContext) { | ||
ExecutionContext.call(this, pageObjectNode, testContext); | ||
} | ||
|
||
IntegrationNativeEventsExecutionContext.prototype = Object.create(ExecutionContext.prototype); | ||
|
||
IntegrationNativeEventsExecutionContext.prototype.visit = function() {}; | ||
|
||
IntegrationNativeEventsExecutionContext.prototype.runAsync = function(cb) { | ||
run(() => { | ||
cb(this); | ||
}); | ||
|
||
return this.pageObjectNode; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
addon/-private/execution_context/native-events-context.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import $ from '-jquery'; | ||
|
||
import { | ||
click, | ||
triggerEvent, | ||
keyEvent | ||
} from 'ember-native-dom-helpers'; | ||
|
||
import { | ||
guardMultiple, | ||
buildSelector, | ||
findClosestValue | ||
} from '../helpers'; | ||
import { | ||
fillElement | ||
} from './helpers'; | ||
import { | ||
ELEMENT_NOT_FOUND, | ||
throwBetterError | ||
} from '../better-errors'; | ||
|
||
const KEYBOARD_EVENT_TYPES = ['keydown', 'keypress', 'keyup']; | ||
|
||
export default function ExecutionContext(pageObjectNode, testContext) { | ||
this.pageObjectNode = pageObjectNode; | ||
this.testContext = testContext; | ||
} | ||
|
||
ExecutionContext.prototype = { | ||
run(cb) { | ||
return cb(this); | ||
}, | ||
|
||
runAsync() { | ||
throw new Error('not implemented'); | ||
}, | ||
|
||
click(selector, container) { | ||
const el = this.$(selector, container)[0]; | ||
click(el); | ||
}, | ||
|
||
fillIn(selector, container, options, content) { | ||
let elements = this.$(selector, container).toArray(); | ||
|
||
elements.forEach((el) => { | ||
fillElement(el, content, { | ||
selector, | ||
pageObjectNode: this.pageObjectNode, | ||
pageObjectKey: options.pageObjectKey | ||
}); | ||
|
||
triggerEvent(el, 'input'); | ||
triggerEvent(el, 'change'); | ||
}); | ||
}, | ||
|
||
$(selector, container) { | ||
if (container) { | ||
return $(selector, container); | ||
} else { | ||
// @todo: we should fixed usage of private `_element` | ||
// after https://github.com/emberjs/ember-test-helpers/issues/184 is resolved | ||
let testsContainer = this.testContext ? | ||
this.testContext._element : | ||
'#ember-testing'; | ||
|
||
return $(selector, testsContainer); | ||
} | ||
}, | ||
|
||
triggerEvent(selector, container, eventName, eventOptions) { | ||
const element = this.$(selector, container)[0]; | ||
|
||
// `keyCode` is a deprecated property. | ||
// @see: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode | ||
// Due to this deprecation `ember-native-dom-helpers` doesn't accept `keyCode` as a `KeyboardEvent` option. | ||
if (typeof eventOptions.key === 'undefined' && typeof eventOptions.keyCode !== 'undefined') { | ||
eventOptions.key = eventOptions.keyCode.toString(); | ||
delete eventOptions.keyCode; | ||
} | ||
|
||
if (KEYBOARD_EVENT_TYPES.indexOf(eventName) > -1) { | ||
keyEvent(element, eventName, eventOptions.key, eventOptions); | ||
} else { | ||
triggerEvent(element, eventName, eventOptions); | ||
} | ||
}, | ||
|
||
assertElementExists(selector, options) { | ||
let container = options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer'); | ||
|
||
let result = this.$(selector, container); | ||
|
||
if (result.length === 0) { | ||
throwBetterError( | ||
this.pageObjectNode, | ||
options.pageObjectKey, | ||
ELEMENT_NOT_FOUND, | ||
{ selector } | ||
); | ||
} | ||
}, | ||
|
||
find(selector, options) { | ||
let container = options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer'); | ||
|
||
selector = buildSelector(this.pageObjectNode, selector, options); | ||
|
||
let result = this.$(selector, container); | ||
|
||
guardMultiple(result, selector, options.multiple); | ||
|
||
return result; | ||
}, | ||
|
||
findWithAssert(selector, options) { | ||
let container = options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer'); | ||
|
||
selector = buildSelector(this.pageObjectNode, selector, options); | ||
|
||
let result = this.$(selector, container); | ||
|
||
if (result.length === 0) { | ||
throwBetterError( | ||
this.pageObjectNode, | ||
options.pageObjectKey, | ||
ELEMENT_NOT_FOUND, | ||
{ selector } | ||
); | ||
} | ||
|
||
guardMultiple(result, selector, options.multiple); | ||
|
||
return result; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,21 @@ | ||
export { findElement } from './-private/extend/find-element'; | ||
export { findElementWithAssert } from './-private/extend/find-element-with-assert'; | ||
export { buildSelector, getContext, fullScope } from './-private/helpers'; | ||
export { register as registerExecutionContext } from './-private/execution_context'; | ||
import { register as registerExecutionContext } from './-private/execution_context'; | ||
|
||
import IntegrationNativeEventsContext from './-private/execution_context/integration-native-events'; | ||
import AcceptanceNativeEventsContext from './-private/execution_context/acceptance-native-events'; | ||
import IntegrationEmberContext from './-private/execution_context/integration'; | ||
import AcceptanceEmberContext from './-private/execution_context/acceptance'; | ||
|
||
function useNativeEvents(flag = true) { | ||
if (flag) { | ||
registerExecutionContext('integration', IntegrationNativeEventsContext); | ||
registerExecutionContext('acceptance', AcceptanceNativeEventsContext); | ||
} else { | ||
registerExecutionContext('integration', IntegrationEmberContext); | ||
registerExecutionContext('acceptance', AcceptanceEmberContext); | ||
} | ||
} | ||
|
||
export { registerExecutionContext, useNativeEvents }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.