-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update exercise 2: cypress components
- Loading branch information
1 parent
6147c66
commit fcd0411
Showing
12 changed files
with
435 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
{ | ||
"component": { | ||
"componentFolder": "exercise2-cypress-components", | ||
"testFiles": "**/*spec.{js,jsx,ts,tsx}" | ||
"testFiles": "**/*test.{js,jsx,ts,tsx}" | ||
}, | ||
"integrationFolder": "exercise3-cypress-integration", | ||
"testFiles": "**/*.spec.{js,jsx,ts,tsx}", | ||
"testFiles": "**/*.test.{js,jsx,ts,tsx}", | ||
"video": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { useEffect, useRef, useState } from "react" | ||
|
||
import "/components/meganav/meganav.scss" | ||
import MegaNavSection from "./meganav-section" | ||
|
||
|
||
const MegaNav = () => { | ||
const [activeMenu, setActiveMenu] = useState() | ||
const navRef = useRef() | ||
const menu1 = useRef() | ||
const menu2 = useRef() | ||
const menu3 = useRef() | ||
|
||
useEffect(() => { | ||
if (activeMenu) { | ||
document.addEventListener('mouseup', clickOutside) | ||
} else { | ||
document.removeEventListener('mouseup', clickOutside) | ||
} | ||
return () => { | ||
document.removeEventListener('mouseup', clickOutside) | ||
} | ||
}, [activeMenu]) | ||
|
||
const clickOutside = (event) => { | ||
if (!navRef.current.contains(event.target)) { | ||
setActiveMenu() | ||
} | ||
} | ||
const handleEscape = (event) => { | ||
if (event.key === 'Escape' && activeMenu) { | ||
setActiveMenu() | ||
} | ||
} | ||
const navItemClick = (menuRef) => { | ||
if (activeMenu === menuRef) { | ||
setActiveMenu() | ||
} else { | ||
setActiveMenu(menuRef) | ||
} | ||
} | ||
return ( | ||
<nav | ||
id="header-megamenu" | ||
onKeyUp={(event) => { handleEscape(event) }} | ||
ref={navRef} | ||
> | ||
<MegaNavSection | ||
activeMenu={activeMenu} | ||
id="megamenu-section1" | ||
buttonName="Plan Your Trip" | ||
ref={menu1} | ||
onClick={() => { navItemClick(menu1) }} | ||
/> | ||
<MegaNavSection | ||
activeMenu={activeMenu} | ||
id="megamenu-section2" | ||
buttonName="Ways to Stay" | ||
ref={menu2} | ||
onClick={() => { navItemClick(menu2) }} | ||
/> | ||
<MegaNavSection | ||
activeMenu={activeMenu} | ||
id="megamenu-section3" | ||
buttonName="Resources" | ||
ref={menu3} | ||
onClick={() => { navItemClick(menu3) }} | ||
/> | ||
</nav> | ||
) | ||
} | ||
export default MegaNav |
48 changes: 48 additions & 0 deletions
48
exercise2-cypress-components/answer/meganav/meganav-section.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React from "react" | ||
import LoadedImageUrl from "/components/utils/loaded-image-url" | ||
|
||
import navMap from "/data/nav-map.json" | ||
import * as imageMap from "../../../images/meganav/*.{jpg,svg}" | ||
|
||
import SubNavButton from './subnav-button' | ||
import SubNavLinkGroup from '/components/meganav/subnav-link-group' | ||
import SubNavImageBlock from '/components/meganav/subnav-image-block' | ||
|
||
const MegaNavSection = React.forwardRef((props, ref) => { | ||
const linkGroups = navMap[props.id].linkGroups | ||
let imageLinkBlocks | ||
if (navMap[props.id].imageLinkBlocks) { | ||
imageLinkBlocks = navMap[props.id].imageLinkBlocks | ||
} | ||
return ( | ||
<div | ||
className={` | ||
megamenu-section | ||
${props.activeMenu === ref ? 'active' : ''} | ||
`} | ||
> | ||
<SubNavButton | ||
aria-expanded={props.activeMenu === ref} | ||
buttonName={props.buttonName} | ||
className="megamenu-navitem header-main-item" | ||
idRef={props.id} | ||
onClick={(ref) => { props.onClick(ref) }} | ||
ref={ref} | ||
> | ||
</SubNavButton> | ||
|
||
<div className="megamenu-submenu" role="region" aria-labelledby={props.id}> | ||
{linkGroups.map((group, index) => { | ||
return <SubNavLinkGroup key={index} headerText={group.headerText} items={group.items} logoLinkRef={props.logoLinkRef} /> | ||
})} | ||
{imageLinkBlocks && imageLinkBlocks.map((block, index) => { | ||
const imageUrl = LoadedImageUrl(imageMap, block.imgSrc) | ||
const iconUrl = LoadedImageUrl(imageMap, block.iconSrc) | ||
return <SubNavImageBlock key={index} imgSrc={imageUrl} iconSrc={iconUrl} linkText={block.linkText} url={block.url} /> | ||
})} | ||
</div> | ||
</div> | ||
) | ||
}) | ||
|
||
export default MegaNavSection |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as React from 'react' | ||
import { mount } from '@cypress/react' | ||
|
||
import MegaNav from 'components/meganav' | ||
|
||
describe('MegaNav', () => { | ||
beforeEach(() => { | ||
mount(<MegaNav />) | ||
}) | ||
|
||
it('should operate with the keyboard via toggle buttons', () => { | ||
cy.get('[data-testid="megamenu-section1"]').focus().click() | ||
|
||
cy.focused().should('have.attr', 'aria-expanded', 'true') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.