Skip to content

Commit b671aaf

Browse files
Add browser tests for tool selector (github#19124)
Co-authored-by: James M. Greene <[email protected]>
1 parent 345e3c3 commit b671aaf

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

tests/browser/browser.js

+110
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,116 @@ describe('platform specific content', () => {
248248
})
249249
})
250250

251+
describe('tool specific content', () => {
252+
const pageWithSingleSwitcher = 'http://localhost:4001/en/actions/managing-workflow-runs/manually-running-a-workflow'
253+
const pageWithoutSwitcher = 'http://localhost:4001/en/billing/managing-billing-for-github-sponsors/about-billing-for-github-sponsors'
254+
const pageWithMultipleSwitcher = 'http://localhost:4001/en/issues/trying-out-the-new-projects-experience/using-the-api-to-manage-projects'
255+
256+
it('should have a tool switcher if a tool switcher is included', async () => {
257+
await page.goto(pageWithSingleSwitcher)
258+
const nav = await page.$$('nav#tool-switcher')
259+
const switches = await page.$$('a.tool-switcher')
260+
const selectedSwitch = await page.$$('a.tool-switcher.selected')
261+
expect(nav).toHaveLength(1)
262+
expect(switches.length).toBeGreaterThan(1)
263+
expect(selectedSwitch).toHaveLength(1)
264+
})
265+
266+
it('should have multiple tool switchers if multiple tools switchers are included', async () => {
267+
await page.goto(pageWithMultipleSwitcher)
268+
const toolSelector = await page.$$('nav#tool-switcher')
269+
const switches = await page.$$('a.tool-switcher')
270+
const selectedSwitch = await page.$$('a.tool-switcher.selected')
271+
console.log(switches.length)
272+
expect(toolSelector.length).toBeGreaterThan(1)
273+
expect(switches.length).toBeGreaterThan(1)
274+
expect(selectedSwitch.length).toEqual(toolSelector.length)
275+
})
276+
277+
it('should NOT have a tool switcher if no tool switcher is included', async () => {
278+
await page.goto(pageWithoutSwitcher)
279+
const toolSelector = await page.$$('nav#tool-switcher')
280+
const switches = await page.$$('a.tool-switcher')
281+
const selectedSwitch = await page.$$('a.tool-switcher.selected')
282+
expect(toolSelector).toHaveLength(0)
283+
expect(switches).toHaveLength(0)
284+
expect(selectedSwitch).toHaveLength(0)
285+
})
286+
287+
it('should use cli if no defaultTool is specified and if webui is not one of the tools', async () => {
288+
await page.goto(pageWithMultipleSwitcher)
289+
const selectedToolElement = await page.waitForSelector('a.tool-switcher.selected')
290+
const selectedTool = await page.evaluate(el => el.textContent, selectedToolElement)
291+
expect(selectedTool).toBe('GitHub CLI')
292+
})
293+
294+
it('should use webui if no defaultTool is specified and if webui is one of the tools', async () => {
295+
await page.goto(pageWithSingleSwitcher)
296+
const selectedToolElement = await page.waitForSelector('a.tool-switcher.selected')
297+
const selectedTool = await page.evaluate(el => el.textContent, selectedToolElement)
298+
expect(selectedTool).toBe('GitHub.com')
299+
})
300+
301+
it('should use the recorded user selection', async () => {
302+
// With no user data, the selected tool is GitHub.com
303+
await page.goto(pageWithSingleSwitcher)
304+
let selectedToolElement = await page.waitForSelector('a.tool-switcher.selected')
305+
let selectedTool = await page.evaluate(el => el.textContent, selectedToolElement)
306+
expect(selectedTool).toBe('GitHub.com')
307+
308+
await page.click(`.tool-switcher[data-tool="cli"]`)
309+
310+
// Revisiting the page after CLI is selected results in CLI as the selected tool
311+
await page.goto(pageWithSingleSwitcher)
312+
selectedToolElement = await page.waitForSelector('a.tool-switcher.selected')
313+
selectedTool = await page.evaluate(el => el.textContent, selectedToolElement)
314+
expect(selectedTool).toBe('GitHub CLI')
315+
})
316+
317+
it('should show the content for the selected tool only', async () => {
318+
await page.goto(pageWithSingleSwitcher)
319+
320+
const tools = ['webui', 'cli']
321+
for (const tool of tools) {
322+
await page.click(`.tool-switcher[data-tool="${tool}"]`)
323+
324+
// content for selected tool is expected to become visible
325+
await page.waitForSelector(`.extended-markdown.${tool}`, { visible: true, timeout: 3000 })
326+
327+
// only a single tab should be selected
328+
const selectedSwitch = await page.$$('a.tool-switcher.selected')
329+
expect(selectedSwitch).toHaveLength(1)
330+
331+
// content for NOT selected tools is expected to become hidden
332+
const otherTools = tools.filter(e => e !== tool)
333+
for (const other of otherTools) {
334+
await page.waitForSelector(`.extended-markdown.${other}`, { hidden: true, timeout: 3000 })
335+
}
336+
}
337+
})
338+
339+
it('selecting a tool in one switcher will control all tool switchers on the page', async () => {
340+
await page.goto(pageWithMultipleSwitcher)
341+
342+
const tools = { cli: 'GitHub CLI', curl: 'cURL' }
343+
for (const [tool, toolName] of Object.entries(tools)) {
344+
await page.click(`.tool-switcher[data-tool="${tool}"]`)
345+
346+
// content for selected tool is expected to become visible
347+
await page.waitForSelector(`.extended-markdown.${tool}`, { visible: true, timeout: 3000 })
348+
349+
// all tabs should be selected
350+
const toolSelector = await page.$$('nav#tool-switcher')
351+
const selectedSwitch = await page.$$('a.tool-switcher.selected')
352+
expect(selectedSwitch).toHaveLength(toolSelector.length)
353+
354+
const selectedToolElement = await page.waitForSelector('a.tool-switcher.selected')
355+
const selectedTool = await page.evaluate(el => el.textContent, selectedToolElement)
356+
expect(selectedTool).toBe(toolName)
357+
}
358+
})
359+
})
360+
251361
describe('code examples', () => {
252362
it('loads correctly', async () => {
253363
await page.goto('http://localhost:4001/en/actions')

0 commit comments

Comments
 (0)