-
-
Save TheSavior/dbe21187dc7c8a6cb2bd5000d100e555 to your computer and use it in GitHub Desktop.
A full example of using mocha and jsdom in coderpad.
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
const jsdom = require('jsdom'); | |
const Mocha = require('mocha'); | |
const assert = require('assert'); | |
const mocha = new Mocha(); | |
function getContentByClassName(className) { | |
const elements = document.getElementsByClassName(className); | |
if (elements.length === 0) { | |
return ''; | |
} | |
return elements[0].textContent; | |
} | |
const topLevel = this; | |
mocha.suite.emit('pre-require', topLevel, 'solution', mocha); | |
describe('#getContentByClassName', () => { | |
it('should return an empty string if given a non existent class', () => { | |
topLevel.document = jsdom.jsdom('<div class="foo">my content</div>'); | |
assert(getContentByClassName('bar') === ''); | |
}); | |
it('should return the content of a class', () => { | |
topLevel.document = jsdom.jsdom('<div class="foo">my content</div>'); | |
assert(getContentByClassName('foo') === 'my content'); | |
}); | |
}); | |
mocha.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment