Skip to content

Instantly share code, notes, and snippets.

@raffaele-abramini
Last active October 16, 2017 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raffaele-abramini/48c01986d4f10de259e40710add20264 to your computer and use it in GitHub Desktop.
Save raffaele-abramini/48c01986d4f10de259e40710add20264 to your computer and use it in GitHub Desktop.
testing example #2 - spec
import MainNav from './mainNav';
describe('With HTML elements missing', ()=> {
test('It\'s initialised without errors', ()=>{
// When the DOM doesn't contain the main element, we want this component to stop the construction
// to avoid errors.
document.body.innerHTML = '';
new MainNav();
});
test('Returns an error if inner elements are missing', ()=>{
// When the DOM contains the main element, but not some of the required childre, we want
// the component to return an useful error message.
document.body.innerHTML = '<div class="article" data-js-main-nav></div>';
try {
new MainNav();
} catch(e) {
expect(e.message).toEqual(MainNav.errors.noElements);
}
});
});
describe('On default scenarios', ()=> {
let mainNav;
beforeAll(()=>{
// Add the basic html structure.
document.body.innerHTML = `
<div class="article" data-js-main-nav>
<button data-main-nav-trigger></button>
<ul data-main-nav-content>
<li>1</li>
<li>2</li>
</ul>
</div>
`;
// Create mainNav instance
mainNav = new MainNav();
});
test('It\'s initialised without errors', ()=>{});
test('Click on button changes button text', ()=>{
// On first click
mainNav.trigger.click();
// we expect the button to have the 'close' label;
expect(mainNav.trigger.innerText).toEqual(MainNav.labels.close);
// On second click
mainNav.trigger.click();
// we expect the button to have the 'open' label
expect(mainNav.trigger.innerText).toEqual(MainNav.labels.open);
});
test('Click on button changes content visibility', ()=>{
// By default we expect the content to be hidden, hence to have the '.is-hidden' class;
expect(mainNav.content.classList.contains(MainNav.classes.isHidden)).toBeTruthy();
// On first click
mainNav.trigger.click();
// we expect the content to be visible
expect(mainNav.content.classList.contains(MainNav.classes.isHidden)).toBeFalsy();
// On second click
mainNav.trigger.click();
// we expect the content to be hidden again
expect(mainNav.content.classList.contains(MainNav.classes.isHidden)).toBeTruthy();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment