-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POC: Plugin interface with wrapper in closure #82
Changes from 3 commits
3313069
c8f119e
cc5427f
922bc05
a818edf
4a95fb1
a1cdd17
57f69eb
f5eb66e
ce1c00c
407a757
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.idea | ||
node_modules | ||
yarn-error.log | ||
dist | ||
coverage | ||
coverage |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class Pluggable { | ||
installedPlugins: any | ||
constructor() { | ||
this.installedPlugins = [] | ||
} | ||
|
||
install(handler, options = {}) { | ||
if (typeof handler !== 'function') { | ||
console.error('plugin.install must receive a function') | ||
handler = () => ({}) | ||
} | ||
this.installedPlugins.push({ handler, options }) | ||
} | ||
|
||
extend(instance) { | ||
const invokeSetup = (plugin) => plugin.handler(instance) // invoke the setup method passed to install | ||
const bindProperty = ([property, value]: [string, any]) => { | ||
instance[property] = | ||
typeof value === 'function' ? value.bind(instance) : value | ||
} | ||
const addAllPropertiesFromSetup = (setupResult) => { | ||
setupResult = typeof setupResult === 'object' ? setupResult : {} | ||
Object.entries(setupResult).forEach(bindProperty) | ||
} | ||
|
||
this.installedPlugins.map(invokeSetup).forEach(addAllPropertiesFromSetup) | ||
} | ||
|
||
/** For testing */ | ||
reset() { | ||
this.installedPlugins = [] | ||
} | ||
} | ||
|
||
export const config = { | ||
plugins: { | ||
VueWrapper: new Pluggable(), | ||
DOMWrapper: new Pluggable() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { mount } from './mount' | ||
import { RouterLinkStub } from './components/RouterLinkStub' | ||
import { VueWrapper } from './vue-wrapper' | ||
|
||
export { mount, RouterLinkStub } | ||
import { config } from './config' | ||
|
||
export { mount, RouterLinkStub, VueWrapper, config } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { mount, VueWrapper, config } from '../../src' | ||
|
||
declare module '../../src/vue-wrapper' { | ||
interface VueWrapper { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 I will give this a try There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To no-one's surprised I changed the types based on @cexbrayat 's recommendation and it worked |
||
width(): number | ||
$el: Element | ||
} | ||
} | ||
|
||
const textValue = `I'm the innerHTML` | ||
const mountComponent = () => mount({ template: `<h1>${textValue}</h1>` }) | ||
|
||
describe('Plugin', () => { | ||
describe('#install method', () => { | ||
beforeEach(() => { | ||
config.plugins.VueWrapper.reset() | ||
}) | ||
|
||
it('extends wrappers with the return values from the install function', () => { | ||
const width = 230 | ||
const plugin = () => ({ width }) | ||
config.plugins.VueWrapper.install(plugin) | ||
const wrapper = mountComponent() | ||
expect(wrapper).toHaveProperty('width', width) | ||
}) | ||
|
||
it('receives the wrapper inside the plugin setup', () => { | ||
const plugin = (wrapper) => { | ||
return { | ||
$el: wrapper.element // simple aliases | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will we have access to private properties on the instance? Like the rootVM? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, your original example will work. You can get access to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const namePlugin = wrapper => ({ lowerCaseName: wrapper.componentVM.$.type.name })
config.plugins.VueWrapper.install(namePlugin)
const wrapper = mount({ template: `<h1>Hello</h1>`, name: 'My_Component' })
wrapper.lowerCaseName // 'my_component' |
||
} | ||
} | ||
config.plugins.VueWrapper.install(plugin) | ||
const wrapper = mountComponent() | ||
expect(wrapper.$el.innerHTML).toEqual(textValue) | ||
}) | ||
|
||
describe('error states', () => { | ||
const plugins = [ | ||
() => false, | ||
() => true, | ||
() => [], | ||
true, | ||
false, | ||
'property', | ||
120 | ||
] | ||
|
||
it.each(plugins)( | ||
'Calling install with %p is handled gracefully', | ||
(plugin) => { | ||
config.plugins.VueWrapper.install(plugin) | ||
expect(() => mountComponent()).not.toThrow() | ||
} | ||
) | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should decide if we're going to exit the process here or quietly swallow the misconfigured plugin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think failing loudly is usually a good idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The louder the better, ideally with "you screwed up and here's where and how to fix it"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. I'm fine with that. I wish I had a plugin name to point people at.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm didn't think of that. 🤔