tabs.spec.tsx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. import React, { FC, useEffect, useRef } 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 { useFileTreeOpenContext } from '@/features/ide-react/context/file-tree-open-context'
  14. const DOC_IDS = {
  15. main: 'doc-main-id',
  16. intro: 'doc-intro-id',
  17. appendix: 'doc-appendix-id',
  18. bibFile: 'file-bib-id',
  19. introA: 'doc-main-a',
  20. introB: 'doc-main-b',
  21. }
  22. const FOLDER_IDS = {
  23. chapterA: 'folder-chapter-a',
  24. chapterB: 'folder-chapter-b',
  25. }
  26. const DOC_NAMES: Record<string, string> = {
  27. [DOC_IDS.main]: 'main.tex',
  28. [DOC_IDS.intro]: 'intro.tex',
  29. [DOC_IDS.appendix]: 'appendix.tex',
  30. [DOC_IDS.bibFile]: 'refs.bib',
  31. [DOC_IDS.introA]: 'intro.tex',
  32. [DOC_IDS.introB]: 'intro.tex',
  33. }
  34. function makeRootFolder(
  35. docs: { _id: string; name: string }[] = [],
  36. folders: any[] = [],
  37. fileRefs: { _id: string; name: string }[] = []
  38. ) {
  39. return [
  40. {
  41. _id: 'root-folder-id',
  42. name: 'rootFolder',
  43. docs,
  44. folders,
  45. fileRefs,
  46. },
  47. ]
  48. }
  49. const defaultDocs = [
  50. { _id: DOC_IDS.main, name: 'main.tex' },
  51. { _id: DOC_IDS.intro, name: 'intro.tex' },
  52. { _id: DOC_IDS.appendix, name: 'appendix.tex' },
  53. ]
  54. const defaultRootFolder = makeRootFolder(defaultDocs)
  55. function makeDocEntity(
  56. id: string,
  57. name: string,
  58. path: string[] = ['root-folder-id']
  59. ): FileTreeDocumentFindResult {
  60. return {
  61. type: 'doc',
  62. entity: { _id: id, name },
  63. parent: [],
  64. parentFolderId: path[path.length - 1],
  65. path,
  66. index: 0,
  67. }
  68. }
  69. function makeFileRefEntity(
  70. id: string,
  71. name: string
  72. ): FileTreeFileRefFindResult {
  73. return {
  74. type: 'fileRef',
  75. entity: {
  76. _id: id,
  77. name,
  78. linkedFileData: undefined,
  79. created: new Date().toISOString(),
  80. hash: 'abc123',
  81. },
  82. parent: [],
  83. parentFolderId: 'root-folder-id',
  84. path: ['root-folder-id'],
  85. index: 0,
  86. }
  87. }
  88. function makeEditorManagerProvider() {
  89. const EditorManagerProvider: FC<React.PropsWithChildren> = ({ children }) => {
  90. const value = {
  91. getEditorType: () => null,
  92. getCurrentDocValue: () => null,
  93. getCurrentDocumentId: () => null,
  94. setIgnoringExternalUpdates: () => {},
  95. openDocWithId: cy.stub().as('openDocWithId').resolves(),
  96. openDoc: cy.stub().as('openDoc').resolves(),
  97. openDocs: { awaitBufferedOps: cy.stub().resolves() } as any,
  98. openFileWithId: cy.stub().as('openFileWithId'),
  99. openInitialDoc: cy.stub().resolves(),
  100. isLoading: false,
  101. jumpToLine: () => {},
  102. debugTimers: { current: {} },
  103. } as unknown as EditorManager
  104. return (
  105. <EditorManagerContext.Provider value={value}>
  106. {children}
  107. </EditorManagerContext.Provider>
  108. )
  109. }
  110. return EditorManagerProvider
  111. }
  112. // Rendered inside the provider tree to call handleFileTreeSelect() when a
  113. // custom DOM event fires. Also triggers handleFileTreeInit() on mount.
  114. function FileSelectionDriver({
  115. autoSelectEntity,
  116. }: {
  117. autoSelectEntity?: FileTreeDocumentFindResult | FileTreeFileRefFindResult
  118. } = {}) {
  119. const { handleFileTreeSelect, handleFileTreeInit } = useFileTreeOpenContext()
  120. const initDone = useRef(false)
  121. useEffect(() => {
  122. if (!initDone.current) {
  123. initDone.current = true
  124. handleFileTreeInit()
  125. if (autoSelectEntity) {
  126. handleFileTreeSelect([autoSelectEntity])
  127. }
  128. }
  129. }, [handleFileTreeInit, handleFileTreeSelect, autoSelectEntity])
  130. useEffect(() => {
  131. const handler = (e: Event) => {
  132. const { detail } = e as CustomEvent
  133. handleFileTreeSelect([detail])
  134. }
  135. document.addEventListener('test:selectEntity', handler)
  136. return () => {
  137. document.removeEventListener('test:selectEntity', handler)
  138. }
  139. }, [handleFileTreeSelect])
  140. return null
  141. }
  142. // Dispatch a custom event to select an entity (simulates file-tree click).
  143. // Must be wrapped in cy.then() so it runs in the Cypress command queue.
  144. function selectEntity(
  145. entity: FileTreeDocumentFindResult | FileTreeFileRefFindResult
  146. ) {
  147. document.dispatchEvent(
  148. new CustomEvent('test:selectEntity', { detail: entity })
  149. )
  150. cy.findByRole('tab', { name: new RegExp(entity.entity.name) }).should('exist')
  151. }
  152. function selectDoc(id: string, path?: string[]) {
  153. selectEntity(makeDocEntity(id, DOC_NAMES[id], path))
  154. }
  155. function enableEditorTabs() {
  156. cy.window().then(win => {
  157. win.metaAttributesCache.set('ol-splitTestVariants', {
  158. 'editor-tabs': 'enabled',
  159. })
  160. win.metaAttributesCache.set('ol-labsExperiments', ['editor-tabs'])
  161. })
  162. }
  163. describe('File Tabs', function () {
  164. function mountTabs(options?: { rootFolder?: any; userSettings?: any }) {
  165. const rootFolder = options?.rootFolder ?? defaultRootFolder
  166. cy.mount(
  167. <EditorProviders
  168. rootFolder={rootFolder as any}
  169. rootDocId={DOC_IDS.main}
  170. userSettings={options?.userSettings}
  171. providers={{
  172. EditorManagerProvider: makeEditorManagerProvider(),
  173. }}
  174. >
  175. <FileSelectionDriver />
  176. <TabsContainer />
  177. </EditorProviders>
  178. )
  179. }
  180. beforeEach(function () {
  181. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  182. cy.interceptEvents()
  183. cy.interceptTutorials()
  184. cy.interceptCompile()
  185. enableEditorTabs()
  186. // Clear persisted tab state from localStorage
  187. cy.window().then(win => {
  188. Object.keys(win.localStorage).forEach(key => {
  189. if (key.startsWith('open-tabs:')) {
  190. win.localStorage.removeItem(key)
  191. }
  192. })
  193. })
  194. cy.window().then(win => {
  195. win.metaAttributesCache.set('ol-user', { id: 'user1' })
  196. })
  197. mountTabs()
  198. })
  199. describe('Initial file selection', function () {
  200. it('automatically creates a tab for the initially opened file', function () {
  201. // Re-mount with auto-selection of the root doc to simulate
  202. // the file tree opening the initial document on startup
  203. cy.mount(
  204. <EditorProviders
  205. rootFolder={defaultRootFolder as any}
  206. rootDocId={DOC_IDS.main}
  207. providers={{
  208. EditorManagerProvider: makeEditorManagerProvider(),
  209. }}
  210. >
  211. <FileSelectionDriver
  212. autoSelectEntity={makeDocEntity(
  213. DOC_IDS.intro,
  214. DOC_NAMES[DOC_IDS.intro]
  215. )}
  216. />
  217. <TabsContainer />
  218. </EditorProviders>
  219. )
  220. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  221. })
  222. })
  223. describe('Tab display', function () {
  224. it('displays a tab when a file is selected', function () {
  225. cy.then(() => selectDoc(DOC_IDS.main))
  226. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  227. })
  228. it('marks the selected tab as active', function () {
  229. cy.then(() => selectDoc(DOC_IDS.main))
  230. cy.findByRole('tab', { name: /main\.tex/ })
  231. .should('have.attr', 'aria-selected', 'true')
  232. .and('have.class', 'tab-selected')
  233. })
  234. })
  235. describe('Opening tabs on file change', function () {
  236. it('opens a new tab when a different file is selected', function () {
  237. cy.then(() => selectDoc(DOC_IDS.main))
  238. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  239. // Make main permanent (keypress) so selecting another file doesn't replace it
  240. cy.get('body').type('a')
  241. // Select another file
  242. cy.then(() => selectDoc(DOC_IDS.intro))
  243. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  244. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  245. })
  246. it('does not duplicate a tab when the same file is selected again', function () {
  247. cy.then(() => selectDoc(DOC_IDS.main))
  248. cy.findAllByRole('tab').should('have.length', 1)
  249. // Select the same file again
  250. cy.then(() => selectDoc(DOC_IDS.main))
  251. cy.findAllByRole('tab').should('have.length', 1)
  252. })
  253. })
  254. describe('Temporary tabs', function () {
  255. beforeEach(function () {
  256. mountTabs({ userSettings: { previewTabs: true } })
  257. })
  258. it('opens a newly selected file as a temporary tab', function () {
  259. cy.then(() => selectDoc(DOC_IDS.main))
  260. // The first tab is temporary until a keypress
  261. cy.findByRole('tab', { name: /main\.tex/ }).should(
  262. 'have.class',
  263. 'tab-temporary'
  264. )
  265. })
  266. it('makes a temporary tab permanent when a key is pressed', function () {
  267. cy.then(() => selectDoc(DOC_IDS.main))
  268. cy.findByRole('tab', { name: /main\.tex/ }).should(
  269. 'have.class',
  270. 'tab-temporary'
  271. )
  272. cy.get('body').type('a')
  273. cy.findByRole('tab', { name: /main\.tex/ }).should(
  274. 'not.have.class',
  275. 'tab-temporary'
  276. )
  277. })
  278. it('replaces a temporary tab when selecting yet another file', function () {
  279. // Open main (temporary)
  280. cy.then(() => selectDoc(DOC_IDS.main))
  281. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  282. // Make main permanent
  283. cy.get('body').type('a')
  284. // Open intro (temporary)
  285. cy.then(() => selectDoc(DOC_IDS.intro))
  286. cy.findByRole('tab', { name: /intro\.tex/ }).should(
  287. 'have.class',
  288. 'tab-temporary'
  289. )
  290. // Open appendix — should replace temporary intro
  291. cy.then(() => selectDoc(DOC_IDS.appendix))
  292. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  293. cy.findByRole('tab', { name: /appendix\.tex/ }).should('exist')
  294. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  295. })
  296. it('makes a temporary tab permanent on double-click', function () {
  297. cy.then(() => selectDoc(DOC_IDS.main))
  298. cy.findByRole('tab', { name: /main\.tex/ }).should(
  299. 'have.class',
  300. 'tab-temporary'
  301. )
  302. cy.findByRole('tab', { name: /main\.tex/ }).dblclick()
  303. cy.findByRole('tab', { name: /main\.tex/ }).should(
  304. 'not.have.class',
  305. 'tab-temporary'
  306. )
  307. })
  308. it('opens a new tab as permanent when previewTabs is disabled', function () {
  309. mountTabs({ userSettings: { previewTabs: false } })
  310. cy.then(() => selectDoc(DOC_IDS.main))
  311. cy.findByRole('tab', { name: /main\.tex/ }).should(
  312. 'not.have.class',
  313. 'tab-temporary'
  314. )
  315. })
  316. })
  317. describe('Closing tabs', function () {
  318. it('closes a tab via the close button', function () {
  319. // Open main
  320. cy.then(() => selectDoc(DOC_IDS.main))
  321. // Open intro
  322. cy.then(() => selectDoc(DOC_IDS.intro))
  323. cy.findAllByRole('tab').should('have.length', 2)
  324. // Close intro
  325. cy.findByRole('tab', { name: /intro\.tex/ }).within(() => {
  326. cy.findByRole('button', { name: 'Close tab' }).click()
  327. })
  328. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  329. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  330. })
  331. it('cannot close the last remaining tab', function () {
  332. cy.then(() => selectDoc(DOC_IDS.main))
  333. cy.findAllByRole('tab').should('have.length', 1)
  334. // Attempt to close the only tab
  335. cy.findByRole('tab', { name: /main\.tex/ }).within(() => {
  336. cy.findByRole('button', { name: 'Close tab' }).click()
  337. })
  338. // Tab must still exist
  339. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  340. cy.findAllByRole('tab').should('have.length', 1)
  341. })
  342. it('switches to an adjacent tab when closing the currently active tab', function () {
  343. // Open three tabs
  344. cy.then(() => selectDoc(DOC_IDS.main))
  345. cy.then(() => selectDoc(DOC_IDS.intro))
  346. cy.then(() => selectDoc(DOC_IDS.appendix))
  347. cy.findAllByRole('tab').should('have.length', 3)
  348. // Close the currently selected tab (appendix — the last one selected)
  349. cy.findByRole('tab', { name: /appendix\.tex/ }).within(() => {
  350. cy.findByRole('button', { name: 'Close tab' }).click()
  351. })
  352. cy.findByRole('tab', { name: /appendix\.tex/ }).should('not.exist')
  353. // The context should have tried to open an adjacent tab
  354. cy.get('@openDocWithId').should('have.been.calledWith', DOC_IDS.intro)
  355. })
  356. it('closes a tab on middle-click', function () {
  357. // Open two tabs
  358. cy.then(() => selectDoc(DOC_IDS.main))
  359. cy.then(() => selectDoc(DOC_IDS.intro))
  360. cy.findAllByRole('tab').should('have.length', 2)
  361. // Middle-click intro tab
  362. cy.findByRole('tab', { name: /intro\.tex/ }).trigger('mouseup', {
  363. button: 1,
  364. })
  365. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  366. })
  367. })
  368. describe('Tab interaction', function () {
  369. it('calls openDocWithId when clicking a non-selected doc tab', function () {
  370. // Open two tabs
  371. cy.then(() => selectDoc(DOC_IDS.main))
  372. cy.then(() => selectDoc(DOC_IDS.intro))
  373. // Click main tab
  374. cy.findByRole('tab', { name: /main\.tex/ }).click()
  375. cy.get('@openDocWithId').should('have.been.calledWith', DOC_IDS.main)
  376. })
  377. it('calls openFileWithId when clicking a non-selected fileRef tab', function () {
  378. const rootFolder = makeRootFolder(
  379. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  380. [],
  381. [{ _id: DOC_IDS.bibFile, name: 'refs.bib' }]
  382. )
  383. mountTabs({ rootFolder })
  384. // Open main doc
  385. cy.then(() => selectDoc(DOC_IDS.main))
  386. // Open fileRef
  387. cy.then(() =>
  388. selectEntity(makeFileRefEntity(DOC_IDS.bibFile, 'refs.bib'))
  389. )
  390. // Switch back to main so refs.bib is no longer selected
  391. cy.findByRole('tab', { name: /main\.tex/ }).click()
  392. // Click the fileRef tab
  393. cy.findByRole('tab', { name: /refs\.bib/ }).click()
  394. cy.get('@openFileWithId').should('have.been.calledWith', DOC_IDS.bibFile)
  395. })
  396. })
  397. describe('Drag and drop', function () {
  398. it('reorders tabs by dragging to the right of another tab', function () {
  399. // Open three tabs
  400. cy.then(() => selectDoc(DOC_IDS.main))
  401. cy.then(() => selectDoc(DOC_IDS.intro))
  402. cy.then(() => selectDoc(DOC_IDS.appendix))
  403. cy.findAllByRole('tab').should('have.length', 3)
  404. // Verify initial order
  405. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  406. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  407. cy.findAllByRole('tab').eq(2).should('contain.text', 'appendix.tex')
  408. // Drag main to the right of appendix
  409. const dataTransfer = new DataTransfer()
  410. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  411. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  412. dataTransfer,
  413. })
  414. cy.findByRole('tab', { name: /appendix\.tex/ }).then($el => {
  415. const rect = $el[0].getBoundingClientRect()
  416. cy.wrap($el).trigger('dragover', {
  417. dataTransfer,
  418. clientX: rect.left + rect.width * 0.75,
  419. })
  420. cy.wrap($el).trigger('drop', {
  421. dataTransfer,
  422. clientX: rect.left + rect.width * 0.75,
  423. })
  424. })
  425. // Expected order after move: intro, appendix, main
  426. cy.findAllByRole('tab').eq(0).should('contain.text', 'intro.tex')
  427. cy.findAllByRole('tab').eq(1).should('contain.text', 'appendix.tex')
  428. cy.findAllByRole('tab').eq(2).should('contain.text', 'main.tex')
  429. })
  430. it('reorders tabs by dragging to the left of another tab', function () {
  431. // Open three tabs
  432. cy.then(() => selectDoc(DOC_IDS.main))
  433. cy.then(() => selectDoc(DOC_IDS.intro))
  434. cy.then(() => selectDoc(DOC_IDS.appendix))
  435. // Verify initial order
  436. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  437. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  438. cy.findAllByRole('tab').eq(2).should('contain.text', 'appendix.tex')
  439. // Drag appendix to the left of main
  440. const dataTransfer = new DataTransfer()
  441. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.appendix)
  442. cy.findByRole('tab', { name: /appendix\.tex/ }).trigger('dragstart', {
  443. dataTransfer,
  444. })
  445. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  446. const rect = $el[0].getBoundingClientRect()
  447. cy.wrap($el).trigger('dragover', {
  448. dataTransfer,
  449. clientX: rect.left + rect.width * 0.25,
  450. })
  451. cy.wrap($el).trigger('drop', {
  452. dataTransfer,
  453. clientX: rect.left + rect.width * 0.25,
  454. })
  455. })
  456. // Expected order: appendix, main, intro
  457. cy.findAllByRole('tab').eq(0).should('contain.text', 'appendix.tex')
  458. cy.findAllByRole('tab').eq(1).should('contain.text', 'main.tex')
  459. cy.findAllByRole('tab').eq(2).should('contain.text', 'intro.tex')
  460. })
  461. it('shows a drop indicator when dragging over a tab', function () {
  462. // Open two tabs
  463. cy.then(() => selectDoc(DOC_IDS.main))
  464. cy.then(() => selectDoc(DOC_IDS.intro))
  465. const dataTransfer = new DataTransfer()
  466. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.intro)
  467. cy.findByRole('tab', { name: /intro\.tex/ }).trigger('dragstart', {
  468. dataTransfer,
  469. })
  470. // Drag over left side of main tab
  471. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  472. const rect = $el[0].getBoundingClientRect()
  473. cy.wrap($el).trigger('dragover', {
  474. dataTransfer,
  475. clientX: rect.left + rect.width * 0.25,
  476. })
  477. })
  478. cy.findByRole('tab', { name: /main\.tex/ }).should(
  479. 'have.class',
  480. 'tab-drop-left'
  481. )
  482. // Drag leave should clear the indicator
  483. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragleave')
  484. cy.findByRole('tab', { name: /main\.tex/ }).should(
  485. 'not.have.class',
  486. 'tab-drop-left'
  487. )
  488. })
  489. it('drops onto the tablist to move a tab to the end', function () {
  490. // Open three tabs
  491. cy.then(() => selectDoc(DOC_IDS.main))
  492. cy.then(() => selectDoc(DOC_IDS.intro))
  493. cy.then(() => selectDoc(DOC_IDS.appendix))
  494. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  495. // Drag main to the tablist gutter (right of last tab)
  496. const dataTransfer = new DataTransfer()
  497. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  498. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  499. dataTransfer,
  500. })
  501. cy.findByRole('tablist').trigger('dragover', { dataTransfer })
  502. cy.findByRole('tablist').trigger('drop', { dataTransfer })
  503. // main should now be last
  504. cy.findAllByRole('tab').last().should('contain.text', 'main.tex')
  505. })
  506. it('does nothing when dropping a tab on itself', function () {
  507. // Open two tabs
  508. cy.then(() => selectDoc(DOC_IDS.main))
  509. cy.then(() => selectDoc(DOC_IDS.intro))
  510. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  511. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  512. // Drag main onto itself
  513. const dataTransfer = new DataTransfer()
  514. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  515. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  516. dataTransfer,
  517. })
  518. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  519. const rect = $el[0].getBoundingClientRect()
  520. cy.wrap($el).trigger('dragover', {
  521. dataTransfer,
  522. clientX: rect.left + rect.width * 0.75,
  523. })
  524. cy.wrap($el).trigger('drop', {
  525. dataTransfer,
  526. clientX: rect.left + rect.width * 0.75,
  527. })
  528. })
  529. // Order unchanged
  530. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  531. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  532. })
  533. })
  534. describe('Path disambiguation for duplicate names', function () {
  535. const duplicateNameRootFolder = makeRootFolder(
  536. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  537. [
  538. {
  539. _id: FOLDER_IDS.chapterA,
  540. name: 'chapter-a',
  541. docs: [{ _id: DOC_IDS.introA, name: 'intro.tex' }],
  542. folders: [],
  543. fileRefs: [],
  544. },
  545. {
  546. _id: FOLDER_IDS.chapterB,
  547. name: 'chapter-b',
  548. docs: [{ _id: DOC_IDS.introB, name: 'intro.tex' }],
  549. folders: [],
  550. fileRefs: [],
  551. },
  552. ]
  553. )
  554. it('shows disambiguated paths when two files share the same name', function () {
  555. mountTabs({ rootFolder: duplicateNameRootFolder })
  556. // Open main.tex (unique name, no disambiguation needed)
  557. cy.then(() => selectDoc(DOC_IDS.main))
  558. // Open intro.tex from chapter-a
  559. cy.then(() =>
  560. selectDoc(DOC_IDS.introA, ['root-folder-id', FOLDER_IDS.chapterA])
  561. )
  562. // Open intro.tex from chapter-b
  563. cy.then(() =>
  564. selectDoc(DOC_IDS.introB, ['root-folder-id', FOLDER_IDS.chapterB])
  565. )
  566. cy.findAllByRole('tab').should('have.length', 3)
  567. // main.tex is unique — should display without a path prefix
  568. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  569. // The two intro.tex tabs should be disambiguated with their folder names
  570. cy.findAllByRole('tab')
  571. .eq(1)
  572. .should('contain.text', 'chapter-a/intro.tex')
  573. cy.findAllByRole('tab')
  574. .eq(2)
  575. .should('contain.text', 'chapter-b/intro.tex')
  576. })
  577. it('uses only the file name when there is no collision', function () {
  578. mountTabs({ rootFolder: duplicateNameRootFolder })
  579. // Open just one of the duplicate-named files
  580. cy.then(() =>
  581. selectDoc(DOC_IDS.introA, ['root-folder-id', FOLDER_IDS.chapterA])
  582. )
  583. // With only one intro.tex open, no disambiguation is needed
  584. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  585. cy.findByRole('tab', { name: /intro\.tex/ }).should(
  586. 'not.contain.text',
  587. 'chapter-a/'
  588. )
  589. })
  590. })
  591. describe('Multiple file types', function () {
  592. it('displays tabs for both docs and file refs', function () {
  593. const rootFolder = makeRootFolder(
  594. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  595. [],
  596. [{ _id: DOC_IDS.bibFile, name: 'refs.bib' }]
  597. )
  598. mountTabs({ rootFolder })
  599. // Open main doc
  600. cy.then(() => selectDoc(DOC_IDS.main))
  601. // Open a fileRef
  602. cy.then(() =>
  603. selectEntity(makeFileRefEntity(DOC_IDS.bibFile, 'refs.bib'))
  604. )
  605. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  606. cy.findByRole('tab', { name: /refs\.bib/ }).should('exist')
  607. })
  608. })
  609. describe('Tab scrolling', function () {
  610. it('scrolls the selected tab into view', function () {
  611. const manyDocs = [
  612. { _id: DOC_IDS.main, name: 'main.tex' },
  613. ...Array.from({ length: 10 }, (_, i) => ({
  614. _id: `ch${i + 1}`,
  615. name: `chapter-${i + 1}.tex`,
  616. })),
  617. ]
  618. const rootFolder = makeRootFolder(manyDocs)
  619. mountTabs({ rootFolder })
  620. // Open main
  621. cy.then(() => selectDoc(DOC_IDS.main))
  622. // Open all chapter tabs first so they push main.tex out of view
  623. for (let i = 1; i <= 10; i++) {
  624. const id = `ch${i}`
  625. cy.then(() => selectEntity(makeDocEntity(id, `chapter-${i}.tex`)))
  626. cy.get('body').type('a')
  627. cy.findByRole('tab', { name: new RegExp(`chapter-${i}.tex`) }).should(
  628. 'be.visible'
  629. )
  630. }
  631. // Now select main again — it should be scrolled back into view
  632. cy.then(() => selectDoc(DOC_IDS.main))
  633. cy.findByRole('tab', { name: /main\.tex/ }).should('be.visible')
  634. })
  635. it('scrolls horizontally on vertical mouse wheel', function () {
  636. const manyDocs = [
  637. { _id: DOC_IDS.main, name: 'main.tex' },
  638. ...Array.from({ length: 10 }, (_, i) => ({
  639. _id: `ch${i + 1}`,
  640. name: `chapter-${i + 1}.tex`,
  641. })),
  642. ]
  643. const rootFolder = makeRootFolder(manyDocs)
  644. mountTabs({ rootFolder })
  645. // Open enough tabs to cause overflow
  646. cy.then(() => selectDoc(DOC_IDS.main))
  647. for (let i = 1; i <= 10; i++) {
  648. const id = `ch${i}`
  649. cy.then(() => selectEntity(makeDocEntity(id, `chapter-${i}.tex`)))
  650. }
  651. // Scroll back to the start
  652. cy.findByRole('tablist').then($el => {
  653. $el[0].scrollLeft = 0
  654. })
  655. // Trigger a vertical wheel event on the tablist
  656. cy.findByRole('tablist').trigger('wheel', { deltaY: 100, deltaX: 0 })
  657. // scrollLeft should have increased
  658. cy.findByRole('tablist').should($el => {
  659. expect($el[0].scrollLeft).to.be.greaterThan(0)
  660. })
  661. })
  662. })
  663. describe('SplitTestBadge', function () {
  664. it('renders the labs badge icon in the tabs container', function () {
  665. cy.window().then(win => {
  666. win.metaAttributesCache.set('ol-splitTestInfo', {
  667. 'editor-tabs': {
  668. phase: 'beta',
  669. badgeInfo: {
  670. url: '/beta/editor-tabs',
  671. tooltipText: 'Editor tabs are in beta',
  672. },
  673. },
  674. })
  675. })
  676. mountTabs()
  677. cy.then(() => selectDoc(DOC_IDS.main))
  678. cy.get('.editor-tabs-labs-icon').should('exist')
  679. })
  680. })
  681. })