Remove ui/jquery (#33855)

This commit is contained in:
Rudolf Meijering 2019-03-28 20:53:07 +01:00 committed by GitHub
parent 980d736c04
commit 5f511fcd83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 179 deletions

View file

@ -19,6 +19,7 @@
import ngMock from 'ng_mock';
import expect from '@kbn/expect';
import testSubjSelector from '@kbn/test-subj-selector';
import sinon from 'sinon';
import $ from 'jquery';
@ -85,7 +86,7 @@ describe('fancy forms', function () {
expect($scope.person.errorCount()).to.be(1);
expect($scope.person.softErrorCount()).to.be(0);
$el.findTestSubject('submit').click();
$el.find(testSubjSelector('submit')).click();
expect($scope.person.errorCount()).to.be(1);
expect($scope.person.softErrorCount()).to.be(1);
});
@ -96,7 +97,7 @@ describe('fancy forms', function () {
expect($scope.person.errorCount()).to.be(1);
sinon.assert.notCalled(onSubmit);
$el.findTestSubject('submit').click();
$el.find(testSubjSelector('submit')).click();
expect($scope.person.errorCount()).to.be(1);
sinon.assert.notCalled(onSubmit);
@ -106,7 +107,7 @@ describe('fancy forms', function () {
expect($scope.person.errorCount()).to.be(0);
sinon.assert.notCalled(onSubmit);
$el.findTestSubject('submit').click();
$el.find(testSubjSelector('submit')).click();
expect($scope.person.errorCount()).to.be(0);
sinon.assert.calledOnce(onSubmit);
});
@ -114,7 +115,7 @@ describe('fancy forms', function () {
it('new fields are no longer soft after blur', function () {
const { $scope, $el } = setup({ name: '' });
expect($scope.person.softErrorCount()).to.be(0);
$el.findTestSubject('name').blur();
$el.find(testSubjSelector('name')).blur();
expect($scope.person.softErrorCount()).to.be(1);
});
@ -139,7 +140,7 @@ describe('fancy forms', function () {
expect($scope.person.errorCount()).to.be(2);
expect($scope.person.softErrorCount()).to.be(0);
$el.findTestSubject('taskDesc').first().blur();
$el.find(testSubjSelector('taskDesc')).first().blur();
expect($scope.person.errorCount()).to.be(2);
expect($scope.person.softErrorCount()).to.be(1);
@ -177,7 +178,7 @@ describe('fancy forms', function () {
expect(form.softErrorCount()).to.be(0);
// blurs only count locally
$task.findTestSubject('taskDesc').blur();
$task.find(testSubjSelector('taskDesc')).blur();
expect(form.softErrorCount()).to.be(1);
// but parent form see them

View file

@ -1,88 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import expect from '@kbn/expect';
import $ from 'jquery';
function $make(subject) {
return $('<div>').attr('data-test-subj', subject);
}
describe('jQuery.findTestSubject', function () {
it('finds all of the element with a subject', function () {
const $container = $('<div>');
const $match = $make('subject').appendTo($container);
const $noMatch = $make('notSubject').appendTo($container);
const $found = $container.findTestSubject('subject');
expect($found.is($match)).to.be(true);
expect($found.is($noMatch)).to.be(false);
});
it('finds multiple elements with a subject', function () {
const $container = $('<div>');
const $match = $make('subject').appendTo($container);
const $otherMatch = $make('subject').appendTo($container);
const $found = $container.findTestSubject('subject');
expect($found.filter($match).length).to.be(1);
expect($found.filter($otherMatch).length).to.be(1);
});
it('finds all of the elements with either subject', function () {
const $container = $('<div>');
const $match1 = $make('subject').appendTo($container);
const $match2 = $make('alsoSubject').appendTo($container);
const $noMatch = $make('notSubject').appendTo($container);
const $found = $container.findTestSubject('subject', 'alsoSubject');
expect($found.filter($match1).length).to.be(1);
expect($found.filter($match2).length).to.be(1);
expect($found.filter($noMatch).length).to.be(0);
});
it('finds all of the elements with a descendant selector', function () {
const $container = $('<div>');
const $parent = $make('foo name').appendTo($container);
const $bar = $make('bar othername').appendTo($parent);
const $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 () {
const $container = $('<div>');
const $bar = $make('button bar').appendTo($container);
const $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);
});
});

View file

@ -1,77 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import testSubjSelector from '@kbn/test-subj-selector';
// eslint-disable-next-line @kbn/eslint/no-default-export
export default function bindToJquery($) {
/**
* Find elements with the `data-test-subj` attribute by the terms in that attribute.
*
* ```js
* // this
* let $button = $('[data-test-subj~="saveButton"]');
*
* // becomes this
* let $button = $.findTestSubject('saveButton');
* ```
*
* Supports multiple subjects
* ```js
* // find any saveButton or cancelButton
* let $buttons = $.findTestSubject('saveButton', 'cancelButton');
* ```
*
* Supports subject "selectors"
* ```js
* // find any saveButton inside a savedObjectForm
* let $button = $.findTestSubject('savedObjectForm saveButton');
* ```
*
* Supports selecting compound subjects
* ```js
* // find any smallButton that is also a saveButton inside a savedObjectForm
* let $input = $.findTestSubject('savedObjectForm smallButton&saveButton');
* ```
*
* @return {jQueryCollection}
*/
$.findTestSubject = function () {
return findTestSubject.apply($(document.body), arguments);
};
/**
* Just like $.findTestSubject, except only finds elements within another element.
* @return {jQueryCollection}
*/
$.fn.findTestSubject = findTestSubject;
function findTestSubject(...subjectSelectors) {
let $els = $();
const $context = this;
subjectSelectors.forEach(function (selector) {
$els = $els.add($context.find(testSubjSelector(selector)));
});
return $els;
}
}

View file

@ -18,6 +18,7 @@
*/
import expect from '@kbn/expect';
import testSubjSelector from '@kbn/test-subj-selector';
import ngMock from 'ng_mock';
import sinon from 'sinon';
import $ from 'jquery';
@ -43,7 +44,7 @@ describe('ui/modals/confirm_modal_promise', function () {
afterEach(function () {
$rootScope.$digest();
$.findTestSubject('confirmModalConfirmButton').click();
$(testSubjSelector('confirmModalConfirmButton')).click();
});
describe('before timeout completes', function () {
@ -58,7 +59,7 @@ describe('ui/modals/confirm_modal_promise', function () {
describe('after timeout completes', function () {
it('confirmation dialogue is loaded to dom with message', function () {
$rootScope.$digest();
const confirmModalElement = $.findTestSubject('confirmModal');
const confirmModalElement = $(testSubjSelector('confirmModal'));
expect(confirmModalElement).to.not.be(undefined);
const htmlString = confirmModalElement[0].innerHTML;
@ -73,7 +74,7 @@ describe('ui/modals/confirm_modal_promise', function () {
promise.then(confirmCallback, cancelCallback);
$rootScope.$digest();
const confirmButton = $.findTestSubject('confirmModalConfirmButton');
const confirmButton = $(testSubjSelector('confirmModalConfirmButton'));
confirmButton.click();
expect(confirmCallback.called).to.be(true);
@ -88,7 +89,7 @@ describe('ui/modals/confirm_modal_promise', function () {
promise.then(confirmCallback, cancelCallback);
$rootScope.$digest();
const noButton = $.findTestSubject('confirmModalCancelButton');
const noButton = $(testSubjSelector('confirmModalCancelButton'));
noButton.click();
expect(cancelCallback.called).to.be(true);

View file

@ -18,8 +18,6 @@
*/
// chrome expects to be loaded first, let it get its way
import $ from 'jquery';
import bindJqueryToFindTestSubject from 'ui/jquery/find_test_subject';
import chrome from '../chrome';
import { parse as parseUrl } from 'url';
@ -32,8 +30,6 @@ import './test_harness.css';
import 'ng_mock';
import { setupTestSharding } from './test_sharding';
bindJqueryToFindTestSubject($);
const { query } = parseUrl(window.location.href, true);
if (query && query.mocha) {
try {