Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/javascript/grid-ui/src/tests/components/TopBar.test.tsx
3990 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
import * as React from 'react'
19
import TopBar from '../../components/TopBar/TopBar'
20
import { render, screen } from '@testing-library/react'
21
import userEvent from '@testing-library/user-event'
22
import { CustomThemeProvider } from '../../contexts/ThemeContext'
23
24
const user = userEvent.setup()
25
26
beforeEach(() => {
27
Object.defineProperty(window, 'matchMedia', {
28
writable: true,
29
value: jest.fn().mockImplementation(() => ({
30
matches: false,
31
addEventListener: jest.fn(),
32
removeEventListener: jest.fn()
33
}))
34
})
35
})
36
37
it('renders basic information', () => {
38
const subheaderText = 'Hello, world!'
39
const handleClick = jest.fn()
40
render(
41
<CustomThemeProvider>
42
<TopBar subheader={subheaderText} drawerOpen toggleDrawer={handleClick}/>
43
</CustomThemeProvider>
44
)
45
expect(screen.getByText('Selenium Grid')).toBeInTheDocument()
46
expect(screen.getByRole('img')).toHaveAttribute('alt', 'Selenium Grid Logo')
47
expect(screen.getByText(subheaderText)).toBeInTheDocument()
48
})
49
50
it('can toggle drawer if error flag is not set and the drawer is open',
51
async () => {
52
const handleClick = jest.fn()
53
render(
54
<CustomThemeProvider>
55
<TopBar subheader="4.0.0" drawerOpen toggleDrawer={handleClick}/>
56
</CustomThemeProvider>
57
)
58
const drawerButton = screen.getByLabelText('close drawer')
59
expect(drawerButton.getAttribute('aria-label')).toBe('close drawer')
60
await user.click(drawerButton)
61
expect(handleClick).toHaveBeenCalledTimes(1)
62
})
63
64
it('can toggle drawer if error flag is not set and the drawer is closed',
65
async () => {
66
const handleClick = jest.fn()
67
render(
68
<CustomThemeProvider>
69
<TopBar subheader="4.0.0" toggleDrawer={handleClick}/>
70
</CustomThemeProvider>
71
)
72
const drawerButton = screen.getByLabelText('open drawer')
73
expect(drawerButton.getAttribute('aria-label')).toBe('open drawer')
74
await user.click(drawerButton)
75
expect(handleClick).toHaveBeenCalledTimes(1)
76
})
77
78
it('should not toggle drawer if error flag is set', async () => {
79
const handleClick = jest.fn()
80
render(
81
<CustomThemeProvider>
82
<TopBar subheader="4.0.0" error toggleDrawer={handleClick}/>
83
</CustomThemeProvider>
84
)
85
expect(screen.queryByLabelText('close drawer')).not.toBeInTheDocument()
86
expect(screen.queryByLabelText('open drawer')).not.toBeInTheDocument()
87
const link = screen.getByRole('link')
88
expect(link.getAttribute('href')).toBe('#help')
89
await user.click(link)
90
expect(handleClick).toHaveBeenCalledTimes(0)
91
})
92
93