Path: blob/trunk/javascript/grid-ui/src/tests/components/TopBar.test.tsx
3990 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617import * as React from 'react'18import TopBar from '../../components/TopBar/TopBar'19import { render, screen } from '@testing-library/react'20import userEvent from '@testing-library/user-event'21import { CustomThemeProvider } from '../../contexts/ThemeContext'2223const user = userEvent.setup()2425beforeEach(() => {26Object.defineProperty(window, 'matchMedia', {27writable: true,28value: jest.fn().mockImplementation(() => ({29matches: false,30addEventListener: jest.fn(),31removeEventListener: jest.fn()32}))33})34})3536it('renders basic information', () => {37const subheaderText = 'Hello, world!'38const handleClick = jest.fn()39render(40<CustomThemeProvider>41<TopBar subheader={subheaderText} drawerOpen toggleDrawer={handleClick}/>42</CustomThemeProvider>43)44expect(screen.getByText('Selenium Grid')).toBeInTheDocument()45expect(screen.getByRole('img')).toHaveAttribute('alt', 'Selenium Grid Logo')46expect(screen.getByText(subheaderText)).toBeInTheDocument()47})4849it('can toggle drawer if error flag is not set and the drawer is open',50async () => {51const handleClick = jest.fn()52render(53<CustomThemeProvider>54<TopBar subheader="4.0.0" drawerOpen toggleDrawer={handleClick}/>55</CustomThemeProvider>56)57const drawerButton = screen.getByLabelText('close drawer')58expect(drawerButton.getAttribute('aria-label')).toBe('close drawer')59await user.click(drawerButton)60expect(handleClick).toHaveBeenCalledTimes(1)61})6263it('can toggle drawer if error flag is not set and the drawer is closed',64async () => {65const handleClick = jest.fn()66render(67<CustomThemeProvider>68<TopBar subheader="4.0.0" toggleDrawer={handleClick}/>69</CustomThemeProvider>70)71const drawerButton = screen.getByLabelText('open drawer')72expect(drawerButton.getAttribute('aria-label')).toBe('open drawer')73await user.click(drawerButton)74expect(handleClick).toHaveBeenCalledTimes(1)75})7677it('should not toggle drawer if error flag is set', async () => {78const handleClick = jest.fn()79render(80<CustomThemeProvider>81<TopBar subheader="4.0.0" error toggleDrawer={handleClick}/>82</CustomThemeProvider>83)84expect(screen.queryByLabelText('close drawer')).not.toBeInTheDocument()85expect(screen.queryByLabelText('open drawer')).not.toBeInTheDocument()86const link = screen.getByRole('link')87expect(link.getAttribute('href')).toBe('#help')88await user.click(link)89expect(handleClick).toHaveBeenCalledTimes(0)90})919293