Skip to content

Commit

Permalink
* [jsfm] update test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Tancy committed Sep 8, 2016
1 parent a677931 commit db14f21
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 24 deletions.
1 change: 1 addition & 0 deletions html5/test/unit/default/app/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { expect } = chai
chai.use(sinonChai)

global.callNative = function () {}
global.callAddElement = function () {}

import * as bundle from '../../../../default/app/bundle'
import * as register from '../../../../default/app/register'
Expand Down
1 change: 1 addition & 0 deletions html5/test/unit/default/app/ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { expect } = chai
chai.use(sinonChai)

global.callNative = function () {}
global.callAddElement = function () {}

import * as ctrl from '../../../../default/app/ctrl'
import Differ from '../../../../default/app/differ'
Expand Down
11 changes: 11 additions & 0 deletions html5/test/unit/default/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ const { expect } = chai
chai.use(sinonChai)

global.callNative = function () {}
global.callAddElement = function () {}

import App from '../../../../default/app'
import { Element } from '../../../../vdom'

describe('App Instance', () => {
const oriCallNative = global.callNative
const oriCallAddElement = global.callAddElement
const callNativeSpy = sinon.spy()
const callAddElementSpy = sinon.spy()
let app

before(() => {
Expand All @@ -22,6 +25,13 @@ describe('App Instance', () => {
app.callbacks[callbackId] && app.callbacks[callbackId]()
}
}
global.callAddElement = (name, ref, json, index, callbackId) => {
callAddElementSpy(name, ref, json, index, callbackId)
/* istanbul ignore if */
if (callbackId !== '-1') {
app.callbacks[callbackId] && app.callbacks[callbackId]()
}
}
})

beforeEach(() => {
Expand All @@ -30,6 +40,7 @@ describe('App Instance', () => {

after(() => {
global.callNative = oriCallNative
global.callAddElement = oriCallAddElement
})

describe('normal check', () => {
Expand Down
1 change: 0 additions & 1 deletion html5/test/unit/default/helper/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Document.prototype.addElement = function (parentRef, config, index) {

function appendToDoc (doc, config, parentRef, index) {
const parent = doc.refs[parentRef]

const el = new Element(config)
doc.refs[el.ref] = el
el.parentRef = parentRef
Expand Down
47 changes: 29 additions & 18 deletions html5/test/unit/default/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ function clearRefs (json) {

describe('framework entry', () => {
const oriCallNative = global.callNative
const oriCallAddElement = global.callAddElement
const callNativeSpy = sinon.spy()
const callAddElementSpy = sinon.spy()
const instanceId = Date.now() + ''

before(() => {
Expand All @@ -35,14 +37,27 @@ describe('framework entry', () => {
}])
}
}

global.callAddElement = (name, id, ref, json, index, callbackId) => {
callAddElementSpy(name, ref, json, index, callbackId)
/* istanbul ignore if */
if (callbackId !== '-1') {
framework.callJS(id, [{
method: 'callback',
args: [callbackId, null, true]
}])
}
}
})

afterEach(() => {
callNativeSpy.reset()
callAddElementSpy.reset()
})

after(() => {
global.callNative = oriCallNative
global.callAddElement = oriCallAddElement
})

describe('createInstance', () => {
Expand Down Expand Up @@ -82,7 +97,8 @@ describe('framework entry', () => {
`
framework.createInstance(instanceId, code)

expect(callNativeSpy.callCount).to.be.equal(3)
expect(callNativeSpy.callCount).to.be.equal(2)
expect(callAddElementSpy.callCount).to.be.equal(1)

expect(callNativeSpy.firstCall.args[0]).to.be.equal(instanceId)
expect(callNativeSpy.firstCall.args[1]).to.deep.equal([{
Expand All @@ -97,25 +113,18 @@ describe('framework entry', () => {
}])
// expect(callNativeSpy.firstCall.args[2]).to.not.equal('-1')

expect(callNativeSpy.secondCall.args[0]).to.be.equal(instanceId)
delete callNativeSpy.secondCall.args[1][0].args[1].ref
expect(callNativeSpy.secondCall.args[1]).to.deep.equal([{
module: 'dom',
method: 'addElement',
args: ['_root', {
type: 'text',
attr: {
value: 'Hello World'
},
style: {}
},
0
]
}])
expect(callAddElementSpy.firstCall.args[0]).to.be.equal(instanceId)
delete callAddElementSpy.firstCall.args[1].ref
expect(callAddElementSpy.firstCall.args[1]).to.deep.equal({
type: 'text',
attr: { value: 'Hello World' },
style: {}
})

// expect(callNativeSpy.secondCall.args[2]).to.not.equal('-1')

expect(callNativeSpy.thirdCall.args[0]).to.be.equal(instanceId)
expect(callNativeSpy.thirdCall.args[1]).to.deep.equal([{
expect(callNativeSpy.secondCall.args[0]).to.be.equal(instanceId)
expect(callNativeSpy.secondCall.args[1]).to.deep.equal([{
module: 'dom',
method: 'createFinish',
args: []
Expand All @@ -137,7 +146,9 @@ describe('framework entry', () => {
}
const code = `// {"framework":"xxx","version":"0.3.1"}
'This is a piece of JavaScript from a third-party Framework...'`

framework.createInstance(instanceId + '~', code)

expect(spy.callCount).equal(1)
expect(spy.firstCall.args).eql([
instanceId + '~',
Expand Down
45 changes: 43 additions & 2 deletions html5/test/unit/default/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,24 @@ chai.use(sinonChai)
const callNativeSpy = sinon.spy()
global.callNative = function () {}

const callAddElementSpy = sinon.spy()
global.callAddElement = function () {}

describe('test input and output', () => {
const oriCallNative = global.callNative
const oriCallAddElement = global.callAddElement
const allDocs = {}

function callAddElementWrapper (name, ref, json, index, cbId) {
callAddElementSpy(ref, json, index)

const doc = allDocs[name]

doc.addElement(ref, json, index)

return callAddElementSpy.args.length
}

function callNativeWrapper (name, tasks, cbId) {
callNativeSpy(tasks)

Expand Down Expand Up @@ -84,10 +98,14 @@ describe('test input and output', () => {
beforeEach(() => {
callNativeSpy.reset()
global.callNative = callNativeWrapper

callAddElementSpy.reset()
global.callAddElement = callAddElementWrapper
})

afterEach(() => {
global.callNative = oriCallNative
global.callAddElement = oriCallAddElement
})

it('single case', () => {
Expand Down Expand Up @@ -148,6 +166,7 @@ describe('test input and output', () => {
framework.createInstance(name, inputCode)
const expected = eval('(' + outputCode + ')')
const actual = doc.toJSON()

expect(actual).eql(expected)

framework.destroyInstance(name)
Expand Down Expand Up @@ -855,6 +874,7 @@ describe('test input and output', () => {

describe('test callNative signals', () => {
const oriCallNative = global.callNative
const oriCallAddElement = global.callAddElement

function genCallNativeWrapper (count) {
return (name, tasks, cbId) => {
Expand All @@ -867,6 +887,17 @@ describe('test callNative signals', () => {
}
}

function genCallAddElementWrapper (count) {
return (name, ref, json, index, cbId) => {
callAddElementSpy(ref, json, index)
const length = callAddElementSpy.args.length
if (length > count) {
return -1
}
return length
}
}

before(() => {
sinon.stub(console, 'info')
sinon.stub(console, 'warn')
Expand All @@ -881,10 +912,12 @@ describe('test callNative signals', () => {

beforeEach(() => {
callNativeSpy.reset()
callAddElementSpy.reset()
})

afterEach(() => {
global.callNative = oriCallNative
global.callAddElement = oriCallAddElement
})

it('signals control', function () {
Expand All @@ -895,10 +928,14 @@ describe('test callNative signals', () => {

function run (calls) {
callNativeSpy.reset()
callAddElementSpy.reset()
global.callNative = genCallNativeWrapper(calls)
global.callAddElement = genCallAddElementWrapper(calls)

framework.createInstance(name + calls, inputCode)
framework.destroyInstance(name + calls)
expect(callNativeSpy.args.length).eql(calls + 2)
expect(callNativeSpy.args.length).eql(2)
expect(callAddElementSpy.args.length).eql(60)
}

for (let i = 5; i < 60; i++) {
Expand All @@ -914,10 +951,14 @@ describe('test callNative signals', () => {

function run (calls) {
callNativeSpy.reset()
callAddElementSpy.reset()
global.callNative = genCallNativeWrapper(calls)
global.callAddElement = genCallAddElementWrapper(calls)
framework.createInstance(name + calls, inputCode)
framework.destroyInstance(name + calls)
expect(callNativeSpy.args.length).eql(calls + 2)

expect(callNativeSpy.args.length).eql(2)
expect(callAddElementSpy.args.length).eql(903)
}

run(10)
Expand Down
1 change: 1 addition & 0 deletions html5/test/unit/default/vm/dom-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chai from 'chai'
const { expect } = chai

global.callNative = function () {}
global.callAddElement = function () {}

import {
createElement,
Expand Down
1 change: 1 addition & 0 deletions html5/test/unit/default/vm/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { expect } = chai
chai.use(sinonChai)

global.callNative = function () {}
global.callAddElement = function () {}

import Vm from '../../../../default/vm'
import { Document } from '../../../../vdom'
Expand Down
1 change: 1 addition & 0 deletions html5/test/unit/default/vm/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { expect } = chai
chai.use(sinonChai)

global.callNative = function () {}
global.callAddElement = function () {}

import Vm from '../../../../default/vm'
import { Document } from '../../../../vdom'
Expand Down
2 changes: 2 additions & 0 deletions html5/test/unit/vdom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { expect } = chai
chai.use(sinonChai)

global.callNative = function () {}
global.callAddElement = function () {}

import {
instanceMap,
Expand All @@ -16,6 +17,7 @@ import {
import Listener from '../../../vdom/listener'

global.callNative = function () {}
global.callAddElement = function () {}

describe('document constructor', () => {
it('create & destroy document', () => {
Expand Down
2 changes: 2 additions & 0 deletions html5/test/unit/vdom/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const { expect } = chai
chai.use(sinonChai)

global.callNative = function () {}
global.callAddElement = function () {}

import { Document } from '../../../vdom'
import Listener from '../../../vdom/listener'

global.callNative = function () {}
global.callAddElement = function () {}

describe('dom listener basic', () => {
it('works with no handler', () => {
Expand Down
37 changes: 36 additions & 1 deletion html5/vdom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,42 @@ function genCallTasks (id) {
if (!Array.isArray(tasks)) {
tasks = [tasks]
}
return callNative(id, tasks, '-1')

// const addElementTask = tasks.filter(task => {
// return task.module === 'dom' && task.method === 'addElement'
// });
//
// const otherTasks = tasks.filter(task => {
// return task.module !== 'dom' || task.method !== 'addElement'
// });

// console.log('====>', addElementTask)
// console.log('----->', otherTasks)

tasks.forEach(task => {
if (task.module === 'dom' && task.method === 'addElement') {
const [ref, json, index] = task.args
callAddElement(id, ref, json, index, '-1')
}
else {
callNative(id, [task], '-1')
}
})

// let returnValue;
// if (otherTasks && otherTasks.length) {
// returnValue = callNative(id, otherTasks, '-1')
// }
//
// if (addElementTask && addElementTask.length) { // dom.addElement
// // if (false) {
// addElementTask.forEach(task => {
// const [ref, json, index] = task.args
// callAddElement(id, ref, json, index, -1)
// });
// }
//
// return returnValue;
}
}

Expand Down
3 changes: 1 addition & 2 deletions html5/vdom/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ Listener.prototype.addElement = function (element, ref, index) {
if (!(index >= 0)) {
index = -1
}
//return this.addActions(createAction('addElement', [ref, element.toJSON(), index]))
callAddElement(id,ref, element.toJSON(), index,-1)
return this.addActions(createAction('addElement', [ref, element.toJSON(), index]))
}

Listener.prototype.removeElement = function (ref) {
Expand Down

0 comments on commit db14f21

Please sign in to comment.