|
| 1 | +import $ from 'jquery'; |
| 2 | + |
| 3 | +import 'ui/html_editor'; |
| 4 | + |
| 5 | +import { openAIDialog } from '../../../helpers/aiToolbarMenu.js'; |
| 6 | +import { |
| 7 | + clickActionButton, |
| 8 | + setResultText, |
| 9 | + getResultText, |
| 10 | +} from '../../../helpers/aiDialog.js'; |
| 11 | +import { AI_DIALOG_CLASS } from '__internal/ui/html_editor/ui/aiDialog'; |
| 12 | + |
| 13 | +const setupHtmlEditorWithAi = (config) => { |
| 14 | + return $('#htmlEditor').dxHtmlEditor({ |
| 15 | + value: 'Test value', |
| 16 | + aiIntegration: {}, |
| 17 | + toolbar: { |
| 18 | + items: [{ |
| 19 | + name: 'ai', |
| 20 | + commands: ['summarize'] |
| 21 | + }], |
| 22 | + }, |
| 23 | + ...config |
| 24 | + }).dxHtmlEditor('instance'); |
| 25 | +}; |
| 26 | + |
| 27 | +const getAIDialog = (htmlEditor) => { |
| 28 | + return htmlEditor._aiDialog; |
| 29 | +}; |
| 30 | + |
| 31 | +const getAIDialogElement = ($htmlEditor) => { |
| 32 | + return $htmlEditor.find(`.${AI_DIALOG_CLASS}`); |
| 33 | +}; |
| 34 | + |
| 35 | +QUnit.module('AI dialog integration', {}, () => { |
| 36 | + QUnit.module('render', () => { |
| 37 | + ['ai', { name: 'ai' }].forEach(aiToolbarItem => { |
| 38 | + QUnit.test(`should be rendered if aiIntegration and ai toolbar item (as ${typeof aiToolbarItem}) are passed`, function(assert) { |
| 39 | + const $element = $('#htmlEditor').dxHtmlEditor({ |
| 40 | + aiIntegration: {}, |
| 41 | + toolbar: { items: [ aiToolbarItem ] }, |
| 42 | + }); |
| 43 | + const $dialog = getAIDialogElement($element); |
| 44 | + |
| 45 | + assert.strictEqual($dialog.length, 1, 'dialog is rendered'); |
| 46 | + }); |
| 47 | + |
| 48 | + QUnit.test('should not be rendered if aiIntegration is not passed and ai toolbar item is passed', function(assert) { |
| 49 | + const $element = $('#htmlEditor').dxHtmlEditor({ |
| 50 | + toolbar: { items: [ aiToolbarItem ] }, |
| 51 | + }); |
| 52 | + const $dialog = getAIDialogElement($element); |
| 53 | + |
| 54 | + assert.strictEqual($dialog.length, 0, 'dialog is not rendered'); |
| 55 | + }); |
| 56 | + |
| 57 | + QUnit.test('should not be rendered if ai toolbar item is not passed and aiIntegration is passed', function(assert) { |
| 58 | + const $element = $('#htmlEditor').dxHtmlEditor({ |
| 59 | + aiIntegration: {}, |
| 60 | + }); |
| 61 | + const $dialog = getAIDialogElement($element); |
| 62 | + |
| 63 | + assert.strictEqual($dialog.length, 0, 'dialog is not rendered'); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + QUnit.module('aiIntegration option', () => { |
| 69 | + QUnit.test('process update if ai dailog was rendered before', function(assert) { |
| 70 | + const htmlEditor = setupHtmlEditorWithAi({}); |
| 71 | + const dialog = getAIDialog(htmlEditor); |
| 72 | + const $dialog = getAIDialogElement(htmlEditor.$element()); |
| 73 | + const updateAIIntegrationSpy = sinon.spy(dialog, 'updateAIIntegration'); |
| 74 | + |
| 75 | + assert.strictEqual(updateAIIntegrationSpy.callCount, 0); |
| 76 | + assert.strictEqual($dialog.length, 1, 'dialog is rendered'); |
| 77 | + |
| 78 | + htmlEditor.option({ aiIntegration: {} }); |
| 79 | + |
| 80 | + assert.strictEqual(updateAIIntegrationSpy.callCount, 1, 'updateAIIntegration is called once'); |
| 81 | + }); |
| 82 | + |
| 83 | + QUnit.test('process update if ai dailog was not rendered before', function(assert) { |
| 84 | + const htmlEditor = setupHtmlEditorWithAi({ aiIntegration: null }); |
| 85 | + let $dialog = getAIDialogElement(htmlEditor.$element()); |
| 86 | + |
| 87 | + assert.strictEqual($dialog.length, 0, 'dialog is not rendered'); |
| 88 | + |
| 89 | + htmlEditor.option({ aiIntegration: {} }); |
| 90 | + |
| 91 | + $dialog = getAIDialogElement(htmlEditor.$element()); |
| 92 | + |
| 93 | + assert.strictEqual($dialog.length, 1, 'dialog is rendered'); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + QUnit.module('action buttons', () => { |
| 98 | + QUnit.test('replace button click should replace selected text with a text in result textArea', function(assert) { |
| 99 | + const done = assert.async(); |
| 100 | + |
| 101 | + const instance = setupHtmlEditorWithAi({ |
| 102 | + onValueChanged: () => { |
| 103 | + const value = instance.option('value'); |
| 104 | + assert.strictEqual(value, '<p>Inserted value</p>', 'value replaced'); |
| 105 | + done(); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + openAIDialog($('#htmlEditor')); |
| 110 | + setResultText('Inserted value'); |
| 111 | + clickActionButton('replace'); |
| 112 | + }); |
| 113 | + |
| 114 | + QUnit.test('insertAbove button click should insert text from result textArea above the selected text', function(assert) { |
| 115 | + const done = assert.async(); |
| 116 | + |
| 117 | + const instance = setupHtmlEditorWithAi({ |
| 118 | + onValueChanged: () => { |
| 119 | + const value = instance.option('value'); |
| 120 | + assert.strictEqual(value, '<p>Inserted value</p><p>Test value</p>', 'inserted above'); |
| 121 | + done(); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + openAIDialog($('#htmlEditor')); |
| 126 | + setResultText('Inserted value'); |
| 127 | + clickActionButton('insertAbove'); |
| 128 | + }); |
| 129 | + |
| 130 | + QUnit.test('insertBelow button click should insert text from result textArea below the selected text', function(assert) { |
| 131 | + const done = assert.async(); |
| 132 | + |
| 133 | + const instance = setupHtmlEditorWithAi({ |
| 134 | + onValueChanged: () => { |
| 135 | + const value = instance.option('value'); |
| 136 | + assert.strictEqual(value, '<p>Test value</p><p>Inserted value</p>', 'inserted below'); |
| 137 | + done(); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + openAIDialog($('#htmlEditor')); |
| 142 | + setResultText('Inserted value'); |
| 143 | + clickActionButton('insertBelow'); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + QUnit.module('input source based on selection', () => { |
| 148 | + QUnit.test('Should use selected text as input', function(assert) { |
| 149 | + const instance = setupHtmlEditorWithAi(); |
| 150 | + |
| 151 | + instance.setSelection(0, 4); |
| 152 | + |
| 153 | + openAIDialog($('#htmlEditor')); |
| 154 | + clickActionButton('replace'); |
| 155 | + |
| 156 | + const resultText = getResultText(); |
| 157 | + |
| 158 | + assert.strictEqual(resultText, 'Test', 'selected text used in resultTextArea'); |
| 159 | + }); |
| 160 | + |
| 161 | + QUnit.test('Should use all text as input if nothing is selected', function(assert) { |
| 162 | + const instance = setupHtmlEditorWithAi(); |
| 163 | + |
| 164 | + instance.setSelection(0, 0); |
| 165 | + |
| 166 | + openAIDialog($('#htmlEditor')); |
| 167 | + clickActionButton('replace'); |
| 168 | + |
| 169 | + const resultText = getResultText(); |
| 170 | + |
| 171 | + assert.strictEqual(resultText, 'Test value\n', 'all text used in resultTextArea'); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + QUnit.module('onValueChanged', () => { |
| 176 | + QUnit.test('should receive correct event parameter if value is updated after action button click', function(assert) { |
| 177 | + const done = assert.async(); |
| 178 | + |
| 179 | + setupHtmlEditorWithAi({ |
| 180 | + onValueChanged: ({ event }) => { |
| 181 | + const clickedText = $(event.target).text(); |
| 182 | + |
| 183 | + assert.strictEqual(event.type, 'dxclick', 'called with correct event type'); |
| 184 | + assert.strictEqual(clickedText, 'Replace', 'called on correct element'); |
| 185 | + done(); |
| 186 | + } |
| 187 | + }); |
| 188 | + |
| 189 | + openAIDialog($('#htmlEditor')); |
| 190 | + setResultText('Inserted value'); |
| 191 | + clickActionButton('replace'); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments