chore: [one click binding] Create a registry to maintain the list of the widget query generators (#21764)
## Description Basic code to build datasource registry for one click binding. Fixes #21506 ## Type of change - New feature (non-breaking change which adds functionality) ## How Has This Been Tested? - Jest ### Test Plan > Add Testsmith test cases links that relate to this PR ### Issues raised during DP testing > Link issues raised during DP testing for better visiblity and tracking (copy link from comments dropped on this PR) ## Checklist: ### Dev activity - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] PR is being merged under a feature flag ### QA activity: - [ ] Test plan has been approved by relevant developers - [ ] Test plan has been peer reviewed by QA - [ ] Cypress test cases have been added and approved by either SDET or manual QA - [ ] Organized project review call with relevant stakeholders after Round 1/2 of QA - [ ] Added Test Plan Approved label after reveiwing all Cypress test
This commit is contained in:
parent
18b82667a2
commit
6410892f2e
32
app/client/src/utils/WidgetQueryGeneratorRegistry.test.ts
Normal file
32
app/client/src/utils/WidgetQueryGeneratorRegistry.test.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import WidgetQueryGeneratorRegistry from "./WidgetQueryGeneratorRegistry";
|
||||
|
||||
describe("WidgetQueryGeneratorRegistry", () => {
|
||||
const somepluginId = "somePluginId";
|
||||
|
||||
it("should be able to register a QueryGenerator", () => {
|
||||
const someQueryGenerator = {};
|
||||
|
||||
WidgetQueryGeneratorRegistry.register(somepluginId, someQueryGenerator);
|
||||
expect(WidgetQueryGeneratorRegistry.get(somepluginId)).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should return a falsey value when searching for an non existing generator", () => {
|
||||
const nonExistingQueryGeneratopr = "someId";
|
||||
expect(
|
||||
WidgetQueryGeneratorRegistry.get(nonExistingQueryGeneratopr),
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should return the same adaptor reference when querying the same pluginId", () => {
|
||||
const adaptor = WidgetQueryGeneratorRegistry.get(somepluginId);
|
||||
expect(adaptor).toBe(WidgetQueryGeneratorRegistry.get(somepluginId));
|
||||
});
|
||||
it("should not find the registered plugin after clearing the registry", () => {
|
||||
WidgetQueryGeneratorRegistry.clear();
|
||||
expect(WidgetQueryGeneratorRegistry.get(somepluginId)).toBeFalsy();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
WidgetQueryGeneratorRegistry.clear();
|
||||
});
|
||||
});
|
||||
36
app/client/src/utils/WidgetQueryGeneratorRegistry.ts
Normal file
36
app/client/src/utils/WidgetQueryGeneratorRegistry.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { error } from "loglevel";
|
||||
|
||||
export default class WidgetQueryGeneratorRegistry {
|
||||
private static queryGeneratorMap = new Map();
|
||||
|
||||
static register(pluginId: string, queryGenerator: any) {
|
||||
if (this.queryGeneratorMap.has(pluginId)) {
|
||||
error(
|
||||
"There is already a widget query generator with the given pluginID:",
|
||||
pluginId,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.queryGeneratorMap.set(pluginId, queryGenerator);
|
||||
}
|
||||
|
||||
static clear() {
|
||||
WidgetQueryGeneratorRegistry.queryGeneratorMap.clear();
|
||||
}
|
||||
|
||||
static get(pluginId: string) {
|
||||
const queryAdaptor = this.queryGeneratorMap.get(pluginId);
|
||||
|
||||
if (!queryAdaptor) {
|
||||
error(
|
||||
"Couldn't find the query generator with the given pluginId:",
|
||||
pluginId,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
return queryAdaptor;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user