diff --git a/src/ui/public/jquery/__tests__/findTestSubject.js b/src/ui/public/jquery/__tests__/findTestSubject.js new file mode 100644 index 000000000000..02c1ae874c62 --- /dev/null +++ b/src/ui/public/jquery/__tests__/findTestSubject.js @@ -0,0 +1,59 @@ +var $ = require('jquery'); +var expect = require('expect.js'); + +function $make(subject) { + return $('
').attr('data-test-subj', subject); +} + +describe('jQuery.findTestSubject', function () { + it('finds all of the element with a subject', function () { + var $container = $('
'); + var $match = $make('subject').appendTo($container); + var $noMatch = $make('notSubject').appendTo($container); + + var $found = $container.findTestSubject('subject'); + expect($found.is($match)).to.be(true); + expect($found.is($noMatch)).to.be(false); + }); + + it('finds all of the elements with either "saveButton" or "cancelButton"', function () { + var $container = $('
'); + var $match1 = $make('subject').appendTo($container); + var $match2 = $make('alsoSubject').appendTo($container); + var $noMatch = $make('notSubject').appendTo($container); + + var $found = $container.findTestSubject('subject', 'alsoSubject'); + expect($found.filter($match1).size()).to.be(1); + expect($found.filter($match2).size()).to.be(1); + expect($found.filter($noMatch).size()).to.be(0); + }); + + it('finds all of the elements with a decendant selector', function () { + var $container = $('
'); + var $parent = $make('foo name').appendTo($container); + var $bar = $make('bar othername').appendTo($parent); + var $baz = $make('baz third name').appendTo($parent); + + expect($container.findTestSubject('foo bar').is($bar)).to.be(true); + expect($container.findTestSubject('foo bar').is($baz)).to.be(false); + + expect($container.findTestSubject('foo baz').is($bar)).to.be(false); + expect($container.findTestSubject('foo baz').is($baz)).to.be(true); + }); + + it('finds elements with compound subjects', function () { + var $container = $('
'); + var $bar = $make('button bar').appendTo($container); + var $baz = $make('button baz').appendTo($container); + + expect($container.findTestSubject('button&bar').is($bar)).to.be(true); + expect($container.findTestSubject('button& bar').is($bar)).to.be(true); + expect($container.findTestSubject('button & bar').is($bar)).to.be(true); + expect($container.findTestSubject('button &bar').is($bar)).to.be(true); + + expect($container.findTestSubject('button&baz').is($baz)).to.be(true); + expect($container.findTestSubject('button& baz').is($baz)).to.be(true); + expect($container.findTestSubject('button & baz').is($baz)).to.be(true); + expect($container.findTestSubject('button &baz').is($baz)).to.be(true); + }); +});