mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
added context hook to allow the override of the default binder
This commit is contained in:
@@ -10,11 +10,13 @@ type ExtensionRegistration = {
|
|||||||
* The Binder class is mainly exported for testing, plugins should only use the default export.
|
* The Binder class is mainly exported for testing, plugins should only use the default export.
|
||||||
*/
|
*/
|
||||||
export class Binder {
|
export class Binder {
|
||||||
|
name: string;
|
||||||
extensionPoints: {
|
extensionPoints: {
|
||||||
[key: string]: Array<ExtensionRegistration>;
|
[key: string]: Array<ExtensionRegistration>;
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor() {
|
constructor(name: string) {
|
||||||
|
this.name = name;
|
||||||
this.extensionPoints = {};
|
this.extensionPoints = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,6 +75,6 @@ export class Binder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// singleton binder
|
// singleton binder
|
||||||
const binder = new Binder();
|
const binder = new Binder("default");
|
||||||
|
|
||||||
export default binder;
|
export default binder;
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
export { default as binder } from "./binder";
|
export { default as binder, Binder } from "./binder";
|
||||||
|
export * from "./useBinder";
|
||||||
export { default as ExtensionPoint } from "./ExtensionPoint";
|
export { default as ExtensionPoint } from "./ExtensionPoint";
|
||||||
|
|||||||
29
scm-ui/ui-extensions/src/useBinder.test.tsx
Normal file
29
scm-ui/ui-extensions/src/useBinder.test.tsx
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import useBinder, { BinderContext } from "./useBinder";
|
||||||
|
import { Binder } from "./binder";
|
||||||
|
import { mount } from "enzyme";
|
||||||
|
import "@scm-manager/ui-tests/enzyme";
|
||||||
|
import React from "react";
|
||||||
|
|
||||||
|
describe("useBinder tests", () => {
|
||||||
|
const BinderName = () => {
|
||||||
|
const binder = useBinder();
|
||||||
|
return <>{binder.name}</>;
|
||||||
|
};
|
||||||
|
|
||||||
|
it("should return default binder", () => {
|
||||||
|
const rendered = mount(<BinderName />);
|
||||||
|
expect(rendered.text()).toBe("default");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return binder from context", () => {
|
||||||
|
const binder = new Binder("from-context");
|
||||||
|
const app = (
|
||||||
|
<BinderContext.Provider value={binder}>
|
||||||
|
<BinderName />
|
||||||
|
</BinderContext.Provider>
|
||||||
|
);
|
||||||
|
|
||||||
|
const rendered = mount(app);
|
||||||
|
expect(rendered.text()).toBe("from-context");
|
||||||
|
});
|
||||||
|
});
|
||||||
16
scm-ui/ui-extensions/src/useBinder.ts
Normal file
16
scm-ui/ui-extensions/src/useBinder.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { createContext, useContext } from "react";
|
||||||
|
import defaultBinder from "./binder";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The BinderContext should only be used to override the default binder for testing purposes.
|
||||||
|
*/
|
||||||
|
export const BinderContext = createContext(defaultBinder);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook to get the binder from context.
|
||||||
|
*/
|
||||||
|
export const useBinder = () => {
|
||||||
|
return useContext(BinderContext);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useBinder;
|
||||||
Reference in New Issue
Block a user