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
2 Answers
Reset to default 7The 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:
Transform the stateless ponent into a class with public methods. Yeah, this kinds beats the purpose. But it is still an option.
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
版权声明:本文标题:javascript - unit test stateless component method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741967299a2407625.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论