admin管理员组

文章数量:1314556

I have this stateless ponent of react

const Clock = () => {
    const formatSeconds = (totalSeconds) => {
        const seconds = totalSeconds % 60,
        minutes = Math.floor(totalSeconds / 60)

        return `${minutes}:${seconds}`
    }
    return(
        <div></div>
    )
}

export default Clock

How to test formatSeconds method?

I wrote this, which the test has failed.

import React from 'react'
import ReactDOM from 'react-dom'
import expect from 'expect'
import TestUtils from 'react-addons-test-utils'

import Clock from '../ponents/Clock'

describe('Clock', () => {
    it('should exist', () => {
        expect(Clock).toExist()
    })

    describe('formatSeconds', () => {
        it('should format seconds', () => {
            const Clock = TestUtils.renderIntoDocument(<Clock />)
            const seconds = 615
            const expected = '10:15'
            const actual = Clock.formatSeconds(seconds)

            expect(actual).toBe(expected)
        })
    })
})

The first test passed but maybe there's some problem doing Clock.formatSeconds.

I have this stateless ponent of react

const Clock = () => {
    const formatSeconds = (totalSeconds) => {
        const seconds = totalSeconds % 60,
        minutes = Math.floor(totalSeconds / 60)

        return `${minutes}:${seconds}`
    }
    return(
        <div></div>
    )
}

export default Clock

How to test formatSeconds method?

I wrote this, which the test has failed.

import React from 'react'
import ReactDOM from 'react-dom'
import expect from 'expect'
import TestUtils from 'react-addons-test-utils'

import Clock from '../ponents/Clock'

describe('Clock', () => {
    it('should exist', () => {
        expect(Clock).toExist()
    })

    describe('formatSeconds', () => {
        it('should format seconds', () => {
            const Clock = TestUtils.renderIntoDocument(<Clock />)
            const seconds = 615
            const expected = '10:15'
            const actual = Clock.formatSeconds(seconds)

            expect(actual).toBe(expected)
        })
    })
})

The first test passed but maybe there's some problem doing Clock.formatSeconds.

Share Improve this question asked Jun 27, 2017 at 14:42 Madeline RiesMadeline Ries 6291 gold badge9 silver badges19 bronze badges 1
  • I don't understand your ponent. Why do you have a function inside a function which never gets used or exposed? – evolutionxbox Commented Jun 27, 2017 at 14:45
Add a ment  | 

2 Answers 2

Reset to default 7

The ponent Clock is a function, which is invoked when the ponent is rendered. The method formatSeconds is defined inside the the Clock closure, and it's not a property of Clock, so you can't reach it from outside.

In addition, you are recreating the formatSeconds method on every render, and since the method doesn't actually use any prop in the scope, it's a bit wasteful. So, you can take the method out, and export it. You can also move it to another utility file, and import it, since it's not an integral part of Clock, and you might want to reuse it other places.

export const formatSeconds = (totalSeconds) => {
    const seconds = totalSeconds % 60,
    minutes = Math.floor(totalSeconds / 60)

    return `${minutes}:${seconds}`
}

const Clock = () => {
    return(
        <div></div>
    )
}

export default Clock

Now testing is easy as well:

import React from 'react'
import ReactDOM from 'react-dom'
import expect from 'expect'
import TestUtils from 'react-addons-test-utils'

import Clock, { formatSeconds } from '../ponents/Clock' // import formatSeconds as well

describe('Clock', () => {
    it('should exist', () => {
        expect(Clock).toExist()
    })

    describe('formatSeconds', () => {
        it('should format seconds', () => {
            const seconds = 615
            const expected = '10:15'
            const actual = formatSeconds(seconds) // use it by itself

            expect(actual).toBe(expected)
        })
    })
})

A little late to the party but I have two approaches I would like to share with you. Right out of the box you cannot test private methods. Regardless if it is a stateless ponent or just a simple function with some private internals. I have for one use the following approaches:

  1. Transform the stateless ponent into a class with public methods. Yeah, this kinds beats the purpose. But it is still an option.

  2. Keep the stateless ponent and declare the private methods as "static".

const Button = onClick => {
   return <button onClick={Button.handleClick}> My Button </button>;
}

Button.handleClick = e => e.stopPropagation();

ReactDOM.render(<Button />, document.getElementById('root'));
<div id="root"></div>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>

本文标签: javascriptunit test stateless component methodStack Overflow