mirror of
https://github.com/pdfme/pdfme.git
synced 2026-04-17 20:49:43 -04:00
* add pluginRepository and make plugin checking more robust * Apply suggestions from code review Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * f * f --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
99 lines
3.0 KiB
TypeScript
99 lines
3.0 KiB
TypeScript
import { pluginRegistry } from '../src/pluginRegistry.js';
|
|
import { Plugins } from '../src/types.js';
|
|
|
|
describe('pluginRegistry', () => {
|
|
it('returns all entries as [label, plugin] pairs', () => {
|
|
const plugins = {
|
|
pluginA: { propPanel: { defaultSchema: { type: 'typeA' } } },
|
|
pluginB: { propPanel: { defaultSchema: { type: 'typeB' } } },
|
|
};
|
|
// @ts-ignore
|
|
const registry = pluginRegistry(plugins);
|
|
|
|
expect(registry.entries()).toEqual([
|
|
['pluginA', plugins.pluginA],
|
|
['pluginB', plugins.pluginB],
|
|
]);
|
|
});
|
|
|
|
it('returns all plugin values', () => {
|
|
const plugins = {
|
|
pluginA: { propPanel: { defaultSchema: { type: 'typeA' } } },
|
|
pluginB: { propPanel: { defaultSchema: { type: 'typeB' } } },
|
|
};
|
|
// @ts-ignore
|
|
const registry = pluginRegistry(plugins);
|
|
|
|
expect(registry.values()).toEqual([plugins.pluginA, plugins.pluginB]);
|
|
});
|
|
|
|
it('checks if plugins exist', () => {
|
|
const plugins = {
|
|
pluginA: { propPanel: { defaultSchema: { type: 'typeA' } } },
|
|
};
|
|
// @ts-ignore
|
|
const registry = pluginRegistry(plugins);
|
|
|
|
expect(registry.exists()).toBe(true);
|
|
});
|
|
|
|
it('returns false if no plugins exist', () => {
|
|
const plugins: Plugins = {};
|
|
const registry = pluginRegistry(plugins);
|
|
|
|
expect(registry.exists()).toBe(false);
|
|
});
|
|
|
|
it('finds a plugin with label by type', () => {
|
|
const plugins = {
|
|
pluginA: { propPanel: { defaultSchema: { type: 'typeA' } } },
|
|
pluginB: { propPanel: { defaultSchema: { type: 'typeB' } } },
|
|
};
|
|
// @ts-ignore
|
|
const registry = pluginRegistry(plugins);
|
|
|
|
expect(registry.findWithLabelByType('typeA')).toEqual(['pluginA', plugins.pluginA]);
|
|
});
|
|
|
|
it('returns undefined if no plugin matches the type in findWithLabelByType', () => {
|
|
const plugins = {
|
|
pluginA: { propPanel: { defaultSchema: { type: 'typeA' } } },
|
|
};
|
|
// @ts-ignore
|
|
const registry = pluginRegistry(plugins);
|
|
|
|
expect(registry.findWithLabelByType('typeB')).toEqual(['', undefined]);
|
|
});
|
|
|
|
it('finds a plugin by type', () => {
|
|
const plugins = {
|
|
pluginA: { propPanel: { defaultSchema: { type: 'typeA' } } },
|
|
pluginB: { propPanel: { defaultSchema: { type: 'typeB' } } },
|
|
};
|
|
// @ts-ignore
|
|
const registry = pluginRegistry(plugins);
|
|
|
|
expect(registry.findByType('typeA')).toEqual(plugins.pluginA);
|
|
});
|
|
|
|
it('returns undefined if no plugin matches the type in findByType', () => {
|
|
const plugins = {
|
|
pluginA: { propPanel: { defaultSchema: { type: 'typeA' } } },
|
|
};
|
|
// @ts-ignore
|
|
const registry = pluginRegistry(plugins);
|
|
|
|
expect(registry.findByType('typeB')).toBeUndefined();
|
|
});
|
|
|
|
it('ignores plugins without a valid propPanel in findWithLabelByType', () => {
|
|
const plugins = {
|
|
pluginA: { propPanel: { defaultSchema: { type: 'typeA' } } },
|
|
pluginB: { propPanel: null },
|
|
};
|
|
// @ts-ignore
|
|
const registry = pluginRegistry(plugins);
|
|
|
|
expect(registry.findWithLabelByType('typeB')).toEqual(['', undefined]);
|
|
});
|
|
}); |