返回 DeepSeek-Reasonix
connection-title.test.tsx
根目录 / desktop / frontend / src / __tests__ / connection-title.test.tsx
1 import {JSDOM} from 'jsdom';
2 import React,{act} from 'react';
3 import assert from 'node:assert/strict';
4 import {LocaleProvider} from '../lib/i18n';
5 const dom=new JSDOM('<div id="root"></div>',{url:'http://localhost',pretendToBeVisual:true});
6 Object.assign(globalThis,{window:dom.window,document:dom.window.document,localStorage:dom.window.localStorage,requestAnimationFrame:dom.window.requestAnimationFrame.bind(dom.window),IS_REACT_ACT_ENVIRONMENT:true});
7 const {createRoot}=await import('react-dom/client');
8 const {ConnectionTitle}=await import('../components/ConnectionTitle');
9 const root=createRoot(document.getElementById('root')!);
10 let calls=0, success=false;
11 await act(async()=>root.render(<LocaleProvider><ConnectionTitle label="My connection" busy={false} onSave={async label=>{assert.equal(label,'My connection');calls++;return success;}}/></LocaleProvider>));
12 await act(async()=>document.querySelector('button')!.click());
13 assert.ok(document.querySelector('input'));
14 await act(async()=>document.querySelector('input')!.dispatchEvent(new dom.window.FocusEvent('blur',{bubbles:true})));
15 assert.equal(calls,0);
16 await act(async()=>document.querySelector('input')!.dispatchEvent(new dom.window.KeyboardEvent('keydown',{key:'Escape',bubbles:true})));
17 assert.equal(document.querySelector('input'),null);assert.equal(calls,0);
18 await act(async()=>document.querySelector('button')!.click());
19 await act(async()=>document.querySelector('input')!.dispatchEvent(new dom.window.KeyboardEvent('keydown',{key:'Enter',bubbles:true})));
20 assert.equal(calls,1);assert.ok(document.querySelector('[role="alert"]'));assert.equal(document.querySelector('input')!.value,'My connection');
21 success=true;
22 await act(async()=> (document.querySelector('.connection-title-editor__row button') as HTMLButtonElement).click());
23 assert.equal(calls,2);assert.equal(document.querySelector('input'),null);
24 await act(async()=>root.unmount());
25 console.log('PASS: explicit save, Escape cancel, no blur submission, failed save retains draft, retry closes editor');
26
26 lines Plain Text