diff --git a/src/components/NavBar/NavBar.test.js b/src/components/NavBar/NavBar.test.js index 7581cb9f..003256c6 100644 --- a/src/components/NavBar/NavBar.test.js +++ b/src/components/NavBar/NavBar.test.js @@ -1,7 +1,93 @@ +import React from 'react'; import NavBar from './index'; describe('NavBar', () => { - it('TODO', () => { - expect(true).to.equal(true); + const classes = { + tabs: 'tyk-nav-bar__with-tabs', + alignCenter: 'tyk-nav-bar--align-center', + alignTop: 'tyk-nav-bar--align-top', + }; + const selectors = { + navbar: '.tyk-nav-bar', + wrapper: '.tyk-nav-bar__wrapper', + container: '.tyk-nav-bar__container', + pre: '.tyk-nav-bar__pre', + title: '.tyk-nav-bar__wrapper h1', + subtitle: '.tyk-nav-bar__wrapper p', + containerLeft: '.tyk-nav-bar__left', + containerRight: '.tyk-nav-bar__right', + }; + + it('renders the component, a wrapper, and a container', () => { + cy.mount() + .get(selectors.navbar) + .should('exist') + .get(selectors.wrapper) + .should('exist') + .get(selectors.container) + .should('exist'); + }); + + it('adds the tabs class if withTabs prop is used', () => { + cy.mount() + .get(selectors.navbar) + .should('have.class', classes.tabs); + }); + + it('the wrapper can be aligned with the "align" prop, "center" by default', () => { + cy.mount() + .get(selectors.wrapper) + .should('have.class', classes.alignCenter); + + cy.mount() + .get(selectors.wrapper) + .should('have.class', classes.alignTop); + }); + + it('prepends content if "pre" prop is used', () => { + const pre = 'something'; + cy.mount() + .get(selectors.pre) + .should('exist') + .and('have.text', pre); + }); + + it('can render a title', () => { + const title = 'my title'; + cy.mount() + .get(selectors.title) + .should('exist') + .and('have.text', title); + }); + + it('can render a subtitle', () => { + const subtitle = 'my subtitle'; + cy.mount() + .get(selectors.subtitle) + .should('exist') + .and('have.text', subtitle); + }); + + it('can render content on the left side of the container', () => { + const content = 'my content'; + cy.mount() + .get(selectors.containerLeft) + .should('exist') + .and('have.text', content); + }); + + it('can render content on the right side of the container', () => { + const content = 'my content'; + cy.mount() + .get(selectors.containerRight) + .should('exist') + .and('have.text', content); + }); + + it('can have children', () => { + const content = 'component content'; + cy.mount({content}) + .get(selectors.navbar) + .and('have.text', content); }); }); diff --git a/src/components/NavBar/index.js b/src/components/NavBar/index.js index db043fb0..0371d734 100644 --- a/src/components/NavBar/index.js +++ b/src/components/NavBar/index.js @@ -1,81 +1,70 @@ -import React, { PureComponent } from 'react'; +import React from 'react'; import PropTypes from 'prop-types'; -export default class NavBar extends PureComponent { - static propTypes = { - align: PropTypes.string, - children: PropTypes.oneOfType([ - PropTypes.arrayOf(PropTypes.node), - PropTypes.node, - PropTypes.element, - PropTypes.string, - ]), - title: PropTypes.string, - subtitle: PropTypes.string, - pre: PropTypes.oneOfType([ - PropTypes.element, - PropTypes.node, - ]), - left: PropTypes.oneOfType([ - PropTypes.element, - PropTypes.node, - ]), - right: PropTypes.oneOfType([ - PropTypes.element, - PropTypes.node, - ]), - withTabs: PropTypes.bool, - } - - render() { - const { - align = 'center', - children, - left, - right, - title, - pre, - subtitle, - withTabs, - } = this.props; - - return ( -
-
- { - pre - ?
{pre}
- : null - } - { - title - ? ( -
-

{ title }

- { - subtitle - ?

{subtitle}

- : null - } -
- ) - : subtitle &&

{subtitle}

- } -
- { - left - ?
{ left }
- : '' - } - { - right - ?
{ right }
- : '' - } -
+function NavBar({ + align = 'center', + children, + left, + right, + title, + pre, + subtitle, + withTabs, +}) { + const subtitleContent = subtitle ?

{ subtitle }

: null; + return ( +
+
+ {pre && ( +
{pre}
+ )} + { + title + ? ( +
+

{ title }

+ { subtitleContent } +
+ ) + : subtitleContent + } +
+ {left && ( +
{ left }
+ )} + {right && ( +
{ right }
+ )}
- { children } -
- ); - } +
+ { children } +
+ ); } + +NavBar.propTypes = { + align: PropTypes.string, + children: PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.node), + PropTypes.node, + PropTypes.element, + PropTypes.string, + ]), + title: PropTypes.string, + subtitle: PropTypes.string, + pre: PropTypes.oneOfType([ + PropTypes.element, + PropTypes.node, + ]), + left: PropTypes.oneOfType([ + PropTypes.element, + PropTypes.node, + ]), + right: PropTypes.oneOfType([ + PropTypes.element, + PropTypes.node, + ]), + withTabs: PropTypes.bool, +}; + +export default NavBar;