1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jsで要素の高さとか計算する処理をテスタブルに書くとどうなるだろうか

1
Last updated at Posted at 2018-11-01

jsで要素の高さを計算する処理を純粋関数型っぽく書きたいとき

普通に書くならこう

const calculateHogeSize = () => {
  return $('.js-hoge').innerHeight() - ($('.js-foo').innerHeight() + $('.js-fuga').innerHeight() + parseInt($('.js-bar').css('margin-top')) + 50);
};

$(window).on('resize', () => {
   const hogeSize = calculateHogeSize();
   $('.js-target').css({height: hogeSize});
});

テスタブルに書くならこう?

const calculateHogeSize = (hogeSize, fooSize, fugaSize, barMargin) => {
  return hogeSize - (fooSize + fugaSize + barMargin + 50);
};

$(window).on('resize', () => {
  const hogeSize = $('.js-hoge').innerHeight();
  const fooSize = $('.js-foo').innerHeight();
  const fugaSize = $('.js-fuga').innerHeight();
  const barMargin = parseInt($('.js-bar').css('margin-top'));

  const hogeSize = calculateHogeSize(hogeSize, fooSize, fugaSize, barMargin);
  $('.js-target').css({height: hogeSize});
});

意図したこと

calculateHogeSize関数をテストしやすい

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?