简体   繁体   中英

How to test class methods using jest?

I have the following react component.

import React, { Component } from 'react'

export class Dummy extends Component {
    public renderCode(){
        return 'xm3'
    }
    render() {
        return (
            <div>
                {this.renderCode()}
            </div>
        )
    }
}

Using only jest, without enzyme, how do I test the returned value from Dummy.renderCode() ?

import React, { Component } from 'react'

const functionToTest = () => {
        return 'xm3'
}

class Dummy extends Component {

    render() {
        return (
            <div>
                {this.functionToTest()}
            </div>
        )
    }
}

public export default Dummy;

One way is to extract the function outside the class then test it directly by export it. Very simple:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM