tabs.spec.tsx 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. import React, { FC, useEffect, useRef, useState } from 'react'
  2. import { EditorProviders } from '../../../helpers/editor-providers'
  3. import { TabsContainer } from '../../../../../frontend/js/features/source-editor/components/tabs/tabs-container'
  4. import {
  5. FileTreeDocumentFindResult,
  6. FileTreeFileRefFindResult,
  7. } from '@/features/ide-react/types/file-tree'
  8. import {
  9. EditorManager,
  10. EditorManagerContext,
  11. } from '@/features/ide-react/context/editor-manager-context'
  12. import { TAB_TRANSFER_TYPE } from '@/features/ide-react/context/tabs-context'
  13. import {
  14. EditorViewContext,
  15. useEditorViewContext,
  16. } from '@/features/ide-react/context/editor-view-context'
  17. import { EditorView } from '@codemirror/view'
  18. import { EditorState, Transaction } from '@codemirror/state'
  19. import { tabsListener } from '@/features/source-editor/extensions/tabs-listener'
  20. import ReviewPanelTabsHeaderPortal from '@/features/review-panel/components/review-panel-tabs-header-portal'
  21. import { FileTree } from '@/features/ide-react/components/file-tree'
  22. import { PROJECT_ID } from '../../../helpers/editor-providers'
  23. const DOC_IDS = {
  24. main: 'doc-main-id',
  25. intro: 'doc-intro-id',
  26. appendix: 'doc-appendix-id',
  27. bibFile: 'file-bib-id',
  28. introA: 'doc-main-a',
  29. introB: 'doc-main-b',
  30. }
  31. const FOLDER_IDS = {
  32. chapterA: 'folder-chapter-a',
  33. chapterB: 'folder-chapter-b',
  34. }
  35. const DOC_NAMES: Record<string, string> = {
  36. [DOC_IDS.main]: 'main.tex',
  37. [DOC_IDS.intro]: 'intro.tex',
  38. [DOC_IDS.appendix]: 'appendix.tex',
  39. [DOC_IDS.bibFile]: 'refs.bib',
  40. [DOC_IDS.introA]: 'intro.tex',
  41. [DOC_IDS.introB]: 'intro.tex',
  42. }
  43. function makeRootFolder(
  44. docs: { _id: string; name: string }[] = [],
  45. folders: any[] = [],
  46. fileRefs: { _id: string; name: string }[] = []
  47. ) {
  48. return [
  49. {
  50. _id: 'root-folder-id',
  51. name: 'rootFolder',
  52. docs,
  53. folders,
  54. fileRefs,
  55. },
  56. ]
  57. }
  58. const defaultDocs = [
  59. { _id: DOC_IDS.main, name: 'main.tex' },
  60. { _id: DOC_IDS.intro, name: 'intro.tex' },
  61. { _id: DOC_IDS.appendix, name: 'appendix.tex' },
  62. ]
  63. const defaultRootFolder = makeRootFolder(defaultDocs)
  64. function makeDocEntity(
  65. id: string,
  66. name: string,
  67. path: string[] = ['root-folder-id']
  68. ): FileTreeDocumentFindResult {
  69. return {
  70. type: 'doc',
  71. entity: { _id: id, name },
  72. parent: [],
  73. parentFolderId: path[path.length - 1],
  74. path,
  75. index: 0,
  76. }
  77. }
  78. function makeFileRefEntity(
  79. id: string,
  80. name: string
  81. ): FileTreeFileRefFindResult {
  82. return {
  83. type: 'fileRef',
  84. entity: {
  85. _id: id,
  86. name,
  87. linkedFileData: undefined,
  88. created: new Date().toISOString(),
  89. hash: 'abc123',
  90. },
  91. parent: [],
  92. parentFolderId: 'root-folder-id',
  93. path: ['root-folder-id'],
  94. index: 0,
  95. }
  96. }
  97. function makeEditorManagerProvider() {
  98. const EditorManagerProvider: FC<React.PropsWithChildren> = ({ children }) => {
  99. const value = {
  100. getEditorType: () => null,
  101. getCurrentDocValue: () => null,
  102. getCurrentDocumentId: () => null,
  103. setIgnoringExternalUpdates: () => {},
  104. openDocWithId: cy.stub().as('openDocWithId').resolves(),
  105. openDoc: cy.stub().as('openDoc').resolves(),
  106. openDocs: { awaitBufferedOps: cy.stub().resolves() } as any,
  107. openFileWithId: cy.stub().as('openFileWithId'),
  108. openInitialDoc: cy.stub().resolves(),
  109. isLoading: false,
  110. jumpToLine: () => {},
  111. debugTimers: { current: {} },
  112. } as unknown as EditorManager
  113. return (
  114. <EditorManagerContext.Provider value={value}>
  115. {children}
  116. </EditorManagerContext.Provider>
  117. )
  118. }
  119. return EditorManagerProvider
  120. }
  121. function makeEditorViewProvider() {
  122. const EditorViewProvider: FC<React.PropsWithChildren> = ({ children }) => {
  123. const parentRef = useRef<HTMLDivElement>(null)
  124. const [view, setView] = useState<EditorView | null>(null)
  125. useEffect(() => {
  126. if (!parentRef.current) return
  127. const editorView = new EditorView({
  128. state: EditorState.create({
  129. extensions: [
  130. tabsListener(true),
  131. EditorView.contentAttributes.of({
  132. 'data-testid': 'mock-editor-view',
  133. }),
  134. ],
  135. }),
  136. parent: parentRef.current,
  137. })
  138. setView(editorView)
  139. return () => editorView.destroy()
  140. }, [])
  141. return (
  142. <EditorViewContext.Provider value={{ view, setView: () => {} }}>
  143. {children}
  144. <div ref={parentRef} />
  145. </EditorViewContext.Provider>
  146. )
  147. }
  148. return EditorViewProvider
  149. }
  150. function RemoteChangeButton() {
  151. const { view } = useEditorViewContext()
  152. return (
  153. <button
  154. type="button"
  155. onClick={() =>
  156. view?.dispatch({
  157. changes: { from: 0, insert: 'remote text' },
  158. annotations: Transaction.remote.of(true),
  159. })
  160. }
  161. >
  162. Add a remote change
  163. </button>
  164. )
  165. }
  166. function expandFolders(path: string[] = []) {
  167. // path[0] is the root folder, so skip that one
  168. path.slice(1).forEach(folderId => {
  169. cy.get(`[data-file-id="${folderId}"] .file-tree-entity-button`).click()
  170. })
  171. }
  172. // Target rows by entity id rather than accessible name so that entities sharing
  173. // a name stay unambiguous.
  174. function selectEntity(
  175. entity: FileTreeDocumentFindResult | FileTreeFileRefFindResult
  176. ) {
  177. expandFolders(entity.path)
  178. cy.get(`[data-file-id="${entity.entity._id}"]`).click()
  179. cy.get(`[data-tab-id="${entity.entity._id}"]`).should('exist')
  180. }
  181. function selectDoc(id: string, path?: string[]) {
  182. selectEntity(makeDocEntity(id, DOC_NAMES[id], path))
  183. }
  184. function enableEditorTabs() {
  185. cy.window().then(win => {
  186. win.metaAttributesCache.set('ol-splitTestVariants', {
  187. 'editor-tabs': 'enabled',
  188. })
  189. win.metaAttributesCache.set('ol-labsExperiments', ['editor-tabs'])
  190. })
  191. }
  192. describe('File Tabs', function () {
  193. function mountTabs(options?: { rootFolder?: any; userSettings?: any }) {
  194. const rootFolder = options?.rootFolder ?? defaultRootFolder
  195. cy.mount(
  196. <EditorProviders
  197. rootFolder={rootFolder as any}
  198. rootDocId={DOC_IDS.main}
  199. userSettings={options?.userSettings}
  200. providers={{
  201. EditorManagerProvider: makeEditorManagerProvider(),
  202. EditorViewProvider: makeEditorViewProvider(),
  203. }}
  204. >
  205. <TabsContainer />
  206. <FileTree />
  207. <ReviewPanelTabsHeaderPortal />
  208. <RemoteChangeButton />
  209. </EditorProviders>
  210. )
  211. }
  212. beforeEach(function () {
  213. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  214. cy.interceptEvents()
  215. cy.interceptTutorials()
  216. cy.interceptCompile()
  217. enableEditorTabs()
  218. // Clear persisted tab state from localStorage
  219. cy.window().then(win => {
  220. Object.keys(win.localStorage).forEach(key => {
  221. if (key.startsWith('open-tabs:') || key.startsWith('folder.')) {
  222. win.localStorage.removeItem(key)
  223. }
  224. })
  225. // Pointing doc.open_id at a non-existent entity stops the file tree
  226. // auto-selecting
  227. win.localStorage.setItem(
  228. `doc.open_id.${PROJECT_ID}`,
  229. JSON.stringify('no-open-doc')
  230. )
  231. })
  232. cy.window().then(win => {
  233. win.metaAttributesCache.set('ol-user', { id: 'user1' })
  234. })
  235. mountTabs()
  236. cy.findByTestId('mock-editor-view').as('editorView')
  237. })
  238. describe('Initial file selection', function () {
  239. it('automatically creates a tab for the initially opened file', function () {
  240. cy.window().then(win => {
  241. win.localStorage.setItem(
  242. `doc.open_id.${PROJECT_ID}`,
  243. JSON.stringify(DOC_IDS.intro)
  244. )
  245. })
  246. mountTabs()
  247. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  248. })
  249. })
  250. describe('Tab display', function () {
  251. it('displays a tab when a file is selected', function () {
  252. cy.then(() => selectDoc(DOC_IDS.main))
  253. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  254. })
  255. it('marks the selected tab as active', function () {
  256. cy.then(() => selectDoc(DOC_IDS.main))
  257. cy.findByRole('tab', { name: /main\.tex/ })
  258. .should('have.attr', 'aria-selected', 'true')
  259. .and('have.class', 'tab-selected')
  260. })
  261. })
  262. describe('Opening tabs on file change', function () {
  263. it('opens a new tab when a different file is selected', function () {
  264. cy.then(() => selectDoc(DOC_IDS.main))
  265. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  266. // Make main permanent (keypress) so selecting another file doesn't replace it
  267. cy.get('@editorView').type('a')
  268. // Select another file
  269. cy.then(() => selectDoc(DOC_IDS.intro))
  270. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  271. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  272. })
  273. it('does not duplicate a tab when the same file is selected again', function () {
  274. cy.then(() => selectDoc(DOC_IDS.main))
  275. cy.findAllByRole('tab').should('have.length', 1)
  276. // Select the same file again
  277. cy.then(() => selectDoc(DOC_IDS.main))
  278. cy.findAllByRole('tab').should('have.length', 1)
  279. })
  280. })
  281. describe('Temporary tabs', function () {
  282. beforeEach(function () {
  283. mountTabs({ userSettings: { previewTabs: true } })
  284. })
  285. it('opens a newly selected file as a temporary tab', function () {
  286. cy.then(() => selectDoc(DOC_IDS.main))
  287. // The first tab is temporary until a keypress
  288. cy.findByRole('tab', { name: /main\.tex/ }).should(
  289. 'have.class',
  290. 'tab-temporary'
  291. )
  292. })
  293. it('makes a temporary tab permanent when a key is pressed', function () {
  294. cy.then(() => selectDoc(DOC_IDS.main))
  295. cy.findByRole('tab', { name: /main\.tex/ }).should(
  296. 'have.class',
  297. 'tab-temporary'
  298. )
  299. cy.get('@editorView').type('a')
  300. cy.findByRole('tab', { name: /main\.tex/ }).should(
  301. 'not.have.class',
  302. 'tab-temporary'
  303. )
  304. })
  305. it('replaces a temporary tab when selecting yet another file', function () {
  306. // Open main (temporary)
  307. cy.then(() => selectDoc(DOC_IDS.main))
  308. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  309. // Make main permanent
  310. cy.get('@editorView').type('a')
  311. // Open intro (temporary)
  312. cy.then(() => selectDoc(DOC_IDS.intro))
  313. cy.findByRole('tab', { name: /intro\.tex/ }).should(
  314. 'have.class',
  315. 'tab-temporary'
  316. )
  317. // Open appendix — should replace temporary intro
  318. cy.then(() => selectDoc(DOC_IDS.appendix))
  319. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  320. cy.findByRole('tab', { name: /appendix\.tex/ }).should('exist')
  321. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  322. })
  323. it('does not make a temporary tab permanent on remote changes', function () {
  324. cy.then(() => selectDoc(DOC_IDS.main))
  325. cy.findByRole('tab', { name: /main\.tex/ }).should(
  326. 'have.class',
  327. 'tab-temporary'
  328. )
  329. cy.findByRole('button', { name: 'Add a remote change' }).click()
  330. cy.findByRole('tab', { name: /main\.tex/ }).should(
  331. 'have.class',
  332. 'tab-temporary'
  333. )
  334. })
  335. it('makes a temporary tab permanent on double-click', function () {
  336. cy.then(() => selectDoc(DOC_IDS.main))
  337. cy.findByRole('tab', { name: /main\.tex/ }).should(
  338. 'have.class',
  339. 'tab-temporary'
  340. )
  341. cy.findByRole('tab', { name: /main\.tex/ }).dblclick()
  342. cy.findByRole('tab', { name: /main\.tex/ }).should(
  343. 'not.have.class',
  344. 'tab-temporary'
  345. )
  346. })
  347. it('makes a temporary tab permanent on double-clicking its file tree entry', function () {
  348. cy.then(() => selectDoc(DOC_IDS.main))
  349. cy.findByRole('tab', { name: /main\.tex/ }).should(
  350. 'have.class',
  351. 'tab-temporary'
  352. )
  353. cy.findByRole('treeitem', { name: 'main.tex' }).dblclick()
  354. cy.findByRole('tab', { name: /main\.tex/ }).should(
  355. 'not.have.class',
  356. 'tab-temporary'
  357. )
  358. })
  359. it('opens a new tab as permanent when previewTabs is disabled', function () {
  360. mountTabs({ userSettings: { previewTabs: false } })
  361. cy.then(() => selectDoc(DOC_IDS.main))
  362. cy.findByRole('tab', { name: /main\.tex/ }).should(
  363. 'not.have.class',
  364. 'tab-temporary'
  365. )
  366. })
  367. })
  368. describe('Closing tabs', function () {
  369. it('closes a tab via the close button', function () {
  370. // Open main
  371. cy.then(() => selectDoc(DOC_IDS.main))
  372. // Open intro
  373. cy.then(() => selectDoc(DOC_IDS.intro))
  374. cy.findAllByRole('tab').should('have.length', 2)
  375. // Close intro
  376. cy.findByRole('tab', { name: /intro\.tex/ }).within(() => {
  377. cy.findByRole('button', { name: 'Close' }).click()
  378. })
  379. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  380. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  381. })
  382. it('cannot close the last remaining tab', function () {
  383. cy.then(() => selectDoc(DOC_IDS.main))
  384. cy.findAllByRole('tab').should('have.length', 1)
  385. cy.findByRole('tab', { name: /main\.tex/ }).within(() => {
  386. cy.findByRole('button', { name: 'Close' }).should('be.disabled')
  387. })
  388. })
  389. it('switches to an adjacent tab when closing the currently active tab', function () {
  390. // Open three tabs
  391. cy.then(() => selectDoc(DOC_IDS.main))
  392. cy.then(() => selectDoc(DOC_IDS.intro))
  393. cy.then(() => selectDoc(DOC_IDS.appendix))
  394. cy.findAllByRole('tab').should('have.length', 3)
  395. // Close the currently selected tab (appendix — the last one selected)
  396. cy.findByRole('tab', { name: /appendix\.tex/ }).within(() => {
  397. cy.findByRole('button', { name: 'Close' }).click()
  398. })
  399. cy.findByRole('tab', { name: /appendix\.tex/ }).should('not.exist')
  400. // The context should have tried to open an adjacent tab
  401. cy.get('@openDocWithId').should('have.been.calledWith', DOC_IDS.intro)
  402. })
  403. it('closes a tab on middle-click', function () {
  404. // Open two tabs
  405. cy.then(() => selectDoc(DOC_IDS.main))
  406. cy.then(() => selectDoc(DOC_IDS.intro))
  407. cy.findAllByRole('tab').should('have.length', 2)
  408. // Middle-click intro tab
  409. cy.findByRole('tab', { name: /intro\.tex/ }).trigger('mouseup', {
  410. button: 1,
  411. })
  412. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  413. })
  414. })
  415. describe('Context menu', function () {
  416. it('opens the context menu on right-click of a tab', function () {
  417. cy.then(() => selectDoc(DOC_IDS.main))
  418. cy.then(() => selectDoc(DOC_IDS.intro))
  419. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  420. cy.findByRole('menu').should('exist')
  421. cy.findByRole('menuitem', { name: 'Close' }).should('exist')
  422. cy.findByRole('menuitem', { name: 'Close other tabs' }).should('exist')
  423. cy.findByRole('menuitem', { name: 'Close tabs to the right' }).should(
  424. 'exist'
  425. )
  426. cy.findByRole('menuitem', { name: 'Tab settings…' }).should('exist')
  427. })
  428. it('closes the clicked tab via "Close"', function () {
  429. cy.then(() => selectDoc(DOC_IDS.main))
  430. cy.then(() => selectDoc(DOC_IDS.intro))
  431. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  432. cy.findByRole('menuitem', { name: 'Close' }).click()
  433. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  434. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  435. cy.findByRole('menu').should('not.exist')
  436. })
  437. it('closes all other tabs via "Close other tabs"', function () {
  438. cy.then(() => selectDoc(DOC_IDS.main))
  439. cy.then(() => selectDoc(DOC_IDS.intro))
  440. cy.then(() => selectDoc(DOC_IDS.appendix))
  441. cy.findAllByRole('tab').should('have.length', 3)
  442. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  443. cy.findByRole('menuitem', { name: 'Close other tabs' }).click()
  444. cy.findAllByRole('tab').should('have.length', 1)
  445. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  446. })
  447. it('navigates to the target tab when closing others from a non-active tab', function () {
  448. cy.then(() => selectDoc(DOC_IDS.main))
  449. cy.then(() => selectDoc(DOC_IDS.intro))
  450. cy.then(() => selectDoc(DOC_IDS.appendix))
  451. // appendix is the currently active tab; close others from intro
  452. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  453. cy.findByRole('menuitem', { name: 'Close other tabs' }).click()
  454. cy.get('@openDocWithId').should('have.been.calledWith', DOC_IDS.intro)
  455. })
  456. it('closes tabs to the right of the target via "Close tabs to the right"', function () {
  457. cy.then(() => selectDoc(DOC_IDS.main))
  458. cy.then(() => selectDoc(DOC_IDS.intro))
  459. cy.then(() => selectDoc(DOC_IDS.appendix))
  460. cy.findAllByRole('tab').should('have.length', 3)
  461. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  462. cy.findByRole('menuitem', { name: 'Close tabs to the right' }).click()
  463. cy.findAllByRole('tab').should('have.length', 2)
  464. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  465. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  466. cy.findByRole('tab', { name: /appendix\.tex/ }).should('not.exist')
  467. })
  468. it('navigates to the target tab when the active tab is closed to the right', function () {
  469. cy.then(() => selectDoc(DOC_IDS.main))
  470. cy.then(() => selectDoc(DOC_IDS.intro))
  471. cy.then(() => selectDoc(DOC_IDS.appendix))
  472. // appendix is the active tab; closing to the right of intro removes it
  473. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  474. cy.findByRole('menuitem', { name: 'Close tabs to the right' }).click()
  475. cy.get('@openDocWithId').should('have.been.calledWith', DOC_IDS.intro)
  476. })
  477. it('keeps the active tab selected when it is to the left of the target', function () {
  478. cy.then(() => selectDoc(DOC_IDS.main))
  479. cy.then(() => selectDoc(DOC_IDS.intro))
  480. cy.then(() => selectDoc(DOC_IDS.appendix))
  481. // Re-select main so it becomes the active tab again (still at index 0)
  482. cy.then(() => selectDoc(DOC_IDS.main))
  483. // Closing to the right of intro removes appendix but not the active tab
  484. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  485. cy.findByRole('menuitem', { name: 'Close tabs to the right' }).click()
  486. cy.findAllByRole('tab').should('have.length', 2)
  487. cy.findByRole('tab', { name: /appendix\.tex/ }).should('not.exist')
  488. cy.findByRole('tab', { name: /main\.tex/ }).should(
  489. 'have.attr',
  490. 'aria-selected',
  491. 'true'
  492. )
  493. })
  494. it('disables the close actions when only one tab is open', function () {
  495. cy.then(() => selectDoc(DOC_IDS.main))
  496. cy.findByRole('tab', { name: /main\.tex/ }).rightclick()
  497. cy.findByRole('menuitem', { name: 'Close' }).should(
  498. 'have.attr',
  499. 'aria-disabled',
  500. 'true'
  501. )
  502. cy.findByRole('menuitem', { name: 'Close other tabs' }).should(
  503. 'have.attr',
  504. 'aria-disabled',
  505. 'true'
  506. )
  507. cy.findByRole('menuitem', { name: 'Close tabs to the right' }).should(
  508. 'have.attr',
  509. 'aria-disabled',
  510. 'true'
  511. )
  512. })
  513. it('disables "Close tabs to the right" when the target is the last tab', function () {
  514. cy.then(() => selectDoc(DOC_IDS.main))
  515. cy.then(() => selectDoc(DOC_IDS.intro))
  516. cy.then(() => selectDoc(DOC_IDS.appendix))
  517. // appendix is the rightmost tab, so there is nothing to close to its right
  518. cy.findByRole('tab', { name: /appendix\.tex/ }).rightclick()
  519. cy.findByRole('menuitem', { name: 'Close tabs to the right' }).should(
  520. 'have.attr',
  521. 'aria-disabled',
  522. 'true'
  523. )
  524. // The other actions remain enabled with more than one tab open
  525. cy.findByRole('menuitem', { name: 'Close' }).should(
  526. 'not.have.attr',
  527. 'aria-disabled',
  528. 'true'
  529. )
  530. cy.findByRole('menuitem', { name: 'Close other tabs' }).should(
  531. 'not.have.attr',
  532. 'aria-disabled',
  533. 'true'
  534. )
  535. })
  536. it('opens tab settings via "Tab settings…"', function () {
  537. cy.then(() => selectDoc(DOC_IDS.main))
  538. cy.then(() => selectDoc(DOC_IDS.intro))
  539. cy.window().then(win => {
  540. cy.spy(win, 'dispatchEvent').as('dispatchEvent')
  541. })
  542. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  543. cy.findByRole('menuitem', { name: 'Tab settings…' }).click()
  544. // opens the settings modal...
  545. cy.get('@dispatchEvent').should(
  546. 'have.been.calledWithMatch',
  547. Cypress.sinon.match({ type: 'ui.toggle-settings', detail: true })
  548. )
  549. // ...and focuses the editorTabs setting
  550. cy.get('@dispatchEvent').should(
  551. 'have.been.calledWithMatch',
  552. Cypress.sinon.match({ type: 'ui.focus-setting', detail: 'editorTabs' })
  553. )
  554. // and the context menu closes
  555. cy.findByRole('menu').should('not.exist')
  556. })
  557. it('closes the menu on Escape', function () {
  558. cy.then(() => selectDoc(DOC_IDS.main))
  559. cy.then(() => selectDoc(DOC_IDS.intro))
  560. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  561. cy.findByRole('menu').should('exist')
  562. cy.findByRole('menu').trigger('keydown', {
  563. key: 'Escape',
  564. })
  565. cy.findByRole('menu').should('not.exist')
  566. })
  567. it('closes the menu on right-click outside', function () {
  568. cy.then(() => selectDoc(DOC_IDS.main))
  569. cy.then(() => selectDoc(DOC_IDS.intro))
  570. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  571. cy.findByRole('menu').should('exist')
  572. // Right-click outside any tab
  573. cy.get('.editor-tabs-container').rightclick('right')
  574. cy.findByRole('menu').should('not.exist')
  575. })
  576. it('focuses the menu when opening', function () {
  577. cy.then(() => selectDoc(DOC_IDS.main))
  578. cy.then(() => selectDoc(DOC_IDS.intro))
  579. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  580. cy.findByRole('menu').should('be.focused')
  581. })
  582. it('returns focus to the originating tab when closing', function () {
  583. cy.then(() => selectDoc(DOC_IDS.main))
  584. cy.then(() => selectDoc(DOC_IDS.intro))
  585. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  586. cy.findByRole('menu').trigger('keydown', { key: 'Escape' })
  587. cy.findByRole('tab', { name: /intro\.tex/ }).should('be.focused')
  588. })
  589. it('moves the menu to another tab on right-click', function () {
  590. cy.then(() => selectDoc(DOC_IDS.main))
  591. cy.then(() => selectDoc(DOC_IDS.intro))
  592. cy.then(() => selectDoc(DOC_IDS.appendix))
  593. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  594. cy.findByRole('menu').should('exist')
  595. // Right-click appendix the menu should retarget to that tab
  596. cy.findByRole('tab', { name: /appendix\.tex/ }).rightclick({
  597. force: true,
  598. })
  599. cy.findByRole('menuitem', { name: 'Close other tabs' }).click()
  600. cy.findAllByRole('tab').should('have.length', 1)
  601. cy.findByRole('tab', { name: /appendix\.tex/ }).should('exist')
  602. })
  603. it('should not open the context menu if shift is held', function () {
  604. cy.then(() => selectDoc(DOC_IDS.intro))
  605. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick({
  606. shiftKey: true,
  607. })
  608. cy.findByRole('menu').should('not.exist')
  609. })
  610. it('should close already open context menu if shift is held', function () {
  611. cy.then(() => selectDoc(DOC_IDS.intro))
  612. cy.then(() => selectDoc(DOC_IDS.main))
  613. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  614. cy.findByRole('menu').should('exist')
  615. cy.findByRole('tab', { name: /main\.tex/ }).rightclick({
  616. force: true,
  617. shiftKey: true,
  618. })
  619. cy.findByRole('menu').should('not.exist')
  620. })
  621. })
  622. describe('Tab interaction', function () {
  623. it('calls openDocWithId when clicking a non-selected doc tab', function () {
  624. // Open two tabs
  625. cy.then(() => selectDoc(DOC_IDS.main))
  626. cy.then(() => selectDoc(DOC_IDS.intro))
  627. // Click main tab
  628. cy.findByRole('tab', { name: /main\.tex/ }).click()
  629. cy.get('@openDocWithId').should('have.been.calledWith', DOC_IDS.main)
  630. })
  631. it('calls openFileWithId when clicking a non-selected fileRef tab', function () {
  632. const rootFolder = makeRootFolder(
  633. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  634. [],
  635. [{ _id: DOC_IDS.bibFile, name: 'refs.bib' }]
  636. )
  637. mountTabs({ rootFolder })
  638. // Open main doc
  639. cy.then(() => selectDoc(DOC_IDS.main))
  640. // Open fileRef
  641. cy.then(() =>
  642. selectEntity(makeFileRefEntity(DOC_IDS.bibFile, 'refs.bib'))
  643. )
  644. // Switch back to main so refs.bib is no longer selected
  645. cy.findByRole('tab', { name: /main\.tex/ }).click()
  646. // Click the fileRef tab
  647. cy.findByRole('tab', { name: /refs\.bib/ }).click()
  648. cy.get('@openFileWithId').should('have.been.calledWith', DOC_IDS.bibFile)
  649. })
  650. })
  651. describe('Drag and drop', function () {
  652. it('reorders tabs by dragging to the right of another tab', function () {
  653. // Open three tabs
  654. cy.then(() => selectDoc(DOC_IDS.main))
  655. cy.then(() => selectDoc(DOC_IDS.intro))
  656. cy.then(() => selectDoc(DOC_IDS.appendix))
  657. cy.findAllByRole('tab').should('have.length', 3)
  658. // Verify initial order
  659. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  660. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  661. cy.findAllByRole('tab').eq(2).should('contain.text', 'appendix.tex')
  662. // Drag main to the right of appendix
  663. const dataTransfer = new DataTransfer()
  664. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  665. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  666. dataTransfer,
  667. })
  668. cy.findByRole('tab', { name: /appendix\.tex/ }).then($el => {
  669. const rect = $el[0].getBoundingClientRect()
  670. cy.wrap($el).trigger('dragover', {
  671. dataTransfer,
  672. clientX: rect.left + rect.width * 0.75,
  673. })
  674. cy.wrap($el).trigger('drop', {
  675. dataTransfer,
  676. clientX: rect.left + rect.width * 0.75,
  677. })
  678. })
  679. // Expected order after move: intro, appendix, main
  680. cy.findAllByRole('tab').eq(0).should('contain.text', 'intro.tex')
  681. cy.findAllByRole('tab').eq(1).should('contain.text', 'appendix.tex')
  682. cy.findAllByRole('tab').eq(2).should('contain.text', 'main.tex')
  683. })
  684. it('reorders tabs by dragging to the left of another tab', function () {
  685. // Open three tabs
  686. cy.then(() => selectDoc(DOC_IDS.main))
  687. cy.then(() => selectDoc(DOC_IDS.intro))
  688. cy.then(() => selectDoc(DOC_IDS.appendix))
  689. // Verify initial order
  690. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  691. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  692. cy.findAllByRole('tab').eq(2).should('contain.text', 'appendix.tex')
  693. // Drag appendix to the left of main
  694. const dataTransfer = new DataTransfer()
  695. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.appendix)
  696. cy.findByRole('tab', { name: /appendix\.tex/ }).trigger('dragstart', {
  697. dataTransfer,
  698. })
  699. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  700. const rect = $el[0].getBoundingClientRect()
  701. cy.wrap($el).trigger('dragover', {
  702. dataTransfer,
  703. clientX: rect.left + rect.width * 0.25,
  704. })
  705. cy.wrap($el).trigger('drop', {
  706. dataTransfer,
  707. clientX: rect.left + rect.width * 0.25,
  708. })
  709. })
  710. // Expected order: appendix, main, intro
  711. cy.findAllByRole('tab').eq(0).should('contain.text', 'appendix.tex')
  712. cy.findAllByRole('tab').eq(1).should('contain.text', 'main.tex')
  713. cy.findAllByRole('tab').eq(2).should('contain.text', 'intro.tex')
  714. })
  715. it('shows a drop indicator when dragging over a tab', function () {
  716. // Open two tabs
  717. cy.then(() => selectDoc(DOC_IDS.main))
  718. cy.then(() => selectDoc(DOC_IDS.intro))
  719. const dataTransfer = new DataTransfer()
  720. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.intro)
  721. cy.findByRole('tab', { name: /intro\.tex/ }).trigger('dragstart', {
  722. dataTransfer,
  723. })
  724. // Drag over left side of main tab
  725. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  726. const rect = $el[0].getBoundingClientRect()
  727. cy.wrap($el).trigger('dragover', {
  728. dataTransfer,
  729. clientX: rect.left + rect.width * 0.25,
  730. })
  731. })
  732. cy.findByRole('tab', { name: /main\.tex/ }).should(
  733. 'have.class',
  734. 'tab-drop-left'
  735. )
  736. // Drag leave should clear the indicator
  737. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragleave')
  738. cy.findByRole('tab', { name: /main\.tex/ }).should(
  739. 'not.have.class',
  740. 'tab-drop-left'
  741. )
  742. })
  743. it('drops onto the tablist to move a tab to the end', function () {
  744. // Open three tabs
  745. cy.then(() => selectDoc(DOC_IDS.main))
  746. cy.then(() => selectDoc(DOC_IDS.intro))
  747. cy.then(() => selectDoc(DOC_IDS.appendix))
  748. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  749. // Drag main to the tablist gutter (right of last tab)
  750. const dataTransfer = new DataTransfer()
  751. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  752. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  753. dataTransfer,
  754. })
  755. cy.findByRole('tablist').trigger('dragover', { dataTransfer })
  756. cy.findByRole('tablist').trigger('drop', { dataTransfer })
  757. // main should now be last
  758. cy.findAllByRole('tab').last().should('contain.text', 'main.tex')
  759. })
  760. it('does nothing when dropping a tab on itself', function () {
  761. // Open two tabs
  762. cy.then(() => selectDoc(DOC_IDS.main))
  763. cy.then(() => selectDoc(DOC_IDS.intro))
  764. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  765. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  766. // Drag main onto itself
  767. const dataTransfer = new DataTransfer()
  768. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  769. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  770. dataTransfer,
  771. })
  772. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  773. const rect = $el[0].getBoundingClientRect()
  774. cy.wrap($el).trigger('dragover', {
  775. dataTransfer,
  776. clientX: rect.left + rect.width * 0.75,
  777. })
  778. cy.wrap($el).trigger('drop', {
  779. dataTransfer,
  780. clientX: rect.left + rect.width * 0.75,
  781. })
  782. })
  783. // Order unchanged
  784. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  785. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  786. })
  787. })
  788. describe('Path disambiguation for duplicate names', function () {
  789. const duplicateNameRootFolder = makeRootFolder(
  790. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  791. [
  792. {
  793. _id: FOLDER_IDS.chapterA,
  794. name: 'chapter-a',
  795. docs: [{ _id: DOC_IDS.introA, name: 'intro.tex' }],
  796. folders: [],
  797. fileRefs: [],
  798. },
  799. {
  800. _id: FOLDER_IDS.chapterB,
  801. name: 'chapter-b',
  802. docs: [{ _id: DOC_IDS.introB, name: 'intro.tex' }],
  803. folders: [],
  804. fileRefs: [],
  805. },
  806. ]
  807. )
  808. it('shows disambiguated paths when two files share the same name', function () {
  809. mountTabs({ rootFolder: duplicateNameRootFolder })
  810. // Open main.tex (unique name, no disambiguation needed)
  811. cy.then(() => selectDoc(DOC_IDS.main))
  812. // Open intro.tex from chapter-a
  813. cy.then(() =>
  814. selectDoc(DOC_IDS.introA, ['root-folder-id', FOLDER_IDS.chapterA])
  815. )
  816. // Open intro.tex from chapter-b
  817. cy.then(() =>
  818. selectDoc(DOC_IDS.introB, ['root-folder-id', FOLDER_IDS.chapterB])
  819. )
  820. cy.findAllByRole('tab').should('have.length', 3)
  821. // main.tex is unique — should display without a path prefix
  822. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  823. // The two intro.tex tabs should be disambiguated with their folder names
  824. cy.findAllByRole('tab')
  825. .eq(1)
  826. .should('contain.text', 'chapter-a/intro.tex')
  827. cy.findAllByRole('tab')
  828. .eq(2)
  829. .should('contain.text', 'chapter-b/intro.tex')
  830. })
  831. it('uses only the file name when there is no collision', function () {
  832. mountTabs({ rootFolder: duplicateNameRootFolder })
  833. // Open just one of the duplicate-named files
  834. cy.then(() =>
  835. selectDoc(DOC_IDS.introA, ['root-folder-id', FOLDER_IDS.chapterA])
  836. )
  837. // With only one intro.tex open, no disambiguation is needed
  838. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  839. cy.findByRole('tab', { name: /intro\.tex/ }).should(
  840. 'not.contain.text',
  841. 'chapter-a/'
  842. )
  843. })
  844. })
  845. describe('Multiple file types', function () {
  846. it('displays tabs for both docs and file refs', function () {
  847. const rootFolder = makeRootFolder(
  848. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  849. [],
  850. [{ _id: DOC_IDS.bibFile, name: 'refs.bib' }]
  851. )
  852. mountTabs({ rootFolder })
  853. // Open main doc
  854. cy.then(() => selectDoc(DOC_IDS.main))
  855. // Open a fileRef
  856. cy.then(() =>
  857. selectEntity(makeFileRefEntity(DOC_IDS.bibFile, 'refs.bib'))
  858. )
  859. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  860. cy.findByRole('tab', { name: /refs\.bib/ }).should('exist')
  861. })
  862. })
  863. describe('Tab scrolling', function () {
  864. it('scrolls the selected tab into view', function () {
  865. const manyDocs = [
  866. { _id: DOC_IDS.main, name: 'main.tex' },
  867. ...Array.from({ length: 10 }, (_, i) => ({
  868. _id: `ch${i + 1}`,
  869. name: `chapter-${i + 1}.tex`,
  870. })),
  871. ]
  872. const rootFolder = makeRootFolder(manyDocs)
  873. mountTabs({ rootFolder })
  874. // Open main
  875. cy.then(() => selectDoc(DOC_IDS.main))
  876. // Open all chapter tabs first so they push main.tex out of view
  877. for (let i = 1; i <= 10; i++) {
  878. const id = `ch${i}`
  879. cy.then(() => selectEntity(makeDocEntity(id, `chapter-${i}.tex`)))
  880. cy.get('@editorView').type('a')
  881. cy.findByRole('tab', { name: new RegExp(`chapter-${i}.tex`) }).should(
  882. 'be.visible'
  883. )
  884. }
  885. // Now select main again — it should be scrolled back into view
  886. cy.then(() => selectDoc(DOC_IDS.main))
  887. cy.findByRole('tab', { name: /main\.tex/ }).should('be.visible')
  888. })
  889. it('scrolls horizontally on vertical mouse wheel', function () {
  890. const manyDocs = [
  891. { _id: DOC_IDS.main, name: 'main.tex' },
  892. ...Array.from({ length: 10 }, (_, i) => ({
  893. _id: `ch${i + 1}`,
  894. name: `chapter-${i + 1}.tex`,
  895. })),
  896. ]
  897. const rootFolder = makeRootFolder(manyDocs)
  898. mountTabs({ rootFolder })
  899. // Open enough tabs to cause overflow
  900. cy.then(() => selectDoc(DOC_IDS.main))
  901. for (let i = 1; i <= 10; i++) {
  902. const id = `ch${i}`
  903. cy.then(() => selectEntity(makeDocEntity(id, `chapter-${i}.tex`)))
  904. }
  905. // Scroll back to the start
  906. cy.findByRole('tablist').then($el => {
  907. $el[0].scrollLeft = 0
  908. })
  909. // Trigger a vertical wheel event on the tablist
  910. cy.findByRole('tablist').trigger('wheel', { deltaY: 100, deltaX: 0 })
  911. // scrollLeft should have increased
  912. cy.findByRole('tablist').should($el => {
  913. expect($el[0].scrollLeft).to.be.greaterThan(0)
  914. })
  915. })
  916. })
  917. describe('Pruning deleted files', function () {
  918. it('prunes persisted tabs whose files are no longer in the tree', function () {
  919. cy.then(() => selectDoc(DOC_IDS.main))
  920. cy.then(() => selectDoc(DOC_IDS.intro))
  921. cy.then(() => selectDoc(DOC_IDS.appendix))
  922. cy.findAllByRole('tab').should('have.length', 3)
  923. // Re-mount with a tree containing only appendix.tex, then verify
  924. // the last remaining tab cannot be closed
  925. const trimmedRootFolder = makeRootFolder([
  926. { _id: DOC_IDS.appendix, name: 'appendix.tex' },
  927. ])
  928. mountTabs({ rootFolder: trimmedRootFolder })
  929. cy.findAllByRole('tab').should('have.length', 1)
  930. cy.findByRole('tab', { name: /appendix\.tex/ }).should('exist')
  931. cy.findByRole('tab', { name: /appendix\.tex/ }).within(() => {
  932. cy.findByRole('button', { name: 'Close' }).should('be.disabled')
  933. })
  934. })
  935. })
  936. describe('Review panel header', function () {
  937. function toggleReviewPanel() {
  938. cy.window().then(win => {
  939. win.dispatchEvent(new win.CustomEvent('ui.toggle-review-panel'))
  940. })
  941. }
  942. it('does not render the review panel header when the review panel is closed', function () {
  943. cy.then(() => selectDoc(DOC_IDS.main))
  944. cy.findByRole('heading', { name: 'Review' }).should('not.exist')
  945. })
  946. it('renders the review panel header inside the tabs container when the review panel is open', function () {
  947. cy.then(() => selectDoc(DOC_IDS.main))
  948. toggleReviewPanel()
  949. cy.get('.editor-tabs-container').within(() => {
  950. cy.findByRole('heading', { name: 'Review' }).should('exist')
  951. })
  952. })
  953. it('removes the review panel header when the review panel is closed again', function () {
  954. cy.then(() => selectDoc(DOC_IDS.main))
  955. toggleReviewPanel()
  956. cy.findByRole('heading', { name: 'Review' }).should('exist')
  957. toggleReviewPanel()
  958. cy.findByRole('heading', { name: 'Review' }).should('not.exist')
  959. })
  960. })
  961. describe('SplitTestBadge', function () {
  962. it('renders the labs badge icon in the tabs container', function () {
  963. cy.window().then(win => {
  964. win.metaAttributesCache.set('ol-splitTestInfo', {
  965. 'editor-tabs': {
  966. phase: 'beta',
  967. badgeInfo: {
  968. url: '/beta/editor-tabs',
  969. tooltipText: 'Editor tabs are in beta',
  970. },
  971. },
  972. })
  973. })
  974. mountTabs()
  975. cy.then(() => selectDoc(DOC_IDS.main))
  976. cy.get('.editor-tabs-labs-icon').should('exist')
  977. })
  978. })
  979. })