When writing a Mocha/Chai test for an angular view, I've discovered that jqlite (since I'm not using jQuery), won't find top-level elements.
I've boiled this down to a simple example:
/* jshint expr:true */
describe( "View", function() {
'use strict';
var $compile, $rootScope;
beforeEach( inject( function( _$compile_, _$rootScope_ ) {
$compile = _$compile_;
$rootScope = _$rootScope_;
} ) );
var view;
beforeEach( function() {
var html = "<div><span>div[0].span</span><p>div[0].p</p></div><div><h1>div[1].h1</h1><p>div[1].p</div>";
view = $compile( angular.element( html ) )( $rootScope );
} );
it( "should find two p tags", function() {
expect( view.find( "p" ).length ).to.equal( 2 );
} );
it( "should find one span tag", function() {
expect( view.find( "span" ).length ).to.equal( 1 );
} );
it( "should find two div tags", function() {
expect( view.find( "div" ).length ).to.equal( 2 );
} );
} );
In this example, the first two tests pass, the third fails:
View
✓ should find two p tags
✓ should find one span tag
✗ should find two div tags
AssertionError: expected 0 to equal 2
at ...
This seems to be because the divs are top-level elements, the span and p tags are beneath them. If I enclose the whole thing in another tag, the third test will start to pass.
Why?