Skip to content

Commit

Permalink
feat(weex): implement "weex.supports" api to support feature detection (
Browse files Browse the repository at this point in the history
#6053)

* feat(weex): add "weex.supports" api for feature detection

* test(weex): add test case for weex.supports and related methods
  • Loading branch information
Hanks10100 authored and yyx990803 committed Jul 10, 2017
1 parent 0d6ad12 commit b1512d8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/platforms/weex/entry-framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export function createInstance (
const weexInstanceVar = {
config,
document,
supports,
requireModule: moduleGetter
}
Object.freeze(weexInstanceVar)
Expand Down Expand Up @@ -216,6 +217,18 @@ export function registerModules (newModules) {
}
}

/**
* Check whether the module or the method has been registered.
* @param {String} module name
* @param {String} method name (optional)
*/
export function isRegisteredModule (name, method) {
if (typeof method === 'string') {
return !!(modules[name] && modules[name][method])
}
return !!modules[name]
}

/**
* Register native components information.
* @param {array} newComponents
Expand All @@ -235,6 +248,35 @@ export function registerComponents (newComponents) {
}
}

/**
* Check whether the component has been registered.
* @param {String} component name
*/
export function isRegisteredComponent (name) {
return !!components[name]
}

/**
* Detects whether Weex supports specific features.
* @param {String} condition
*/
export function supports (condition) {
if (typeof condition !== 'string') return null

const res = condition.match(/^@(\w+)\/(\w+)(\.(\w+))?$/i)
if (res) {
const type = res[1]
const name = res[2]
const method = res[4]
switch (type) {
case 'module': return isRegisteredModule(name, method)
case 'component': return isRegisteredComponent(name)
}
}

return null
}

/**
* Create a fresh instance of Vue for each Weex instance.
*/
Expand Down
60 changes: 60 additions & 0 deletions test/weex/runtime/framework.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,30 @@ describe('framework APIs', () => {
).toEqual(['b(1)'])
})

it('isRegisteredModule', () => {
framework.registerModules({
foo: ['a', 'b'],
bar: [
{ name: 'x', args: ['string'] },
{ name: 'y', args: ['number'] }
]
})
expect(framework.isRegisteredModule('foo')).toBe(true)
expect(framework.isRegisteredModule('bar')).toBe(true)
expect(framework.isRegisteredModule('foo', 'a')).toBe(true)
expect(framework.isRegisteredModule('foo', 'b')).toBe(true)
expect(framework.isRegisteredModule('bar', 'x')).toBe(true)
expect(framework.isRegisteredModule('bar', 'y')).toBe(true)
expect(framework.isRegisteredModule('FOO')).toBe(false)
expect(framework.isRegisteredModule(' bar ')).toBe(false)
expect(framework.isRegisteredModule('unknown')).toBe(false)
expect(framework.isRegisteredModule('#}{)=}')).toBe(false)
expect(framework.isRegisteredModule('foo', '')).toBe(false)
expect(framework.isRegisteredModule('foo', 'c')).toBe(false)
expect(framework.isRegisteredModule('bar', 'z')).toBe(false)
expect(framework.isRegisteredModule('unknown', 'unknown')).toBe(false)
})

it('registerComponents', () => {
framework.registerComponents(['foo', { type: 'bar' }, 'text'])
const instance = new Instance(runtime)
Expand All @@ -431,6 +455,42 @@ describe('framework APIs', () => {
})
})

it('isRegisteredComponent', () => {
framework.registerComponents(['foo', { type: 'bar' }, 'text'])
expect(framework.isRegisteredComponent('foo')).toBe(true)
expect(framework.isRegisteredComponent('bar')).toBe(true)
expect(framework.isRegisteredComponent('text')).toBe(true)
expect(framework.isRegisteredComponent('FOO')).toBe(false)
expect(framework.isRegisteredComponent(' bar ')).toBe(false)
expect(framework.isRegisteredComponent('<text>')).toBe(false)
expect(framework.isRegisteredComponent('#}{)=}')).toBe(false)
})

it('weex.supports', () => {
framework.registerComponents(['apple', { type: 'banana' }])
framework.registerModules({
cat: ['eat', 'sleep'],
dog: [
{ name: 'bark', args: ['string'] }
]
})
expect(framework.supports('@component/apple')).toBe(true)
expect(framework.supports('@component/banana')).toBe(true)
expect(framework.supports('@module/cat')).toBe(true)
expect(framework.supports('@module/cat.eat')).toBe(true)
expect(framework.supports('@module/cat.sleep')).toBe(true)
expect(framework.supports('@module/dog.bark')).toBe(true)
expect(framework.supports('@component/candy')).toBe(false)
expect(framework.supports('@module/bird')).toBe(false)
expect(framework.supports('@module/bird.sing')).toBe(false)
expect(framework.supports('@module/dog.sleep')).toBe(false)
expect(framework.supports('apple')).toBe(null)
expect(framework.supports('<banana>')).toBe(null)
expect(framework.supports('cat')).toBe(null)
expect(framework.supports('@dog')).toBe(null)
expect(framework.supports('@component/dog#bark')).toBe(null)
})

it('vm.$getConfig', () => {
const instance = new Instance(runtime)
instance.$create(`
Expand Down

0 comments on commit b1512d8

Please sign in to comment.