2019-09-19 19:45:01 +03:00
|
|
|
/* global expect */
|
|
|
|
import React from 'react';
|
|
|
|
import ShallowRenderer from 'react-test-renderer/shallow';
|
2020-09-03 14:21:16 +03:00
|
|
|
import renderer from 'react-test-renderer';
|
2019-09-19 19:45:01 +03:00
|
|
|
|
2020-09-03 14:21:16 +03:00
|
|
|
import { UniversalNav } from './components/UniversalNav';
|
|
|
|
import { AuthOrProfile } from './components/NavLinks';
|
2020-08-14 11:56:14 +02:00
|
|
|
|
2019-10-04 18:00:17 +03:00
|
|
|
describe('<UniversalNav />', () => {
|
2020-09-03 14:21:16 +03:00
|
|
|
const UniversalNavProps = {
|
|
|
|
displayMenu: false,
|
|
|
|
menuButtonRef: {},
|
|
|
|
searchBarRef: {},
|
|
|
|
toggleDisplayMenu: function() {},
|
|
|
|
pathName: '/'
|
|
|
|
};
|
2019-09-19 19:45:01 +03:00
|
|
|
it('renders to the DOM', () => {
|
|
|
|
const shallow = new ShallowRenderer();
|
2019-10-04 18:00:17 +03:00
|
|
|
shallow.render(<UniversalNav {...UniversalNavProps} />);
|
2019-09-19 19:45:01 +03:00
|
|
|
const result = shallow.getRenderOutput();
|
|
|
|
expect(result).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
2020-09-03 14:21:16 +03:00
|
|
|
|
2019-09-19 19:45:01 +03:00
|
|
|
describe('<NavLinks />', () => {
|
2020-10-22 01:20:22 +03:00
|
|
|
it('shows Curriculum and Sign In buttons when not signed in', () => {
|
2020-09-03 14:21:16 +03:00
|
|
|
const landingPageProps = {
|
2020-10-22 01:20:22 +03:00
|
|
|
pending: false
|
2020-09-03 14:21:16 +03:00
|
|
|
};
|
|
|
|
const shallow = new ShallowRenderer();
|
|
|
|
shallow.render(<AuthOrProfile {...landingPageProps} />);
|
|
|
|
const result = shallow.getRenderOutput();
|
2019-09-19 19:45:01 +03:00
|
|
|
|
2020-09-03 14:21:16 +03:00
|
|
|
expect(
|
2020-10-22 19:27:41 +03:00
|
|
|
hasForumNavItem(result) &&
|
|
|
|
hasCurriculumNavItem(result) &&
|
|
|
|
hasSignInButton(result)
|
2020-09-03 14:21:16 +03:00
|
|
|
).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has avatar with default border for default users', () => {
|
|
|
|
const defaultUserProps = {
|
|
|
|
user: {
|
|
|
|
username: 'test-user',
|
|
|
|
picture: 'https://freecodecamp.org/image.png'
|
|
|
|
},
|
2020-10-22 01:20:22 +03:00
|
|
|
pending: false
|
2020-09-03 14:21:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const componentTree = renderer
|
|
|
|
.create(<AuthOrProfile {...defaultUserProps} />)
|
|
|
|
.toJSON();
|
|
|
|
|
|
|
|
expect(avatarHasClass(componentTree, 'default-border')).toBeTruthy();
|
2019-09-19 19:45:01 +03:00
|
|
|
});
|
2020-09-03 14:21:16 +03:00
|
|
|
|
|
|
|
it('has avatar with gold border for donating users', () => {
|
|
|
|
const donatingUserProps = {
|
|
|
|
user: {
|
|
|
|
username: 'test-user',
|
|
|
|
picture: 'https://freecodecamp.org/image.png',
|
|
|
|
isDonating: true
|
|
|
|
},
|
2020-10-22 01:20:22 +03:00
|
|
|
pending: false
|
2020-09-03 14:21:16 +03:00
|
|
|
};
|
|
|
|
const componentTree = renderer
|
|
|
|
.create(<AuthOrProfile {...donatingUserProps} />)
|
|
|
|
.toJSON();
|
|
|
|
|
|
|
|
expect(avatarHasClass(componentTree, 'gold-border')).toBeTruthy();
|
2019-09-19 19:45:01 +03:00
|
|
|
});
|
|
|
|
|
2020-09-03 14:21:16 +03:00
|
|
|
it('has avatar with green border for top contributors', () => {
|
|
|
|
const topContributorUserProps = {
|
|
|
|
user: {
|
|
|
|
username: 'test-user',
|
|
|
|
picture: 'https://freecodecamp.org/image.png',
|
|
|
|
yearsTopContributor: [2020]
|
|
|
|
},
|
2020-10-22 01:20:22 +03:00
|
|
|
pending: false
|
2020-09-03 14:21:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const componentTree = renderer
|
|
|
|
.create(<AuthOrProfile {...topContributorUserProps} />)
|
|
|
|
.toJSON();
|
|
|
|
|
|
|
|
expect(avatarHasClass(componentTree, 'green-border')).toBeTruthy();
|
|
|
|
});
|
|
|
|
it('has avatar with purple border for donating top contributors', () => {
|
|
|
|
const topDonatingContributorUserProps = {
|
|
|
|
user: {
|
|
|
|
username: 'test-user',
|
|
|
|
picture: 'https://freecodecamp.org/image.png',
|
|
|
|
isDonating: true,
|
|
|
|
yearsTopContributor: [2020]
|
|
|
|
},
|
2020-10-22 01:20:22 +03:00
|
|
|
pending: false
|
2020-09-03 14:21:16 +03:00
|
|
|
};
|
|
|
|
const componentTree = renderer
|
|
|
|
.create(<AuthOrProfile {...topDonatingContributorUserProps} />)
|
|
|
|
.toJSON();
|
|
|
|
expect(avatarHasClass(componentTree, 'purple-border')).toBeTruthy();
|
2019-09-19 19:45:01 +03:00
|
|
|
});
|
|
|
|
});
|
2019-10-04 18:00:17 +03:00
|
|
|
|
2020-10-22 19:27:41 +03:00
|
|
|
const navigationLinks = (component, navItem) => {
|
|
|
|
return component.props.children[0].props.children[navItem].props.children
|
|
|
|
.props;
|
|
|
|
};
|
2020-09-03 14:21:16 +03:00
|
|
|
|
2020-10-22 19:27:41 +03:00
|
|
|
const profileNavItem = component => component[2].children[0];
|
|
|
|
|
|
|
|
const hasForumNavItem = component => {
|
|
|
|
const { children, to } = navigationLinks(component, 0);
|
|
|
|
return children === 'Forum' && to === 'https://forum.freecodecamp.org';
|
2020-09-03 14:21:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const hasCurriculumNavItem = component => {
|
2020-10-22 19:27:41 +03:00
|
|
|
const { children, to } = navigationLinks(component, 1);
|
|
|
|
return children === 'Curriculum' && to === '/learn';
|
2020-09-03 14:21:16 +03:00
|
|
|
};
|
|
|
|
|
2020-10-22 19:27:41 +03:00
|
|
|
const hasSignInButton = component =>
|
|
|
|
component.props.children[1].props.children === 'Sign In';
|
|
|
|
|
2020-09-03 14:21:16 +03:00
|
|
|
const avatarHasClass = (componentTree, classes) => {
|
2020-10-22 19:27:41 +03:00
|
|
|
// componentTree[1].children[0].children[1].props.className
|
2020-09-03 14:21:16 +03:00
|
|
|
return (
|
2020-10-22 19:27:41 +03:00
|
|
|
profileNavItem(componentTree).children[1].props.className ===
|
2020-09-03 14:21:16 +03:00
|
|
|
'avatar-container ' + classes
|
|
|
|
);
|
2019-10-04 18:00:17 +03:00
|
|
|
};
|