简体   繁体   中英

Value set on style.display in unit test is not on the element in implementation

I understand this is a simplified example, but how come in the test below, the if branch is never executed?

displayValue is always being set to inline , even though in my test I'm setting a value ..

it('should store the display value in an attribute', function() {
  var myElement = document.createElement('myElement');
  myElement.style.display = 'hidden';
  myObject.doSomething(myElement);
  expect(myElement.getAttribute('original-display')).to.equal('hidden');
});

The implementation is like this ..

function doSomething(element) {
  var displayValue = element.style.display !== '' ? element.style.display : 'inline';
  console.log(displayValue); // always prints `inline`
  element.setAttribute('original-display', displayValue);
  element.style.display = 'none';
}

The display attribute of an element will never be an empty string because it always has a default. Also, hidden is an invalid value for the display attribute, so that never gets set. hidden is a value for the visibility attribute. So an element can be display: inline but also be visibility: hidden , for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM