Skip to content

Instantly share code, notes, and snippets.

@raulb
Created February 20, 2012 04:17
Show Gist options
  • Save raulb/1867822 to your computer and use it in GitHub Desktop.
Save raulb/1867822 to your computer and use it in GitHub Desktop.
Fadeout test with Jasmine
// If we want to test Asynchronous events we should use waits or waitsFor
// To test a fadeOut effect, waits(num ms) could be ok, but is not too smart in my opinion
// more info: https://github.com/pivotal/jasmine/wiki/Asynchronous-specs
// What happens if we want to test if any element is correctly deleted but before of that the element is going into a fadeOut
// event?. Which is happening is its opacity is going to 0 but immediately of that the element is undefined.
// This is my way to do it:
it("If one element is deleted correctly", function() {
loadFixtures("fixtureContent.html");
expect($('#elements li').size()).toEqual(1);
$('#removeElement').click();
// Wait for fadeOut event finished
waitsFor(function() {
return (($('#elementToRemove').css('opacity') == 0) || $('#elementToRemove').css('opacity') == undefined);
}, "The element won't ever be hidden", 10000);
runs(function () {
expect($('#elements li').size()).toEqual(0);
});
});
@marcagas
Copy link

can you explain clearly what does the waitsFor do? could i remove the element inside the waitsFor function? cause what I did in my fadeOut callback is to remove the element

@raulb
Copy link
Author

raulb commented Jun 22, 2012

Sure you could remove it using the fadeOut callback (that's the right way to do it). Since the scope of this code is a spec, the idea under waitsFor is testing that you're doing it right.

More info about asynchronous specs here: https://github.com/pivotal/jasmine/wiki/Asynchronous-specs

:bowtie:

@marcagas
Copy link

marcagas commented Jul 4, 2012

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment