// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { expect, galata, test } from '@jupyterlab/galata';
import * as path from 'path';

const fileName = 'toc_scrolling_notebook.ipynb';

test.use({ tmpPath: 'test-toc' });

test.describe('Table of Contents scrolling to heading', () => {
  test.beforeAll(async ({ request, tmpPath }) => {
    const contents = galata.newContentsHelper(request);
    await contents.uploadFile(
      path.resolve(__dirname, `./notebooks/${fileName}`),
      `${tmpPath}/${fileName}`
    );
  });

  test.beforeEach(async ({ page, tmpPath }) => {
    await page.notebook.openByPath(`${tmpPath}/${fileName}`);
    await page.notebook.activate(fileName);

    await page.sidebar.openTab('table-of-contents');
  });

  test.afterEach(async ({ page }) => {
    await page.notebook.close(true);
  });

  test.afterAll(async ({ request, tmpPath }) => {
    const contents = galata.newContentsHelper(request);
    await contents.deleteDirectory(tmpPath);
  });

  test('Notebook scrolls to heading', async ({ page }) => {
    await page.notebook.selectCells(0);

    await page.sidebar.getContentPanel(
      await page.sidebar.getTabPosition('table-of-contents')
    );

    await page
      .locator('.jp-TableOfContents-tree >> text="the last one"')
      .click({
        button: 'left'
      });

    const nbPanel = await page.notebook.getNotebookInPanel();
    expect(await nbPanel.screenshot()).toMatchSnapshot(
      'scrolled-to-bottom-heading.png'
    );
  });
});
