-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathHeader.test.tsx
More file actions
185 lines (161 loc) · 5.18 KB
/
Header.test.tsx
File metadata and controls
185 lines (161 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { useStaticQuery } from 'gatsby';
import Header from './Header';
import UserContext from 'src/contexts/user-context';
jest.mock('src/contexts/layout-context', () => ({
useLayoutContext: jest.fn().mockReturnValue({
activePage: {
tree: [],
page: { name: '', link: '' },
languages: [],
language: 'javascript',
product: null,
template: null,
},
}),
}));
jest.mock('@ably/ui/core/Icon', () => {
const MockIcon: React.FC<{ name: string }> = ({ name }) => <div>{name}</div>;
MockIcon.displayName = 'MockIcon';
return MockIcon;
});
jest.mock('@ably/ui/core/LinkButton', () => {
const MockButton: React.FC<{ children: React.ReactNode }> = ({ children }) => <button>{children}</button>;
MockButton.displayName = 'MockButton';
return MockButton;
});
jest.mock('./LeftSidebar', () => ({
__esModule: true,
default: jest.fn(() => <div>LeftSidebar</div>),
}));
jest.mock('@reach/router', () => ({
useLocation: jest.fn().mockReturnValue({ pathname: '/docs' }),
}));
jest.mock('gatsby', () => ({
...jest.requireActual('gatsby'),
useStaticQuery: jest.fn().mockReturnValue({
site: {
siteMetadata: {
externalScriptsData: {
inkeepSearchEnabled: true,
inkeepChatEnabled: true,
},
},
},
}),
graphql: jest.fn(),
}));
jest.mock('./LanguageSelector', () => ({
LanguageSelector: jest.fn(() => <div>LanguageSelector</div>),
}));
jest.mock('../Link', () => {
const MockLink: React.FC<{ to: string; children: React.ReactNode; className?: string }> = ({ children }) => (
<a>{children}</a>
);
MockLink.displayName = 'MockLink';
return MockLink;
});
describe('Header', () => {
beforeEach(() => {
document.body.innerHTML = '';
});
afterEach(() => {
jest.clearAllMocks();
});
it('renders the header with logo and links', () => {
render(<Header />);
expect(screen.getByAltText('Ably')).toBeInTheDocument();
expect(screen.getByText('Docs')).toBeInTheDocument();
expect(screen.getByText('Platform')).toBeInTheDocument();
expect(screen.getByText('Products')).toBeInTheDocument();
expect(screen.getByText('Examples')).toBeInTheDocument();
});
it('toggles the mobile menu when the burger icon is clicked', () => {
render(<Header />);
const burgerIcon = screen.getByText('icon-gui-bars-3-outline');
fireEvent.click(burgerIcon);
expect(screen.getByText('LeftSidebar')).toBeInTheDocument();
});
it('renders the sign in buttons when not signed in', () => {
render(
<UserContext.Provider value={{ sessionState: { signedIn: false }, apps: [] }}>
<Header />
</UserContext.Provider>,
);
expect(screen.getByText('Login')).toBeInTheDocument();
expect(screen.getByText('Start free')).toBeInTheDocument();
expect(screen.queryByText('Dashboard')).not.toBeInTheDocument();
});
it('renders the dashboard button when signed in', () => {
render(
<UserContext.Provider
value={{
sessionState: {
signedIn: true,
account: {
id: 'test-id',
name: 'Test Account',
links: { dashboard: { href: '/dashboard', text: 'Dashboard' } },
},
},
apps: [],
}}
>
<Header />
</UserContext.Provider>,
);
expect(screen.queryByText('Login')).not.toBeInTheDocument();
expect(screen.queryByText('Start free')).not.toBeInTheDocument();
expect(screen.getByText('Dashboard')).toBeInTheDocument();
});
it('does not render search bar when inkeepSearchEnabled is false', () => {
(useStaticQuery as jest.Mock).mockReturnValue({
site: {
siteMetadata: {
externalScriptsData: {
inkeepSearchEnabled: false,
inkeepChatEnabled: true,
},
},
},
});
render(<Header />);
const searchBar = document.getElementById('inkeep-search');
expect(searchBar).not.toBeInTheDocument();
});
it('does not render Ask AI button and chat bar when inkeepChatEnabled is false', () => {
(useStaticQuery as jest.Mock).mockReturnValue({
site: {
siteMetadata: {
externalScriptsData: {
inkeepSearchEnabled: true,
inkeepChatEnabled: false,
},
},
},
});
render(<Header />);
expect(screen.queryByText('Ask AI')).not.toBeInTheDocument();
const chatBar = document.getElementById('inkeep-ai-chat');
expect(chatBar).not.toBeInTheDocument();
});
it('does not render search bar or Ask AI button when both flags are false', () => {
(useStaticQuery as jest.Mock).mockReturnValue({
site: {
siteMetadata: {
externalScriptsData: {
inkeepSearchEnabled: false,
inkeepChatEnabled: false,
},
},
},
});
render(<Header />);
expect(screen.queryByText('Ask AI')).not.toBeInTheDocument();
const searchBar = document.getElementById('inkeep-search');
const chatBar = document.getElementById('inkeep-ai-chat');
expect(searchBar).not.toBeInTheDocument();
expect(chatBar).not.toBeInTheDocument();
});
});