Skip to content

Commit

Permalink
change plugin system behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
djyde committed Mar 31, 2017
1 parent 22d2e33 commit 7186a86
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 63 deletions.
22 changes: 3 additions & 19 deletions src/cans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@ export interface ICansPluginObject {
}

export interface ICansPlugin {
namespace: string,
protected?: boolean,
observable: (app: Cans) => any
(app: Cans, options: any): void
}

export class Cans {

private __routerComponent: JSX.Element = React.createElement('div')
private __mountedRoot?: Element | null

private __plugins: ICansPlugin[] = []
private __pluginsObject: ICansPluginObject = {}

private __models: ICansModel[] = []
private __modelsObject: ICansModelObject = {}

Expand All @@ -43,10 +38,6 @@ export class Cans {
return this.__modelsObject
}

get plugins () {
return this.__pluginsObject
}

route (routeFunc: () => JSX.Element) {
// wrap provider
const providerProps = {}
Expand All @@ -70,15 +61,8 @@ export class Cans {
this.__mountedRoot = el
}

use (plugin: ICansPlugin) {
// registry plugin
const o = plugin.observable(this)
if (isProtected(plugin)) {
defineReadOnlyProperty(this.__pluginsObject, plugin.namespace, o, 'This plugin is readonly')
} else {
this.__pluginsObject[plugin.namespace] = plugin.observable(this)
}
this.__plugins.push(plugin)
use (plugin: ICansPlugin, options?) {
plugin(this, options)
}
}

Expand Down
27 changes: 0 additions & 27 deletions test/cans.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ describe('cans', () => {
protected: false,
observable: observable({ data: 'foo' })
})

app.use({
namespace: 'bar',
observable: app => 'bar'
})

app.use({
namespace: 'public',
protected: false,
observable: app => 'bar'
})
})

afterEach(() => {
Expand Down Expand Up @@ -74,22 +63,6 @@ describe('cans', () => {
assert.doesNotThrow(() => { app.models.public = 'foooo' })
done()
})

it('plguins should be readonly', done => {
assert.throws(() => { app.plugins.bar = 'bar' })
done()
})

it('can modify none exist plugins', done => {
assert.doesNotThrow(() => { app.plugins.blabla = 'bar' })
done()
})

it('can modify none protected plugins', done => {
assert.doesNotThrow(() => { app.plugins.public = 'foooo' })
done()
})

})

describe('#start', () => {
Expand Down
30 changes: 13 additions & 17 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ import React from 'react'
import assert from 'power-assert'
import { mount, shallow } from 'enzyme'

const httpPlugin = {
namespace: 'http',
observable: app => {
return (url) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('blablabla')
}, 1000)
})
}
}
const httpPlugin = (app, options) => {
app.http = url => new Promise((resolve, reject) => {
setTimeout(() => {
resolve(options.response)
}, 1000)
})
}

describe('plugin system', () => {
const app = cans()

app.use(httpPlugin)
app.use(httpPlugin, { response: 'fake response' })

app.model({
namespace: 'weather',
Expand All @@ -32,7 +28,7 @@ describe('plugin system', () => {
}),

fetchWeather: action.bound(async function () {
const data = await app.plugins.http('blablabla')
const data = await app.http('blablabla')
this.applyData(data)
})
})
Expand All @@ -49,7 +45,7 @@ describe('plugin system', () => {
})

it('should use plugin', done => {
assert(app.plugins.http)
assert(app.http)
done()
})

Expand All @@ -58,13 +54,13 @@ describe('plugin system', () => {
await app.models.weather.fetchWeather()
} catch (e) {
done(e)
}
}
})

it('should change weather data', done => {
const wrapped = shallow(<Weather.wrappedComponent models={{ weather: app.models.weather }} />)
const wrapped = shallow(<Weather.wrappedComponent models={app.models} />)

assert.equal(wrapped.find('#data').first().text(), 'blablabla')
assert.equal(wrapped.find('#data').first().text(), 'fake response')

done()
})
Expand Down

0 comments on commit 7186a86

Please sign in to comment.