/** * Copyright (c) 2023 Google LLC * SPDX-License-Identifier: MIT */ import React from 'react'; import { fireEvent, screen } from '@testing-library/react'; import { renderWithTheme } from '@looker/test-utils'; import { mockTreeData } from './stories/examples'; import { TreeResults } from './TreeResults'; describe('TreeResults', () => { test('loading', async () => { renderWithTheme(); expect(screen.queryByRole('treeitem')).not.toBeInTheDocument(); expect(screen.getByTestId('loading-spinner')).toBeVisible(); }); test('search', async () => { const { rerender } = renderWithTheme( ); expect(screen.getAllByRole('treeitem')).toHaveLength(2); rerender(); expect(screen.getAllByRole('treeitem')).toHaveLength(8); }); test('no results', async () => { renderWithTheme( ); expect(screen.queryByRole('treeitem')).not.toBeInTheDocument(); expect(screen.getByText('No matching fields')).toBeVisible(); }); test('open then close nodes', async () => { renderWithTheme(); expect(screen.getAllByRole('treeitem')).toHaveLength(2); const root1 = screen.getByText('Root1'); fireEvent.click(root1); expect(screen.getAllByRole('treeitem')).toHaveLength(4); const someSection = screen.getByText('some section'); fireEvent.click(someSection); expect(screen.getAllByRole('treeitem')).toHaveLength(6); fireEvent.click(someSection); expect(screen.getAllByRole('treeitem')).toHaveLength(4); fireEvent.click(root1); expect(screen.getAllByRole('treeitem')).toHaveLength(2); }); test('search then close and open a node', async () => { renderWithTheme( ); expect(screen.getAllByRole('treeitem')).toHaveLength(8); fireEvent.click(screen.getByText('some section')); expect(screen.getAllByRole('treeitem')).toHaveLength(6); fireEvent.click(screen.getByText('some section')); expect(screen.getAllByRole('treeitem')).toHaveLength(8); }); });