<commit_before>/* eslint no-unused-expressions: 0 */
import React from 'react'
import { mount } from 'enzyme'
import { spy } from 'sinon'
import chai, { expect } from 'chai'
import ContainerDimensions from '../src/index'

chai.use(require('chai-enzyme')())
const MyComponent = ({ width, height }) => <span>{width}, {height}</span> // eslint-disable-line

describe('react-container-dimensions', () => {

    it('should throw without children', () => {
        expect(() => mount(<div><ContainerDimensions /></div>))
            .to.throw('Expected children to be one of function or React.Element')
    })

    it('calls componentDidMount', (done) => {
        spy(ContainerDimensions.prototype, 'componentDidMount')
        spy(ContainerDimensions.prototype, 'componentWillUnmount')
        const wrapper = mount(
            <div>
                <ContainerDimensions>
                    <MyComponent />
                </ContainerDimensions>
            </div>
        , { attachTo: document.body })
        expect(wrapper.find('div').length).to.eq(1)
        expect(ContainerDimensions.prototype.componentDidMount.calledOnce).to.be.true
        ContainerDimensions.prototype.componentDidMount.restore()
        setTimeout(() => {
            wrapper.unmount()
            expect(ContainerDimensions.prototype.componentWillUnmount.calledOnce).to.be.true
            ContainerDimensions.prototype.componentWillUnmount.restore()
            done()
        }, 0)
    })

    it('calls onResize on mount', () => {
        spy(ContainerDimensions.prototype, 'onResize')
        mount(
            <div>
                <ContainerDimensions>
                    <MyComponent />
                </ContainerDimensions>
            </div>
        )
        expect(ContainerDimensions.prototype.onResize.calledOnce).to.be.true
        ContainerDimensions.prototype.onResize.restore()
    })

    it('calls onResize when parent has been resized', (done) => {
        spy(ContainerDimensions.prototype, 'onResize')
        const wrapper = mount(
            <div ref="node" id="node" style={{ width: 10 }}>
                <ContainerDimensions>
                    <MyComponent />
                </ContainerDimensions>
            </div>
        , { attachTo: document.body })
        const el = wrapper.render()
        el.css('width', 10)
        setTimeout(() => {
            el.css('width', 100)
            expect(ContainerDimensions.prototype.onResize.calledTwice).to.be.true
            ContainerDimensions.prototype.onResize.restore()
            done()
        }, 0)
    })

    it('should pass width and height as props to its children', () => {
        const wrapper = mount(
            <ContainerDimensions>
                <MyComponent />
            </ContainerDimensions>
        )
        wrapper.setState({
            width: 100,
            height: 200
        })
        expect(wrapper.find(MyComponent)).to.have.length(1)
        expect(wrapper.find(MyComponent)).to.have.prop('width', 100)
        expect(wrapper.find(MyComponent)).to.have.prop('height', 200)
    })

    it('should pass width and height as function arguments', () => {<commit_msg>Added a test for initial rendering<commit_after>/* eslint no-unused-expressions: 0 */
import React from 'react'
import { mount } from 'enzyme'
import { spy } from 'sinon'
import chai, { expect } from 'chai'
import ContainerDimensions from '../src/index'

chai.use(require('chai-enzyme')())
const MyComponent = ({ width, height }) => <span>{width}, {height}</span> // eslint-disable-line

describe('react-container-dimensions', () => {

    it('should throw without children', () => {
        expect(() => mount(<div><ContainerDimensions /></div>))
            .to.throw('Expected children to be one of function or React.Element')
    })

    it('calls componentDidMount', (done) => {
        spy(ContainerDimensions.prototype, 'componentDidMount')
        spy(ContainerDimensions.prototype, 'componentWillUnmount')
        const wrapper = mount(
            <div>
                <ContainerDimensions>
                    <MyComponent />
                </ContainerDimensions>
            </div>
        , { attachTo: document.body })
        expect(wrapper.find('div').length).to.eq(1)
        expect(ContainerDimensions.prototype.componentDidMount.calledOnce).to.be.true
        ContainerDimensions.prototype.componentDidMount.restore()
        setTimeout(() => {
            wrapper.unmount()
            expect(ContainerDimensions.prototype.componentWillUnmount.calledOnce).to.be.true
            ContainerDimensions.prototype.componentWillUnmount.restore()
            done()
        }, 0)
    })

    it('calls onResize on mount', () => {
        spy(ContainerDimensions.prototype, 'onResize')
        mount(
            <div>
                <ContainerDimensions>
                    <MyComponent />
                </ContainerDimensions>
            </div>
        )
        expect(ContainerDimensions.prototype.onResize.calledOnce).to.be.true
        ContainerDimensions.prototype.onResize.restore()
    })

    it('calls onResize when parent has been resized', (done) => {
        spy(ContainerDimensions.prototype, 'onResize')
        const wrapper = mount(
            <div ref="node" id="node" style={{ width: 10 }}>
                <ContainerDimensions>
                    <MyComponent />
                </ContainerDimensions>
            </div>
        , { attachTo: document.body })
        const el = wrapper.render()
        el.css('width', 10)
        setTimeout(() => {
            el.css('width', 100)
            expect(ContainerDimensions.prototype.onResize.calledTwice).to.be.true
            ContainerDimensions.prototype.onResize.restore()
            done()
        }, 0)
    })

    it('should initially pass { width: 0, height: 0 } to its children', () => {
        const wrapper = mount(
            <ContainerDimensions>
                <MyComponent />
            </ContainerDimensions>
        )
        expect(wrapper.find(MyComponent)).to.have.length(1)
        expect(wrapper.find(MyComponent)).to.have.prop('width', 0)
        expect(wrapper.find(MyComponent)).to.have.prop('height', 0)
    })

    it('should pass width and height as props to its children', () => {
        const wrapper = mount(
            <ContainerDimensions>
                <MyComponent />
            </ContainerDimensions>
        )
        wrapper.setState({
            width: 100,
            height: 200
        })
        expect(wrapper.find(MyComponent)).to.have.length(1)
        expect(wrapper.find(MyComponent)).to.have.prop('width', 100)
        expect(wrapper.find(MyComponent)).to.have.prop('height', 200)
    })

    it('should pass width and height as function arguments', () => {
{'fraction_non_alphanumeric': 0.1315711754282339, 'fraction_numerical': 0.007826343768458357, 'mean_word_length': 2.1634750116767867, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2680096', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1788, 'n_words': 348}