tabs.spec.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. }
  151. function selectDoc(id: string, path?: string[]) {
  152. selectEntity(makeDocEntity(id, DOC_NAMES[id], path))
  153. }
  154. function enableEditorTabs() {
  155. cy.window().then(win => {
  156. win.metaAttributesCache.set('ol-splitTestVariants', {
  157. 'editor-tabs': 'enabled',
  158. })
  159. win.metaAttributesCache.set('ol-labsExperiments', ['editor-tabs'])
  160. })
  161. }
  162. describe('File Tabs', function () {
  163. function mountTabs(options?: { rootFolder?: any }) {
  164. const rootFolder = options?.rootFolder ?? defaultRootFolder
  165. cy.mount(
  166. <EditorProviders
  167. rootFolder={rootFolder as any}
  168. rootDocId={DOC_IDS.main}
  169. providers={{
  170. EditorManagerProvider: makeEditorManagerProvider(),
  171. }}
  172. >
  173. <FileSelectionDriver />
  174. <TabsContainer />
  175. </EditorProviders>
  176. )
  177. }
  178. beforeEach(function () {
  179. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  180. cy.interceptEvents()
  181. cy.interceptTutorials()
  182. cy.interceptCompile()
  183. enableEditorTabs()
  184. // Clear persisted tab state from localStorage
  185. cy.window().then(win => {
  186. Object.keys(win.localStorage).forEach(key => {
  187. if (key.startsWith('open-tabs:')) {
  188. win.localStorage.removeItem(key)
  189. }
  190. })
  191. })
  192. cy.window().then(win => {
  193. win.metaAttributesCache.set('ol-user', { id: 'user1' })
  194. })
  195. mountTabs()
  196. })
  197. describe('Initial file selection', function () {
  198. it('automatically creates a tab for the initially opened file', function () {
  199. // Re-mount with auto-selection of the root doc to simulate
  200. // the file tree opening the initial document on startup
  201. cy.mount(
  202. <EditorProviders
  203. rootFolder={defaultRootFolder as any}
  204. rootDocId={DOC_IDS.main}
  205. providers={{
  206. EditorManagerProvider: makeEditorManagerProvider(),
  207. }}
  208. >
  209. <FileSelectionDriver
  210. autoSelectEntity={makeDocEntity(
  211. DOC_IDS.intro,
  212. DOC_NAMES[DOC_IDS.intro]
  213. )}
  214. />
  215. <TabsContainer />
  216. </EditorProviders>
  217. )
  218. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  219. })
  220. })
  221. describe('Tab display', function () {
  222. it('displays a tab when a file is selected', function () {
  223. cy.then(() => selectDoc(DOC_IDS.main))
  224. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  225. })
  226. it('marks the selected tab as active', function () {
  227. cy.then(() => selectDoc(DOC_IDS.main))
  228. cy.findByRole('tab', { name: /main\.tex/ })
  229. .should('have.attr', 'aria-selected', 'true')
  230. .and('have.class', 'tab-selected')
  231. })
  232. })
  233. describe('Opening tabs on file change', function () {
  234. it('opens a new tab when a different file is selected', function () {
  235. cy.then(() => selectDoc(DOC_IDS.main))
  236. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  237. // Make main permanent (keypress) so selecting another file doesn't replace it
  238. cy.get('body').type('a')
  239. // Select another file
  240. cy.then(() => selectDoc(DOC_IDS.intro))
  241. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  242. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  243. })
  244. it('does not duplicate a tab when the same file is selected again', function () {
  245. cy.then(() => selectDoc(DOC_IDS.main))
  246. cy.findAllByRole('tab').should('have.length', 1)
  247. // Select the same file again
  248. cy.then(() => selectDoc(DOC_IDS.main))
  249. cy.findAllByRole('tab').should('have.length', 1)
  250. })
  251. })
  252. describe('Temporary tabs', function () {
  253. it('opens a newly selected file as a temporary tab', function () {
  254. cy.then(() => selectDoc(DOC_IDS.main))
  255. // The first tab is temporary until a keypress
  256. cy.findByRole('tab', { name: /main\.tex/ }).should(
  257. 'have.class',
  258. 'tab-temporary'
  259. )
  260. })
  261. it('makes a temporary tab permanent when a key is pressed', function () {
  262. cy.then(() => selectDoc(DOC_IDS.main))
  263. cy.findByRole('tab', { name: /main\.tex/ }).should(
  264. 'have.class',
  265. 'tab-temporary'
  266. )
  267. cy.get('body').type('a')
  268. cy.findByRole('tab', { name: /main\.tex/ }).should(
  269. 'not.have.class',
  270. 'tab-temporary'
  271. )
  272. })
  273. it('replaces a temporary tab when selecting yet another file', function () {
  274. // Open main (temporary)
  275. cy.then(() => selectDoc(DOC_IDS.main))
  276. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  277. // Make main permanent
  278. cy.get('body').type('a')
  279. // Open intro (temporary)
  280. cy.then(() => selectDoc(DOC_IDS.intro))
  281. cy.findByRole('tab', { name: /intro\.tex/ }).should(
  282. 'have.class',
  283. 'tab-temporary'
  284. )
  285. // Open appendix — should replace temporary intro
  286. cy.then(() => selectDoc(DOC_IDS.appendix))
  287. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  288. cy.findByRole('tab', { name: /appendix\.tex/ }).should('exist')
  289. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  290. })
  291. it('makes a temporary tab permanent on double-click', function () {
  292. cy.then(() => selectDoc(DOC_IDS.main))
  293. cy.findByRole('tab', { name: /main\.tex/ }).should(
  294. 'have.class',
  295. 'tab-temporary'
  296. )
  297. cy.findByRole('tab', { name: /main\.tex/ }).dblclick()
  298. cy.findByRole('tab', { name: /main\.tex/ }).should(
  299. 'not.have.class',
  300. 'tab-temporary'
  301. )
  302. })
  303. })
  304. describe('Closing tabs', function () {
  305. it('closes a tab via the close button', function () {
  306. // Open main (permanent)
  307. cy.then(() => selectDoc(DOC_IDS.main))
  308. cy.get('body').type('a')
  309. // Open intro (permanent)
  310. cy.then(() => selectDoc(DOC_IDS.intro))
  311. cy.get('body').type('a')
  312. cy.findAllByRole('tab').should('have.length', 2)
  313. // Close intro
  314. cy.findByRole('tab', { name: /intro\.tex/ }).within(() => {
  315. cy.findByRole('button', { name: 'Close tab' }).click()
  316. })
  317. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  318. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  319. })
  320. it('cannot close the last remaining tab', function () {
  321. cy.then(() => selectDoc(DOC_IDS.main))
  322. cy.findAllByRole('tab').should('have.length', 1)
  323. // Attempt to close the only tab
  324. cy.findByRole('tab', { name: /main\.tex/ }).within(() => {
  325. cy.findByRole('button', { name: 'Close tab' }).click()
  326. })
  327. // Tab must still exist
  328. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  329. cy.findAllByRole('tab').should('have.length', 1)
  330. })
  331. it('switches to an adjacent tab when closing the currently active tab', function () {
  332. // Open three permanent tabs
  333. cy.then(() => selectDoc(DOC_IDS.main))
  334. cy.get('body').type('a')
  335. cy.then(() => selectDoc(DOC_IDS.intro))
  336. cy.get('body').type('a')
  337. cy.then(() => selectDoc(DOC_IDS.appendix))
  338. cy.get('body').type('a')
  339. cy.findAllByRole('tab').should('have.length', 3)
  340. // Close the currently selected tab (appendix — the last one selected)
  341. cy.findByRole('tab', { name: /appendix\.tex/ }).within(() => {
  342. cy.findByRole('button', { name: 'Close tab' }).click()
  343. })
  344. cy.findByRole('tab', { name: /appendix\.tex/ }).should('not.exist')
  345. // The context should have tried to open an adjacent tab
  346. cy.get('@openDocWithId').should('have.been.calledWith', DOC_IDS.intro)
  347. })
  348. it('closes a tab on middle-click', function () {
  349. // Open two permanent tabs
  350. cy.then(() => selectDoc(DOC_IDS.main))
  351. cy.get('body').type('a')
  352. cy.then(() => selectDoc(DOC_IDS.intro))
  353. cy.get('body').type('a')
  354. cy.findAllByRole('tab').should('have.length', 2)
  355. // Middle-click intro tab
  356. cy.findByRole('tab', { name: /intro\.tex/ }).trigger('mouseup', {
  357. button: 1,
  358. })
  359. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  360. })
  361. })
  362. describe('Tab interaction', function () {
  363. it('calls openDocWithId when clicking a non-selected doc tab', function () {
  364. // Open two permanent tabs
  365. cy.then(() => selectDoc(DOC_IDS.main))
  366. cy.get('body').type('a')
  367. cy.then(() => selectDoc(DOC_IDS.intro))
  368. cy.get('body').type('a')
  369. // Click main tab
  370. cy.findByRole('tab', { name: /main\.tex/ }).click()
  371. cy.get('@openDocWithId').should('have.been.calledWith', DOC_IDS.main)
  372. })
  373. it('calls openFileWithId when clicking a non-selected fileRef tab', function () {
  374. const rootFolder = makeRootFolder(
  375. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  376. [],
  377. [{ _id: DOC_IDS.bibFile, name: 'refs.bib' }]
  378. )
  379. mountTabs({ rootFolder })
  380. // Open main doc (permanent)
  381. cy.then(() => selectDoc(DOC_IDS.main))
  382. cy.get('body').type('a')
  383. // Open fileRef (permanent)
  384. cy.then(() =>
  385. selectEntity(makeFileRefEntity(DOC_IDS.bibFile, 'refs.bib'))
  386. )
  387. cy.get('body').type('a')
  388. // Switch back to main so refs.bib is no longer selected
  389. cy.findByRole('tab', { name: /main\.tex/ }).click()
  390. // Click the fileRef tab
  391. cy.findByRole('tab', { name: /refs\.bib/ }).click()
  392. cy.get('@openFileWithId').should('have.been.calledWith', DOC_IDS.bibFile)
  393. })
  394. })
  395. describe('Drag and drop', function () {
  396. it('reorders tabs by dragging to the right of another tab', function () {
  397. // Open three permanent tabs
  398. cy.then(() => selectDoc(DOC_IDS.main))
  399. cy.get('body').type('a')
  400. cy.then(() => selectDoc(DOC_IDS.intro))
  401. cy.get('body').type('a')
  402. cy.then(() => selectDoc(DOC_IDS.appendix))
  403. cy.get('body').type('a')
  404. cy.findAllByRole('tab').should('have.length', 3)
  405. // Verify initial order
  406. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  407. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  408. cy.findAllByRole('tab').eq(2).should('contain.text', 'appendix.tex')
  409. // Drag main to the right of appendix
  410. const dataTransfer = new DataTransfer()
  411. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  412. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  413. dataTransfer,
  414. })
  415. cy.findByRole('tab', { name: /appendix\.tex/ }).then($el => {
  416. const rect = $el[0].getBoundingClientRect()
  417. cy.wrap($el).trigger('dragover', {
  418. dataTransfer,
  419. clientX: rect.left + rect.width * 0.75,
  420. })
  421. cy.wrap($el).trigger('drop', {
  422. dataTransfer,
  423. clientX: rect.left + rect.width * 0.75,
  424. })
  425. })
  426. // Expected order after move: intro, appendix, main
  427. cy.findAllByRole('tab').eq(0).should('contain.text', 'intro.tex')
  428. cy.findAllByRole('tab').eq(1).should('contain.text', 'appendix.tex')
  429. cy.findAllByRole('tab').eq(2).should('contain.text', 'main.tex')
  430. })
  431. it('reorders tabs by dragging to the left of another tab', function () {
  432. // Open three permanent tabs
  433. cy.then(() => selectDoc(DOC_IDS.main))
  434. cy.get('body').type('a')
  435. cy.then(() => selectDoc(DOC_IDS.intro))
  436. cy.get('body').type('a')
  437. cy.then(() => selectDoc(DOC_IDS.appendix))
  438. cy.get('body').type('a')
  439. // Verify initial order
  440. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  441. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  442. cy.findAllByRole('tab').eq(2).should('contain.text', 'appendix.tex')
  443. // Drag appendix to the left of main
  444. const dataTransfer = new DataTransfer()
  445. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.appendix)
  446. cy.findByRole('tab', { name: /appendix\.tex/ }).trigger('dragstart', {
  447. dataTransfer,
  448. })
  449. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  450. const rect = $el[0].getBoundingClientRect()
  451. cy.wrap($el).trigger('dragover', {
  452. dataTransfer,
  453. clientX: rect.left + rect.width * 0.25,
  454. })
  455. cy.wrap($el).trigger('drop', {
  456. dataTransfer,
  457. clientX: rect.left + rect.width * 0.25,
  458. })
  459. })
  460. // Expected order: appendix, main, intro
  461. cy.findAllByRole('tab').eq(0).should('contain.text', 'appendix.tex')
  462. cy.findAllByRole('tab').eq(1).should('contain.text', 'main.tex')
  463. cy.findAllByRole('tab').eq(2).should('contain.text', 'intro.tex')
  464. })
  465. it('shows a drop indicator when dragging over a tab', function () {
  466. // Open two permanent tabs
  467. cy.then(() => selectDoc(DOC_IDS.main))
  468. cy.get('body').type('a')
  469. cy.then(() => selectDoc(DOC_IDS.intro))
  470. cy.get('body').type('a')
  471. const dataTransfer = new DataTransfer()
  472. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.intro)
  473. cy.findByRole('tab', { name: /intro\.tex/ }).trigger('dragstart', {
  474. dataTransfer,
  475. })
  476. // Drag over left side of main tab
  477. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  478. const rect = $el[0].getBoundingClientRect()
  479. cy.wrap($el).trigger('dragover', {
  480. dataTransfer,
  481. clientX: rect.left + rect.width * 0.25,
  482. })
  483. })
  484. cy.findByRole('tab', { name: /main\.tex/ }).should(
  485. 'have.class',
  486. 'tab-drop-left'
  487. )
  488. // Drag leave should clear the indicator
  489. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragleave')
  490. cy.findByRole('tab', { name: /main\.tex/ }).should(
  491. 'not.have.class',
  492. 'tab-drop-left'
  493. )
  494. })
  495. it('drops onto the tablist to move a tab to the end', function () {
  496. // Open three permanent tabs
  497. cy.then(() => selectDoc(DOC_IDS.main))
  498. cy.get('body').type('a')
  499. cy.then(() => selectDoc(DOC_IDS.intro))
  500. cy.get('body').type('a')
  501. cy.then(() => selectDoc(DOC_IDS.appendix))
  502. cy.get('body').type('a')
  503. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  504. // Drag main to the tablist gutter (right of last tab)
  505. const dataTransfer = new DataTransfer()
  506. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  507. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  508. dataTransfer,
  509. })
  510. cy.findByRole('tablist').trigger('dragover', { dataTransfer })
  511. cy.findByRole('tablist').trigger('drop', { dataTransfer })
  512. // main should now be last
  513. cy.findAllByRole('tab').last().should('contain.text', 'main.tex')
  514. })
  515. it('does nothing when dropping a tab on itself', function () {
  516. // Open two permanent tabs
  517. cy.then(() => selectDoc(DOC_IDS.main))
  518. cy.get('body').type('a')
  519. cy.then(() => selectDoc(DOC_IDS.intro))
  520. cy.get('body').type('a')
  521. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  522. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  523. // Drag main onto itself
  524. const dataTransfer = new DataTransfer()
  525. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  526. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  527. dataTransfer,
  528. })
  529. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  530. const rect = $el[0].getBoundingClientRect()
  531. cy.wrap($el).trigger('dragover', {
  532. dataTransfer,
  533. clientX: rect.left + rect.width * 0.75,
  534. })
  535. cy.wrap($el).trigger('drop', {
  536. dataTransfer,
  537. clientX: rect.left + rect.width * 0.75,
  538. })
  539. })
  540. // Order unchanged
  541. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  542. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  543. })
  544. })
  545. describe('Path disambiguation for duplicate names', function () {
  546. const duplicateNameRootFolder = makeRootFolder(
  547. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  548. [
  549. {
  550. _id: FOLDER_IDS.chapterA,
  551. name: 'chapter-a',
  552. docs: [{ _id: DOC_IDS.introA, name: 'intro.tex' }],
  553. folders: [],
  554. fileRefs: [],
  555. },
  556. {
  557. _id: FOLDER_IDS.chapterB,
  558. name: 'chapter-b',
  559. docs: [{ _id: DOC_IDS.introB, name: 'intro.tex' }],
  560. folders: [],
  561. fileRefs: [],
  562. },
  563. ]
  564. )
  565. it('shows disambiguated paths when two files share the same name', function () {
  566. mountTabs({ rootFolder: duplicateNameRootFolder })
  567. // Open main.tex (unique name, no disambiguation needed)
  568. cy.then(() => selectDoc(DOC_IDS.main))
  569. cy.get('body').type('a')
  570. // Open intro.tex from chapter-a
  571. cy.then(() =>
  572. selectDoc(DOC_IDS.introA, ['root-folder-id', FOLDER_IDS.chapterA])
  573. )
  574. cy.get('body').type('a')
  575. // Open intro.tex from chapter-b
  576. cy.then(() =>
  577. selectDoc(DOC_IDS.introB, ['root-folder-id', FOLDER_IDS.chapterB])
  578. )
  579. cy.get('body').type('a')
  580. cy.findAllByRole('tab').should('have.length', 3)
  581. // main.tex is unique — should display without a path prefix
  582. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  583. // The two intro.tex tabs should be disambiguated with their folder names
  584. cy.findAllByRole('tab')
  585. .eq(1)
  586. .should('contain.text', 'chapter-a/intro.tex')
  587. cy.findAllByRole('tab')
  588. .eq(2)
  589. .should('contain.text', 'chapter-b/intro.tex')
  590. })
  591. it('uses only the file name when there is no collision', function () {
  592. mountTabs({ rootFolder: duplicateNameRootFolder })
  593. // Open just one of the duplicate-named files
  594. cy.then(() =>
  595. selectDoc(DOC_IDS.introA, ['root-folder-id', FOLDER_IDS.chapterA])
  596. )
  597. cy.get('body').type('a')
  598. // With only one intro.tex open, no disambiguation is needed
  599. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  600. cy.findByRole('tab', { name: /intro\.tex/ }).should(
  601. 'not.contain.text',
  602. 'chapter-a/'
  603. )
  604. })
  605. })
  606. describe('Multiple file types', function () {
  607. it('displays tabs for both docs and file refs', function () {
  608. const rootFolder = makeRootFolder(
  609. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  610. [],
  611. [{ _id: DOC_IDS.bibFile, name: 'refs.bib' }]
  612. )
  613. mountTabs({ rootFolder })
  614. // Open main doc
  615. cy.then(() => selectDoc(DOC_IDS.main))
  616. cy.get('body').type('a')
  617. // Open a fileRef
  618. cy.then(() =>
  619. selectEntity(makeFileRefEntity(DOC_IDS.bibFile, 'refs.bib'))
  620. )
  621. cy.get('body').type('a')
  622. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  623. cy.findByRole('tab', { name: /refs\.bib/ }).should('exist')
  624. })
  625. })
  626. describe('Tab scroll into view', function () {
  627. it('scrolls the selected tab into view', function () {
  628. const manyDocs = [
  629. { _id: DOC_IDS.main, name: 'main.tex' },
  630. ...Array.from({ length: 10 }, (_, i) => ({
  631. _id: `ch${i + 1}`,
  632. name: `chapter-${i + 1}.tex`,
  633. })),
  634. ]
  635. const rootFolder = makeRootFolder(manyDocs)
  636. mountTabs({ rootFolder })
  637. // Open main and make it permanent
  638. cy.then(() => selectDoc(DOC_IDS.main))
  639. cy.get('body').type('a')
  640. // Open all chapter tabs first so they push main.tex out of view
  641. for (let i = 1; i <= 10; i++) {
  642. const id = `ch${i}`
  643. cy.then(() => selectEntity(makeDocEntity(id, `chapter-${i}.tex`)))
  644. cy.get('body').type('a')
  645. cy.findByRole('tab', { name: new RegExp(`chapter-${i}.tex`) }).should(
  646. 'be.visible'
  647. )
  648. }
  649. // Now select main again — it should be scrolled back into view
  650. cy.then(() => selectDoc(DOC_IDS.main))
  651. cy.findByRole('tab', { name: /main\.tex/ }).should('be.visible')
  652. })
  653. })
  654. describe('SplitTestBadge', function () {
  655. it('renders the labs badge icon in the tabs container', function () {
  656. cy.window().then(win => {
  657. win.metaAttributesCache.set('ol-splitTestInfo', {
  658. 'editor-tabs': {
  659. phase: 'beta',
  660. badgeInfo: {
  661. url: '/beta/editor-tabs',
  662. tooltipText: 'Editor tabs are in beta',
  663. },
  664. },
  665. })
  666. })
  667. mountTabs()
  668. cy.then(() => selectDoc(DOC_IDS.main))
  669. cy.get('.editor-tabs-labs-icon').should('exist')
  670. })
  671. })
  672. })