import React from 'react'
import test from 'ava'
import { mount } from 'enzyme'
import expect from 'expect'

import withSize from '../src/withSize'

test('append withSize after mounting', () => {
  const Container = withSize()('div')
  const wrapper = mount(<Container />)

  expect(wrapper.find('div').props()).toIncludeKey('DOMSize')
})


test('update offsetToRoot when the window is resized', () => {
  const cwrpSpy = expect.createSpy()

  class FatCat extends React.Component {
    componentWillReceiveProps = cwrpSpy
    render = () => <div style={{ width: this.props.width }} />
  }

  const Container = withSize()(FatCat)

  const wrapper = mount(<Container />)

  // invoke in cdm
  expect(cwrpSpy.calls.length).toEqual(1)

  wrapper.setProps({ width: 100 })
  wrapper.setProps({ width: 200 })

  expect(cwrpSpy.calls.length).toEqual(3)

  wrapper.unmount()

  wrapper.setProps({ width: 300 })

  expect(cwrpSpy.calls.length).toEqual(3)
})

@@ -155,20 +155,19 @@
 'append 
-with
+DOM
 Size aft
@@ -343,54 +343,72 @@
 st('
-update offsetToRoot when the window is resized
+invoke the cancel function of the provided throttle when unmount
 ', (
@@ -423,19 +423,21 @@
  const c
-wrp
+ancel
 Spy = ex
@@ -460,149 +460,145 @@
 %0A%0A  
-class FatCat extends React.Component %7B%0A    componentWillReceiveProps = cwrpSpy%0A    render = () =%3E %3Cdiv style=%7B%7B width: this.props.width %7D%7D /%3E
+/* eslint no-param-reassign:%5B%22error%22, %7B %22props%22: false %7D%5D */%0A  const fakeThrottle = func =%3E %7B%0A    func.cancel = cancelSpy%0A    return func
 %0A  %7D
@@ -632,18 +632,28 @@
 ize(
-)(FatCat)%0A
+fakeThrottle)('div')
 %0A  c
@@ -695,279 +695,64 @@
 %0A%0A  
-// invoke in cdm%0A  expect(cwrpSpy.calls.length).toEqual(1)%0A%0A  wrapper.setProps(%7B width: 100 %7D)%0A  wrapper.setProps(%7B width: 200 %7D)%0A%0A  expect(cwrpSpy.calls.length).toEqual(3)%0A%0A  wrapper.unmount()%0A%0A  wrapper.setProps(%7B width: 300 %7D)%0A%0A  expect(cwrpSpy.calls.length).toEqual(3
+wrapper.unmount()%0A%0A  expect(cancelSpy).toHaveBeenCalled(
 )%0A%7D)

Modify withSize test