-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added definePlugin hybrid function.
- Loading branch information
1 parent
321adb8
commit 0826a51
Showing
14 changed files
with
206 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { DefinePluginFn } from './types'; | ||
|
||
export const definePlugin: DefinePluginFn<any, any> = () => ({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/react/src/__tests__/__snapshots__/definePlugin.spec.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`react > definePlugin() > should create a new higher order component 1`] = ` | ||
<div> | ||
Hello world! | ||
</div> | ||
`; | ||
|
||
exports[`react > definePlugin() > should provide data to children 1`] = ` | ||
<div> | ||
light | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { render } from '@testing-library/react'; | ||
import { defineComponent, definePlugin, h, inject } from '../index'; | ||
|
||
describe('react', () => { | ||
describe('definePlugin()', () => { | ||
it('should create a new higher order component', () => { | ||
const Plugin = definePlugin(() => {}); | ||
const Component = defineComponent({ | ||
render () { | ||
return h('div', {}, 'Hello world!'); | ||
} | ||
}); | ||
|
||
const wrapper = render(h(Plugin, {}, h(Component)) as any); | ||
expect(wrapper.container.firstChild).toMatchSnapshot(); | ||
}); | ||
|
||
it('should be called with plugin options', () => { | ||
const pluginOptions = { | ||
color: 'light' | ||
}; | ||
|
||
const Plugin = definePlugin((options) => { | ||
expect(options).toEqual(pluginOptions); | ||
}); | ||
const Component = defineComponent({ | ||
render () { | ||
return h('div', {}, 'Hello world!'); | ||
} | ||
}); | ||
|
||
render(h(Plugin, { options: pluginOptions }, h(Component)) as any); | ||
}); | ||
|
||
it('should provide data to children', () => { | ||
const provideSymbol = Symbol('provide'); | ||
const provideData = { color: 'light' }; | ||
|
||
const Plugin = definePlugin((options, { provide }) => { | ||
provide(provideSymbol, provideData); | ||
}); | ||
const Component = defineComponent({ | ||
setup () { | ||
const data = inject(provideSymbol); | ||
|
||
return { data }; | ||
}, | ||
render ({ data }) { | ||
return h('div', {}, data?.color); | ||
} | ||
}); | ||
|
||
const wrapper = render(h(Plugin, {}, h(Component)) as any); | ||
expect(wrapper.container.firstChild).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { DefineReactPluginFn } from './types'; | ||
import { provide } from './provide'; | ||
|
||
export const definePlugin: DefineReactPluginFn = (setup) => ({ children, options }) => { | ||
/** | ||
* Setup the plugin | ||
*/ | ||
|
||
setup(options, { provide }); | ||
|
||
/** | ||
* Render the child components array as-is | ||
*/ | ||
return children; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { definePlugin } from '../index'; | ||
import { createApp } from 'vue'; | ||
|
||
describe('vue', () => { | ||
describe('definePlugin()', () => { | ||
it('should create a new vue plugin', () => { | ||
const Plugin = definePlugin(() => {}); | ||
|
||
expect(Plugin).toHaveProperty('install'); | ||
expect(Plugin.install).toBeInstanceOf(Function); | ||
}); | ||
|
||
it('should be installable', () => { | ||
const app = createApp({}); | ||
const Inkline = definePlugin(() => {}); | ||
|
||
expect(() => app.use(Inkline)).not.toThrow(); | ||
}); | ||
|
||
it('should pass plugin options', () => { | ||
const pluginOptions = { color: 'light' }; | ||
const app = createApp({}); | ||
const Inkline = definePlugin((options) => { | ||
expect(options).toEqual(pluginOptions); | ||
}); | ||
|
||
app.use(Inkline, pluginOptions); | ||
}); | ||
|
||
it('should provide data to children', () => { | ||
const provideSymbol = Symbol('provided'); | ||
const provideData = { color: 'light' }; | ||
|
||
const app = createApp({}); | ||
const Inkline = definePlugin((options, { provide }) => { | ||
provide(provideSymbol, provideData); | ||
}); | ||
|
||
app.use(Inkline); | ||
|
||
expect(app.config.globalProperties).toHaveProperty(`$${provideSymbol.description}`, provideData); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { DefineVuePluginFn } from './types'; | ||
|
||
export const definePlugin: DefineVuePluginFn = (setup) => ({ | ||
install (app, options) { | ||
/** | ||
* Register components provided through options globally | ||
*/ | ||
|
||
for (const componentIndex in options?.components) { | ||
if (options.components[componentIndex].name) { | ||
app.component(options.components[componentIndex].name, options.components[componentIndex]); | ||
} | ||
} | ||
|
||
/** | ||
* Setup the plugin | ||
*/ | ||
|
||
setup(options, { | ||
provide: (identifier, data) => { | ||
app.config.globalProperties[`$${typeof identifier === 'symbol' ? identifier.description : identifier}`] = data; | ||
app.provide(identifier, data); | ||
} | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters