frontend-plugin-api: add ability to override params from .makeWithOverrides
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -949,4 +949,119 @@ describe('createExtensionBlueprint', () => {
|
||||
),
|
||||
).toThrow('Refused to override params and factory at the same time');
|
||||
});
|
||||
|
||||
it('should be possible to override extensions resulting from .makeWithOverrides', () => {
|
||||
const testDataRef1 = createExtensionDataRef<string>().with({ id: 'test1' });
|
||||
const testDataRef2 = createExtensionDataRef<string>().with({ id: 'test2' });
|
||||
|
||||
function getOutputs(ext: ExtensionDefinition) {
|
||||
const tester = createExtensionTester(ext);
|
||||
return {
|
||||
test1: tester.get(testDataRef1),
|
||||
test2: tester.get(testDataRef2),
|
||||
};
|
||||
}
|
||||
|
||||
const blueprint = createExtensionBlueprint({
|
||||
kind: 'test-extension',
|
||||
attachTo: { id: 'test', input: 'default' },
|
||||
output: [testDataRef1, testDataRef2],
|
||||
*factory(params: { test1: string; test2: string }) {
|
||||
yield testDataRef1(params.test1);
|
||||
yield testDataRef2(params.test2);
|
||||
},
|
||||
});
|
||||
|
||||
const extension = blueprint.makeWithOverrides({
|
||||
factory(origFactory) {
|
||||
return origFactory({
|
||||
test1: 'orig-1',
|
||||
test2: 'orig-2',
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
expect(getOutputs(extension)).toEqual({
|
||||
test1: 'orig-1',
|
||||
test2: 'orig-2',
|
||||
});
|
||||
|
||||
expect(
|
||||
getOutputs(
|
||||
extension.override({
|
||||
params: {
|
||||
test1: 'override-1',
|
||||
test2: 'override-2',
|
||||
},
|
||||
}),
|
||||
),
|
||||
).toEqual({
|
||||
test1: 'override-1',
|
||||
test2: 'override-2',
|
||||
});
|
||||
|
||||
// Partial override
|
||||
expect(
|
||||
getOutputs(
|
||||
extension.override({
|
||||
params: {
|
||||
test2: 'override-2',
|
||||
},
|
||||
}),
|
||||
),
|
||||
).toEqual({
|
||||
test1: 'orig-1',
|
||||
test2: 'override-2',
|
||||
});
|
||||
|
||||
expect(
|
||||
getOutputs(
|
||||
extension.override({
|
||||
factory(origFactory) {
|
||||
return origFactory({
|
||||
params: {
|
||||
test1: 'override-1',
|
||||
test2: 'override-2',
|
||||
},
|
||||
});
|
||||
},
|
||||
}),
|
||||
),
|
||||
).toEqual({
|
||||
test1: 'override-1',
|
||||
test2: 'override-2',
|
||||
});
|
||||
|
||||
// Partial override via factory
|
||||
expect(
|
||||
getOutputs(
|
||||
extension.override({
|
||||
factory(origFactory) {
|
||||
return origFactory({
|
||||
params: {
|
||||
test2: 'override-2',
|
||||
},
|
||||
});
|
||||
},
|
||||
}),
|
||||
),
|
||||
).toEqual({
|
||||
test1: 'orig-1',
|
||||
test2: 'override-2',
|
||||
});
|
||||
|
||||
expect(() =>
|
||||
getOutputs(
|
||||
extension.override({
|
||||
params: {
|
||||
test1: 'override-1',
|
||||
test2: 'override-2',
|
||||
},
|
||||
factory(origFactory) {
|
||||
return origFactory();
|
||||
},
|
||||
}),
|
||||
),
|
||||
).toThrow('Refused to override params and factory at the same time');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -508,20 +508,25 @@ export function createExtensionBlueprint<
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
factory: ({ node, config, inputs, apis }) => {
|
||||
factory: ctx => {
|
||||
const { node, config, inputs, apis } = ctx;
|
||||
return args.factory(
|
||||
(innerParams, innerContext) => {
|
||||
return createExtensionDataContainer<UOutput>(
|
||||
options.factory(innerParams, {
|
||||
apis,
|
||||
node,
|
||||
config: (innerContext?.config ?? config) as any,
|
||||
inputs: resolveInputOverrides(
|
||||
options.inputs,
|
||||
inputs,
|
||||
innerContext?.inputs,
|
||||
) as any,
|
||||
}) as Iterable<any>,
|
||||
options.factory(
|
||||
// TODO(Rugvip): Same as ctx.params from .make
|
||||
{ ...innerParams, ...(ctx as any).params },
|
||||
{
|
||||
apis,
|
||||
node,
|
||||
config: (innerContext?.config ?? config) as any,
|
||||
inputs: resolveInputOverrides(
|
||||
options.inputs,
|
||||
inputs,
|
||||
innerContext?.inputs,
|
||||
) as any,
|
||||
},
|
||||
) as Iterable<any>,
|
||||
options.output,
|
||||
);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user