Я думаю, что первая проблема заключается в том, что с длительностью (в данном случае «быстрой»), toggle and show functions don't just change the visibility:
When a duration, a plain object, or a single "complete" function is provided, .toggle() becomes an animation method. The .toggle() method animates the width, height, and opacity of the matched elements simultaneously. When these properties reach 0 after a hiding animation, the display style property is set to none to ensure that the element no longer affects the layout of the page.
So you actually want to be checking for the display
of the element.
The next issue is that it takes 200ms before jquery sets the display to none
, so we need to account for that, meaning we could write something like this:
it('toggles the field', function(done) {
givenFields(`
kuku переключатель `);var initialDisplay = $ ("# field"). css ('display');$ ( '# Тумблер') нажмите ().// Проверка того, что это не проблема рендеринга jsdom ожидаем (document.hidden) .toEqual (false);var ожидаетсяDisplay = initialDisplay === 'блок'?'none': 'block' setTimeout (function () {Ожидайте ($ ("# поле"). css ('display')). toEqual (Ожидаемый вывод); done ()}, 200)});
Также может быть способ ввести jest timer-mocks into jquery so that you can avoid the 200ms wait. Would be cool but not sure if it's possible.
EDIT:
jQuery.fx.off = true;
This works great, leaving us with:
jest.dontMock('jquery');
jest.dontMock('./toggle.js');
$ = jQuery = require('jquery');
jQuery.fx.off = true;
describe('toggle.js', function() {
var sut = require('./toggle.js');
function givenFields(formFields) {
document.documentElement.innerHTML = `
${formFields}
`;
sut.initialize();
}
it('toggles the field', function() {
givenFields(`
Куку переключатель `);var initialDisplay = $ ("# field"). css ('display');$ ( '# Тумблер') нажмите ().// Проверка того, что это не проблема рендеринга jsdom ожидаем (document.hidden) .toEqual (false);var ожидаетсяDisplay = initialDisplay === 'блок'?'none': 'block' Ожидается ($ ("# field"). css ('display')). toEqual (Ожидаемый вывод);});it ('показывает поле', function () {GivenFields (`Куку show `);$ ( '# Шоу') нажмите ().ожидать ($ ( "# поле") есть ( ": видимые").) toEqual (истина);.});});