tabs.spec.tsx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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('Context menu', function () {
  369. it('opens the context menu on right-click of a tab', function () {
  370. cy.then(() => selectDoc(DOC_IDS.main))
  371. cy.then(() => selectDoc(DOC_IDS.intro))
  372. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  373. cy.findByRole('menu').should('exist')
  374. cy.findByRole('menuitem', { name: 'Close tab' }).should('exist')
  375. cy.findByRole('menuitem', { name: 'Close others' }).should('exist')
  376. })
  377. it('closes the clicked tab via "Close tab"', function () {
  378. cy.then(() => selectDoc(DOC_IDS.main))
  379. cy.then(() => selectDoc(DOC_IDS.intro))
  380. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  381. cy.findByRole('menuitem', { name: 'Close tab' }).click()
  382. cy.findByRole('tab', { name: /intro\.tex/ }).should('not.exist')
  383. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  384. cy.findByRole('menu').should('not.exist')
  385. })
  386. it('closes all other tabs via "Close others"', function () {
  387. cy.then(() => selectDoc(DOC_IDS.main))
  388. cy.then(() => selectDoc(DOC_IDS.intro))
  389. cy.then(() => selectDoc(DOC_IDS.appendix))
  390. cy.findAllByRole('tab').should('have.length', 3)
  391. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  392. cy.findByRole('menuitem', { name: 'Close others' }).click()
  393. cy.findAllByRole('tab').should('have.length', 1)
  394. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  395. })
  396. it('navigates to the target tab when closing others from a non-active tab', function () {
  397. cy.then(() => selectDoc(DOC_IDS.main))
  398. cy.then(() => selectDoc(DOC_IDS.intro))
  399. cy.then(() => selectDoc(DOC_IDS.appendix))
  400. // appendix is the currently active tab; close others from intro
  401. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  402. cy.findByRole('menuitem', { name: 'Close others' }).click()
  403. cy.get('@openDocWithId').should('have.been.calledWith', DOC_IDS.intro)
  404. })
  405. it('disables both items when only one tab is open', function () {
  406. cy.then(() => selectDoc(DOC_IDS.main))
  407. cy.findByRole('tab', { name: /main\.tex/ }).rightclick()
  408. cy.findByRole('menuitem', { name: 'Close tab' }).should(
  409. 'have.attr',
  410. 'aria-disabled',
  411. 'true'
  412. )
  413. cy.findByRole('menuitem', { name: 'Close others' }).should(
  414. 'have.attr',
  415. 'aria-disabled',
  416. 'true'
  417. )
  418. })
  419. it('closes the menu on Escape', function () {
  420. cy.then(() => selectDoc(DOC_IDS.main))
  421. cy.then(() => selectDoc(DOC_IDS.intro))
  422. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  423. cy.findByRole('menu').should('exist')
  424. cy.findByRole('menu').trigger('keydown', {
  425. key: 'Escape',
  426. })
  427. cy.findByRole('menu').should('not.exist')
  428. })
  429. it('closes the menu on right-click outside', function () {
  430. cy.then(() => selectDoc(DOC_IDS.main))
  431. cy.then(() => selectDoc(DOC_IDS.intro))
  432. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  433. cy.findByRole('menu').should('exist')
  434. // Right-click outside any tab
  435. cy.get('.editor-tabs-container').rightclick('right')
  436. cy.findByRole('menu').should('not.exist')
  437. })
  438. it('focuses the menu when opening', function () {
  439. cy.then(() => selectDoc(DOC_IDS.main))
  440. cy.then(() => selectDoc(DOC_IDS.intro))
  441. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  442. cy.findByRole('menu').should('be.focused')
  443. })
  444. it('returns focus to the originating tab when closing', function () {
  445. cy.then(() => selectDoc(DOC_IDS.main))
  446. cy.then(() => selectDoc(DOC_IDS.intro))
  447. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  448. cy.findByRole('menu').trigger('keydown', { key: 'Escape' })
  449. cy.findByRole('tab', { name: /intro\.tex/ }).should('be.focused')
  450. })
  451. it('moves the menu to another tab on right-click', function () {
  452. cy.then(() => selectDoc(DOC_IDS.main))
  453. cy.then(() => selectDoc(DOC_IDS.intro))
  454. cy.then(() => selectDoc(DOC_IDS.appendix))
  455. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  456. cy.findByRole('menu').should('exist')
  457. // Right-click appendix the menu should retarget to that tab
  458. cy.findByRole('tab', { name: /appendix\.tex/ }).rightclick({
  459. force: true,
  460. })
  461. cy.findByRole('menuitem', { name: 'Close others' }).click()
  462. cy.findAllByRole('tab').should('have.length', 1)
  463. cy.findByRole('tab', { name: /appendix\.tex/ }).should('exist')
  464. })
  465. it('should not open the context menu if shift is held', function () {
  466. cy.then(() => selectDoc(DOC_IDS.intro))
  467. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick({
  468. shiftKey: true,
  469. })
  470. cy.findByRole('menu').should('not.exist')
  471. })
  472. it('should close already open context menu if shift is held', function () {
  473. cy.then(() => selectDoc(DOC_IDS.intro))
  474. cy.then(() => selectDoc(DOC_IDS.main))
  475. cy.findByRole('tab', { name: /intro\.tex/ }).rightclick()
  476. cy.findByRole('menu').should('exist')
  477. cy.findByRole('tab', { name: /main\.tex/ }).rightclick({
  478. force: true,
  479. shiftKey: true,
  480. })
  481. cy.findByRole('menu').should('not.exist')
  482. })
  483. })
  484. describe('Tab interaction', function () {
  485. it('calls openDocWithId when clicking a non-selected doc tab', function () {
  486. // Open two tabs
  487. cy.then(() => selectDoc(DOC_IDS.main))
  488. cy.then(() => selectDoc(DOC_IDS.intro))
  489. // Click main tab
  490. cy.findByRole('tab', { name: /main\.tex/ }).click()
  491. cy.get('@openDocWithId').should('have.been.calledWith', DOC_IDS.main)
  492. })
  493. it('calls openFileWithId when clicking a non-selected fileRef tab', function () {
  494. const rootFolder = makeRootFolder(
  495. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  496. [],
  497. [{ _id: DOC_IDS.bibFile, name: 'refs.bib' }]
  498. )
  499. mountTabs({ rootFolder })
  500. // Open main doc
  501. cy.then(() => selectDoc(DOC_IDS.main))
  502. // Open fileRef
  503. cy.then(() =>
  504. selectEntity(makeFileRefEntity(DOC_IDS.bibFile, 'refs.bib'))
  505. )
  506. // Switch back to main so refs.bib is no longer selected
  507. cy.findByRole('tab', { name: /main\.tex/ }).click()
  508. // Click the fileRef tab
  509. cy.findByRole('tab', { name: /refs\.bib/ }).click()
  510. cy.get('@openFileWithId').should('have.been.calledWith', DOC_IDS.bibFile)
  511. })
  512. })
  513. describe('Drag and drop', function () {
  514. it('reorders tabs by dragging to the right of another tab', function () {
  515. // Open three tabs
  516. cy.then(() => selectDoc(DOC_IDS.main))
  517. cy.then(() => selectDoc(DOC_IDS.intro))
  518. cy.then(() => selectDoc(DOC_IDS.appendix))
  519. cy.findAllByRole('tab').should('have.length', 3)
  520. // Verify initial order
  521. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  522. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  523. cy.findAllByRole('tab').eq(2).should('contain.text', 'appendix.tex')
  524. // Drag main to the right of appendix
  525. const dataTransfer = new DataTransfer()
  526. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  527. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  528. dataTransfer,
  529. })
  530. cy.findByRole('tab', { name: /appendix\.tex/ }).then($el => {
  531. const rect = $el[0].getBoundingClientRect()
  532. cy.wrap($el).trigger('dragover', {
  533. dataTransfer,
  534. clientX: rect.left + rect.width * 0.75,
  535. })
  536. cy.wrap($el).trigger('drop', {
  537. dataTransfer,
  538. clientX: rect.left + rect.width * 0.75,
  539. })
  540. })
  541. // Expected order after move: intro, appendix, main
  542. cy.findAllByRole('tab').eq(0).should('contain.text', 'intro.tex')
  543. cy.findAllByRole('tab').eq(1).should('contain.text', 'appendix.tex')
  544. cy.findAllByRole('tab').eq(2).should('contain.text', 'main.tex')
  545. })
  546. it('reorders tabs by dragging to the left of another tab', function () {
  547. // Open three tabs
  548. cy.then(() => selectDoc(DOC_IDS.main))
  549. cy.then(() => selectDoc(DOC_IDS.intro))
  550. cy.then(() => selectDoc(DOC_IDS.appendix))
  551. // Verify initial order
  552. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  553. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  554. cy.findAllByRole('tab').eq(2).should('contain.text', 'appendix.tex')
  555. // Drag appendix to the left of main
  556. const dataTransfer = new DataTransfer()
  557. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.appendix)
  558. cy.findByRole('tab', { name: /appendix\.tex/ }).trigger('dragstart', {
  559. dataTransfer,
  560. })
  561. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  562. const rect = $el[0].getBoundingClientRect()
  563. cy.wrap($el).trigger('dragover', {
  564. dataTransfer,
  565. clientX: rect.left + rect.width * 0.25,
  566. })
  567. cy.wrap($el).trigger('drop', {
  568. dataTransfer,
  569. clientX: rect.left + rect.width * 0.25,
  570. })
  571. })
  572. // Expected order: appendix, main, intro
  573. cy.findAllByRole('tab').eq(0).should('contain.text', 'appendix.tex')
  574. cy.findAllByRole('tab').eq(1).should('contain.text', 'main.tex')
  575. cy.findAllByRole('tab').eq(2).should('contain.text', 'intro.tex')
  576. })
  577. it('shows a drop indicator when dragging over a tab', function () {
  578. // Open two tabs
  579. cy.then(() => selectDoc(DOC_IDS.main))
  580. cy.then(() => selectDoc(DOC_IDS.intro))
  581. const dataTransfer = new DataTransfer()
  582. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.intro)
  583. cy.findByRole('tab', { name: /intro\.tex/ }).trigger('dragstart', {
  584. dataTransfer,
  585. })
  586. // Drag over left side of main tab
  587. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  588. const rect = $el[0].getBoundingClientRect()
  589. cy.wrap($el).trigger('dragover', {
  590. dataTransfer,
  591. clientX: rect.left + rect.width * 0.25,
  592. })
  593. })
  594. cy.findByRole('tab', { name: /main\.tex/ }).should(
  595. 'have.class',
  596. 'tab-drop-left'
  597. )
  598. // Drag leave should clear the indicator
  599. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragleave')
  600. cy.findByRole('tab', { name: /main\.tex/ }).should(
  601. 'not.have.class',
  602. 'tab-drop-left'
  603. )
  604. })
  605. it('drops onto the tablist to move a tab to the end', function () {
  606. // Open three tabs
  607. cy.then(() => selectDoc(DOC_IDS.main))
  608. cy.then(() => selectDoc(DOC_IDS.intro))
  609. cy.then(() => selectDoc(DOC_IDS.appendix))
  610. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  611. // Drag main to the tablist gutter (right of last tab)
  612. const dataTransfer = new DataTransfer()
  613. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  614. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  615. dataTransfer,
  616. })
  617. cy.findByRole('tablist').trigger('dragover', { dataTransfer })
  618. cy.findByRole('tablist').trigger('drop', { dataTransfer })
  619. // main should now be last
  620. cy.findAllByRole('tab').last().should('contain.text', 'main.tex')
  621. })
  622. it('does nothing when dropping a tab on itself', function () {
  623. // Open two tabs
  624. cy.then(() => selectDoc(DOC_IDS.main))
  625. cy.then(() => selectDoc(DOC_IDS.intro))
  626. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  627. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  628. // Drag main onto itself
  629. const dataTransfer = new DataTransfer()
  630. dataTransfer.setData(TAB_TRANSFER_TYPE, DOC_IDS.main)
  631. cy.findByRole('tab', { name: /main\.tex/ }).trigger('dragstart', {
  632. dataTransfer,
  633. })
  634. cy.findByRole('tab', { name: /main\.tex/ }).then($el => {
  635. const rect = $el[0].getBoundingClientRect()
  636. cy.wrap($el).trigger('dragover', {
  637. dataTransfer,
  638. clientX: rect.left + rect.width * 0.75,
  639. })
  640. cy.wrap($el).trigger('drop', {
  641. dataTransfer,
  642. clientX: rect.left + rect.width * 0.75,
  643. })
  644. })
  645. // Order unchanged
  646. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  647. cy.findAllByRole('tab').eq(1).should('contain.text', 'intro.tex')
  648. })
  649. })
  650. describe('Path disambiguation for duplicate names', function () {
  651. const duplicateNameRootFolder = makeRootFolder(
  652. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  653. [
  654. {
  655. _id: FOLDER_IDS.chapterA,
  656. name: 'chapter-a',
  657. docs: [{ _id: DOC_IDS.introA, name: 'intro.tex' }],
  658. folders: [],
  659. fileRefs: [],
  660. },
  661. {
  662. _id: FOLDER_IDS.chapterB,
  663. name: 'chapter-b',
  664. docs: [{ _id: DOC_IDS.introB, name: 'intro.tex' }],
  665. folders: [],
  666. fileRefs: [],
  667. },
  668. ]
  669. )
  670. it('shows disambiguated paths when two files share the same name', function () {
  671. mountTabs({ rootFolder: duplicateNameRootFolder })
  672. // Open main.tex (unique name, no disambiguation needed)
  673. cy.then(() => selectDoc(DOC_IDS.main))
  674. // Open intro.tex from chapter-a
  675. cy.then(() =>
  676. selectDoc(DOC_IDS.introA, ['root-folder-id', FOLDER_IDS.chapterA])
  677. )
  678. // Open intro.tex from chapter-b
  679. cy.then(() =>
  680. selectDoc(DOC_IDS.introB, ['root-folder-id', FOLDER_IDS.chapterB])
  681. )
  682. cy.findAllByRole('tab').should('have.length', 3)
  683. // main.tex is unique — should display without a path prefix
  684. cy.findAllByRole('tab').eq(0).should('contain.text', 'main.tex')
  685. // The two intro.tex tabs should be disambiguated with their folder names
  686. cy.findAllByRole('tab')
  687. .eq(1)
  688. .should('contain.text', 'chapter-a/intro.tex')
  689. cy.findAllByRole('tab')
  690. .eq(2)
  691. .should('contain.text', 'chapter-b/intro.tex')
  692. })
  693. it('uses only the file name when there is no collision', function () {
  694. mountTabs({ rootFolder: duplicateNameRootFolder })
  695. // Open just one of the duplicate-named files
  696. cy.then(() =>
  697. selectDoc(DOC_IDS.introA, ['root-folder-id', FOLDER_IDS.chapterA])
  698. )
  699. // With only one intro.tex open, no disambiguation is needed
  700. cy.findByRole('tab', { name: /intro\.tex/ }).should('exist')
  701. cy.findByRole('tab', { name: /intro\.tex/ }).should(
  702. 'not.contain.text',
  703. 'chapter-a/'
  704. )
  705. })
  706. })
  707. describe('Multiple file types', function () {
  708. it('displays tabs for both docs and file refs', function () {
  709. const rootFolder = makeRootFolder(
  710. [{ _id: DOC_IDS.main, name: 'main.tex' }],
  711. [],
  712. [{ _id: DOC_IDS.bibFile, name: 'refs.bib' }]
  713. )
  714. mountTabs({ rootFolder })
  715. // Open main doc
  716. cy.then(() => selectDoc(DOC_IDS.main))
  717. // Open a fileRef
  718. cy.then(() =>
  719. selectEntity(makeFileRefEntity(DOC_IDS.bibFile, 'refs.bib'))
  720. )
  721. cy.findByRole('tab', { name: /main\.tex/ }).should('exist')
  722. cy.findByRole('tab', { name: /refs\.bib/ }).should('exist')
  723. })
  724. })
  725. describe('Tab scrolling', function () {
  726. it('scrolls the selected tab into view', function () {
  727. const manyDocs = [
  728. { _id: DOC_IDS.main, name: 'main.tex' },
  729. ...Array.from({ length: 10 }, (_, i) => ({
  730. _id: `ch${i + 1}`,
  731. name: `chapter-${i + 1}.tex`,
  732. })),
  733. ]
  734. const rootFolder = makeRootFolder(manyDocs)
  735. mountTabs({ rootFolder })
  736. // Open main
  737. cy.then(() => selectDoc(DOC_IDS.main))
  738. // Open all chapter tabs first so they push main.tex out of view
  739. for (let i = 1; i <= 10; i++) {
  740. const id = `ch${i}`
  741. cy.then(() => selectEntity(makeDocEntity(id, `chapter-${i}.tex`)))
  742. cy.get('body').type('a')
  743. cy.findByRole('tab', { name: new RegExp(`chapter-${i}.tex`) }).should(
  744. 'be.visible'
  745. )
  746. }
  747. // Now select main again — it should be scrolled back into view
  748. cy.then(() => selectDoc(DOC_IDS.main))
  749. cy.findByRole('tab', { name: /main\.tex/ }).should('be.visible')
  750. })
  751. it('scrolls horizontally on vertical mouse wheel', function () {
  752. const manyDocs = [
  753. { _id: DOC_IDS.main, name: 'main.tex' },
  754. ...Array.from({ length: 10 }, (_, i) => ({
  755. _id: `ch${i + 1}`,
  756. name: `chapter-${i + 1}.tex`,
  757. })),
  758. ]
  759. const rootFolder = makeRootFolder(manyDocs)
  760. mountTabs({ rootFolder })
  761. // Open enough tabs to cause overflow
  762. cy.then(() => selectDoc(DOC_IDS.main))
  763. for (let i = 1; i <= 10; i++) {
  764. const id = `ch${i}`
  765. cy.then(() => selectEntity(makeDocEntity(id, `chapter-${i}.tex`)))
  766. }
  767. // Scroll back to the start
  768. cy.findByRole('tablist').then($el => {
  769. $el[0].scrollLeft = 0
  770. })
  771. // Trigger a vertical wheel event on the tablist
  772. cy.findByRole('tablist').trigger('wheel', { deltaY: 100, deltaX: 0 })
  773. // scrollLeft should have increased
  774. cy.findByRole('tablist').should($el => {
  775. expect($el[0].scrollLeft).to.be.greaterThan(0)
  776. })
  777. })
  778. })
  779. describe('SplitTestBadge', function () {
  780. it('renders the labs badge icon in the tabs container', function () {
  781. cy.window().then(win => {
  782. win.metaAttributesCache.set('ol-splitTestInfo', {
  783. 'editor-tabs': {
  784. phase: 'beta',
  785. badgeInfo: {
  786. url: '/beta/editor-tabs',
  787. tooltipText: 'Editor tabs are in beta',
  788. },
  789. },
  790. })
  791. })
  792. mountTabs()
  793. cy.then(() => selectDoc(DOC_IDS.main))
  794. cy.get('.editor-tabs-labs-icon').should('exist')
  795. })
  796. })
  797. })