|
| 1 | +import { |
| 2 | + afterEach, beforeEach, describe, expect, it, jest, |
| 3 | +} from '@jest/globals'; |
| 4 | +import type { GenerateGridColumnCommandResponse } from '@js/common/ai-integration'; |
| 5 | +import type { dxElementWrapper } from '@js/core/renderer'; |
| 6 | +import $ from '@js/core/renderer'; |
| 7 | +import type { Properties as TreeListProperties } from '@js/ui/tree_list'; |
| 8 | +import TreeList from '@js/ui/tree_list'; |
| 9 | +import { AIIntegration } from '@ts/core/ai_integration/core/ai_integration'; |
| 10 | +import { TreeListModel } from '@ts/grids/tree_list/__tests__/__mock__/model/tree_list'; |
| 11 | + |
| 12 | +const SELECTORS = { |
| 13 | + treeListContainer: '#treeListContainer', |
| 14 | +}; |
| 15 | + |
| 16 | +const TREELIST_CONTAINER_ID = 'treeListContainer'; |
| 17 | + |
| 18 | +const items = [ |
| 19 | + { |
| 20 | + id: 1, parentId: 0, name: 'Name 1', value: 10, |
| 21 | + }, |
| 22 | + { |
| 23 | + id: 2, parentId: 1, name: 'Name 2', value: 20, |
| 24 | + }, |
| 25 | +]; |
| 26 | + |
| 27 | +interface RequestResult { |
| 28 | + promise: Promise<GenerateGridColumnCommandResponse>; |
| 29 | + abort: () => void; |
| 30 | +} |
| 31 | + |
| 32 | +const createTreeList = async ( |
| 33 | + options: TreeListProperties = {}, |
| 34 | +): Promise<{ |
| 35 | + $container: dxElementWrapper; |
| 36 | + component: TreeListModel; |
| 37 | + instance: TreeList; |
| 38 | +}> => new Promise((resolve) => { |
| 39 | + const $container = $('<div>') |
| 40 | + .attr('id', TREELIST_CONTAINER_ID) |
| 41 | + .appendTo(document.body); |
| 42 | + |
| 43 | + const instance = new TreeList($container.get(0) as HTMLDivElement, options); |
| 44 | + const component = new TreeListModel($container.get(0) as HTMLElement); |
| 45 | + |
| 46 | + jest.runAllTimers(); |
| 47 | + |
| 48 | + resolve({ |
| 49 | + $container, |
| 50 | + component, |
| 51 | + instance, |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +const beforeTest = (): void => { |
| 56 | + jest.useFakeTimers(); |
| 57 | +}; |
| 58 | + |
| 59 | +const afterTest = (): void => { |
| 60 | + const $container = $(SELECTORS.treeListContainer); |
| 61 | + const treeList = ($container as any).dxTreeList('instance') as TreeList; |
| 62 | + |
| 63 | + treeList.dispose(); |
| 64 | + $container.remove(); |
| 65 | + jest.clearAllMocks(); |
| 66 | + jest.useRealTimers(); |
| 67 | +}; |
| 68 | + |
| 69 | +describe('AI data', () => { |
| 70 | + beforeEach(beforeTest); |
| 71 | + afterEach(afterTest); |
| 72 | + |
| 73 | + describe('when prompt is set', () => { |
| 74 | + it('should be rendered', async () => { |
| 75 | + const { component } = await createTreeList({ |
| 76 | + dataSource: items, |
| 77 | + keyExpr: 'id', |
| 78 | + parentIdExpr: 'parentId', |
| 79 | + autoExpandAll: true, |
| 80 | + columns: [ |
| 81 | + { dataField: 'id', caption: 'ID' }, |
| 82 | + { dataField: 'name', caption: 'Name' }, |
| 83 | + { dataField: 'value', caption: 'Value' }, |
| 84 | + { |
| 85 | + type: 'ai', |
| 86 | + caption: 'AI Column', |
| 87 | + name: 'myColumn', |
| 88 | + ai: { |
| 89 | + prompt: 'Initial prompt', |
| 90 | + aiIntegration: new AIIntegration({ |
| 91 | + sendRequest(): RequestResult { |
| 92 | + return { |
| 93 | + promise: new Promise<string>((resolve) => { |
| 94 | + resolve('{"1":"AI Response 1","2":"AI Response 2"}'); |
| 95 | + }), |
| 96 | + abort: (): void => {}, |
| 97 | + }; |
| 98 | + }, |
| 99 | + }), |
| 100 | + }, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }); |
| 104 | + |
| 105 | + expect(component.getDataCell(0, 3).getText()).toBe('AI Response 1'); |
| 106 | + expect(component.getDataCell(1, 3).getText()).toBe('AI Response 2'); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments