weex-vdom-tester
Advanced tools
Comparing version 0.1.0 to 0.1.1
var ref = require('./document'); | ||
var Document = ref.Document; | ||
var Element = ref.Element; | ||
var ref$1 = require('./runtime'); | ||
@@ -9,3 +10,7 @@ var Runtime = ref$1.Runtime; | ||
var Instance = function Instance (runtime) { | ||
this._runtime = runtime || new Runtime() | ||
if (!(runtime instanceof Runtime)) { | ||
console.error("[instance] can not create a instance without runtime") | ||
return | ||
} | ||
this._runtime = runtime | ||
this._target = this._runtime.target | ||
@@ -21,4 +26,4 @@ this.id = this._runtime._genInstanceId().toString() | ||
this.history = { | ||
callNative: [], | ||
callJS: [], | ||
callNative: [], | ||
callJS: [], | ||
refresh: [] | ||
@@ -88,2 +93,20 @@ } | ||
} | ||
if (typeof code !== 'string' || !code) { | ||
console.error("[instance] can not createInstance without code") | ||
return | ||
} | ||
try { | ||
clonePlainObject(config || {}) | ||
} | ||
catch (e) { | ||
console.error(("[instance] \"config\" must be plain object or falsy value when createInstance\n" + e)) | ||
return | ||
} | ||
try { | ||
clonePlainObject(data || {}) | ||
} | ||
catch (e) { | ||
console.error(("[instance] \"data\" must be plain object or falsy value when createInstance\n" + e)) | ||
return | ||
} | ||
var target = this._target | ||
@@ -107,2 +130,9 @@ this.history.refresh.push({ | ||
} | ||
try { | ||
clonePlainObject(data || {}) | ||
} | ||
catch (e) { | ||
console.error(("[instance] \"data\" must be plain object or falsy value when refreshInstance\n" + e)) | ||
return | ||
} | ||
var target = this._target | ||
@@ -138,2 +168,24 @@ this.history.refresh.push({ | ||
} | ||
if (typeof ref !== 'string' || !ref) { | ||
console.error("[instance] \"ref\" must be truthy string in fireEvent") | ||
return | ||
} | ||
if (typeof type !== 'string' || !type) { | ||
console.error("[instance] event \"type\" must be truthy string") | ||
return | ||
} | ||
try { | ||
clonePlainObject(data || {}) | ||
} | ||
catch (e) { | ||
console.error(("[instance] \"data\" must be plain object or falsy value when fireEvent\n" + e)) | ||
return | ||
} | ||
try { | ||
clonePlainObject(domChanges || {}) | ||
} | ||
catch (e) { | ||
console.error(("[instance] \"domChanges\" must be plain object or falsy value when fireEvent\n" + e)) | ||
return | ||
} | ||
var target = this._target | ||
@@ -144,4 +196,4 @@ this.history.callJS.push({ | ||
args: clonePlainObject([ref, type, data, domChanges]) | ||
}) | ||
target.receiveTasks(this.id, [{ | ||
}) | ||
target.receiveTasks(this.id, [{ | ||
method: 'fireEvent', | ||
@@ -155,2 +207,13 @@ args: clonePlainObject([ref, type, data, domChanges]) | ||
} | ||
if (typeof funcId !== 'string' || !funcId) { | ||
console.error("[instance] \"funcId\" must be truthy string in fireEvent") | ||
return | ||
} | ||
try { | ||
clonePlainObject(data || {}) | ||
} | ||
catch (e) { | ||
console.error(("[instance] \"data\" must be plain object or falsy value when callback\n" + e)) | ||
return | ||
} | ||
var target = this._target | ||
@@ -160,5 +223,5 @@ this.history.callJS.push({ | ||
timestamp: Date.now(), | ||
args: clonePlainObject([funcId, data, ifLast]) | ||
}) | ||
target.receiveTasks(this.id, [{ | ||
args: clonePlainObject([funcId, data, ifLast]) | ||
}) | ||
target.receiveTasks(this.id, [{ | ||
method: 'callback', | ||
@@ -204,2 +267,6 @@ args: clonePlainObject([funcId, data, ifLast]) | ||
} | ||
if (!(element instanceof Element)) { | ||
console.error("[instance] you can only listen an Element but the target you want listen to is not") | ||
return | ||
} | ||
element.$addListener(this.doc, handler) | ||
@@ -206,0 +273,0 @@ }; |
@@ -5,2 +5,2 @@ exports.dom = require('./dom') | ||
exports.storage = require('./storage') | ||
exports.stream = require('./stream') | ||
// exports.stream = require('./stream') |
@@ -22,2 +22,7 @@ var ref = require('./env/default'); | ||
if (!jsFramework) { | ||
console.error("[runtime] no JS framework given, init failed.") | ||
return | ||
} | ||
// Init instance management. | ||
@@ -48,3 +53,3 @@ this.instanceMap = {} | ||
if (!logger.level) { | ||
logger.handler.apply(null, [level.substr(2).toLowerCase()].concat(args)) | ||
logger.handler.apply(null, [level.substr(2).toLowerCase()].concat(args)) | ||
} | ||
@@ -68,2 +73,6 @@ else if (LOG_LEVELS.indexOf(logger.level) === levelIndex) { | ||
Runtime.prototype.onlog = function onlog (type, handler) { | ||
if (!this.target) { | ||
console.error("[runtime] no JS framework given, this runtime is not working.") | ||
return | ||
} | ||
if (typeof type === 'function') { | ||
@@ -82,2 +91,6 @@ handler = type | ||
if (!this.target) { | ||
console.error("[runtime] no JS framework given, this runtime is not working.") | ||
return | ||
} | ||
this.loggers.some(function (logger, index) { | ||
@@ -93,2 +106,6 @@ if (logger.handler === handler) { | ||
if (!this.target) { | ||
console.error("[runtime] no JS framework given, this runtime is not working.") | ||
return | ||
} | ||
var target = this.target | ||
@@ -121,2 +138,6 @@ modules.forEach(function (module) { | ||
Runtime.prototype.registerComponents = function registerComponents (components) { | ||
if (!this.target) { | ||
console.error("[runtime] no JS framework given, this runtime is not working.") | ||
return | ||
} | ||
var target = this.target | ||
@@ -126,2 +147,6 @@ components.forEach(function (component) { return target.registerComponents([component]); }) | ||
Runtime.prototype._genInstanceId = function _genInstanceId () { | ||
if (!this.target) { | ||
console.error("[runtime] no JS framework given, this runtime is not working.") | ||
return | ||
} | ||
return this._nextInstanceId++ | ||
@@ -128,0 +153,0 @@ }; |
{ | ||
"name": "weex-vdom-tester", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Virtual-DOM test driver for Weex", | ||
@@ -39,3 +39,4 @@ "main": "index.js", | ||
"sinon": "^1.17.5", | ||
"sinon-chai": "^2.8.0" | ||
"sinon-chai": "^2.8.0", | ||
"weex-js-framework": "^0.17.0-alpha2" | ||
}, | ||
@@ -42,0 +43,0 @@ "dependencies": { |
@@ -9,2 +9,10 @@ # weex-vdom-tester | ||
In Weex virtual-DOM test driver, you can create a `Runtime` which can simulate a native app JavaScript runtime. The `Runtime` instance can be initialized with a Weex JS framework like Vanilla, Vue, React etc. | ||
Then in the runtime you can run a `Instance` with JS Bundle which is based on the target framework. | ||
This can be used to test whether the framework work well. For example: make sure a certain JS Bundle could generate a certain "real" DOM tree in renderer as expect, or do a certain series of JS-bridge calls. | ||
See `test/case.js` for some use case. | ||
### Weex JS runtime APIs | ||
@@ -21,2 +29,4 @@ | ||
// Create a Weex JavaScript runtime for a certain Weex JS framework. | ||
// You can also simulate the native environment which includes | ||
// global env variables, native modules & components. | ||
const runtime = new Runtime(jsFramework, { | ||
@@ -29,2 +39,4 @@ // modules: DEFAULT_MODULES, | ||
// Listen `nativeLog` calls. | ||
// The `type` is in `['debug', 'log', 'info', 'warn', 'error']`. | ||
// If no `type` defined than it will listen all type of logs. | ||
runtime.onlog((type, args) => { ... }) | ||
@@ -35,2 +47,3 @@ runtime.onlog(type, (args) => { ... }) | ||
// Register more modules and components. | ||
// You can simulate all module APIs by this. | ||
runtime.registerModules({ | ||
@@ -42,2 +55,13 @@ x: { | ||
}) | ||
// Also if the vdom tester have implemented the module, you can just pass | ||
// an Array of method names to register them. | ||
runtime.registerModules({ | ||
"modal": [ | ||
"alert", | ||
"toast", | ||
"prompt", | ||
"confirm" | ||
] | ||
}) | ||
// Register native components. | ||
runtime.registerComponents([ | ||
@@ -54,6 +78,10 @@ x: { type: 'x', append: true } | ||
// Create a Weex instance in a certain runtime. | ||
// The `runtime` parameter is optional. | ||
const instance = new Instance(runtime) | ||
// Each instance has an id. | ||
instance.id | ||
// Each instance has a document object to record what it will render. | ||
instance.doc | ||
// Send commands to Weex JS runtime about this instance. | ||
// See more details in Weex documentations. | ||
instance.$create(code, config, data) | ||
@@ -94,10 +122,1 @@ instance.$refresh(data) | ||
``` | ||
### Weex advanced APIs (not work yet) | ||
```javascript | ||
runtime.test([inputFileOrCode, outputFileOrCode]) | ||
runtime.test([inputFileOrCode, ...refreshs, outputFileOrCode]) | ||
runtime.test([inputFileOrCode, ...events, outputFileOrCode]) | ||
runtime.test([inputFileOrCode, ...mockCallbacks, outputFileOrCode]) | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77572
1117
116
8