devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
145 lines (144 loc) • 5.57 kB
JavaScript
/**
* DevExtreme (esm/__internal/core/ai_integration/commands/generateGridColumn.test.js)
* Version: 25.2.5
* Build date: Fri Feb 20 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import {
beforeEach,
describe,
expect,
it,
jest
} from "@jest/globals";
import {
PromptManager
} from "../../../core/ai_integration/core/prompt_manager";
import {
RequestManager
} from "../../../core/ai_integration/core/request_manager";
import {
templates
} from "../../../core/ai_integration/templates";
import {
Provider
} from "../../../core/ai_integration/test_utils/provider_mock";
import {
GenerateGridColumnCommand
} from "./generateGridColumn";
const COMMAND_NAME = "generateGridColumn";
const USER_TEXT = "user text";
const PROCESSED_DATA = '{"1":{"id":1,"name":"Test 1"},"2":{"id":2,"name":"Test 2"}}';
describe("GenerateGridColumnCommand", (() => {
const params = {
text: USER_TEXT,
data: {
1: {
id: 1,
name: "Test 1"
},
2: {
id: 2,
name: "Test 2"
}
}
};
let promptManager = null;
let requestManager = null;
let command = null;
beforeEach((() => {
const provider = new Provider;
requestManager = new RequestManager(provider);
promptManager = new PromptManager;
command = new GenerateGridColumnCommand(promptManager, requestManager)
}));
describe("getTemplateName", (() => {
it("should return the name of the corresponding template", (() => {
const templateName = command.getTemplateName();
expect(templateName).toStrictEqual(COMMAND_NAME)
}))
}));
describe("buildPromptData", (() => {
it("should form PromptData with text and fields info", (() => {
const promptData = command.buildPromptData(params);
expect(promptData).toStrictEqual({
user: {
text: USER_TEXT,
data: PROCESSED_DATA
}
})
}))
}));
describe("parseResult", (() => {
it("should return the parsed result", (() => {
const result = command.parseResult('{ "1": "Item with the name Item 1.", "10": "Item with the name Item 10." }');
expect(result).toStrictEqual({
data: {
1: "Item with the name Item 1.",
10: "Item with the name Item 10."
}
})
}));
it("should return empty data object when response is an empty string", (() => {
const result = command.parseResult("");
expect(result).toStrictEqual({
data: {}
})
}));
it("should parse result when response data is a stringified object", (() => {
const result = command.parseResult({
data: '{ "1": "Item with the name Item 1.", "10": "Item with the name Item 10." }'
});
expect(result).toStrictEqual({
data: {
1: "Item with the name Item 1.",
10: "Item with the name Item 10."
}
})
}));
it("should not parse result when response data is an object", (() => {
const response = {
data: {
1: "Item with the name Item 1.",
10: "Item with the name Item 10."
}
};
const result = command.parseResult(response);
expect(result).toStrictEqual(response)
}))
}));
describe("execute", (() => {
const callbacks = {
onComplete: () => {}
};
it("promptManager.buildPrompt should be called with the passed data", (() => {
const buildPromptSpy = jest.spyOn(promptManager, "buildPrompt");
command.execute(params, callbacks);
expect(buildPromptSpy).toHaveBeenCalledTimes(1);
expect(promptManager.buildPrompt).toHaveBeenCalledWith(COMMAND_NAME, {
user: {
text: USER_TEXT,
data: PROCESSED_DATA
}
})
}));
it("promptManager.buildPrompt should should return prompt with passed values", (() => {
var _templates$generateGr;
jest.spyOn(promptManager, "buildPrompt");
command.execute(params, callbacks);
const expectedUserPrompt = null === (_templates$generateGr = templates.generateGridColumn.user) || void 0 === _templates$generateGr ? void 0 : _templates$generateGr.replace("{{text}}", USER_TEXT).replace("{{data}}", PROCESSED_DATA);
expect(promptManager.buildPrompt).toHaveReturnedWith({
system: templates.generateGridColumn.system,
user: expectedUserPrompt
})
}));
it("should call provider.sendRequest once and return the abort function", (() => {
const sendRequestSpy = jest.spyOn(requestManager, "sendRequest");
const abort = command.execute(params, callbacks);
expect(typeof abort).toBe("function");
expect(sendRequestSpy).toHaveBeenCalledTimes(1)
}))
}))
}));