Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

reactjs - simulate change not working not have answer Enzyme

I am trying to perform unit test for my react app The test is to check whether upperCase works or not. I have gone through several references but can't seem to work out the problem are giving erroneous output.

test.js

  describe("handles form input correctly", () => {
    beforeEach(() => {
      cc = mount(<CurrencyConverter />);
      findRate = cc.find(".find-rate");
      currSrc = cc.find(".currency-source");
    });
    afterEach(() => jest.resetAllMocks());

    test('"currency-source" should uppercase alphabetical input', () => {
      currSrc.simulate("change", { target: { value: "abc" } });
      cc.update()
      expect(currSrc.instance().value).toEqual("ABC");
      currSrc.simulate("change", { target: { value: "" } });
    });

app.js

      const [source, setSource] = useState("");
      const [solution, setSolution] = useState("");
    
      const validateCurrency = (value,source) => {
        const letters = /^[A-Za-z]{3,}$/;
        if (value.match(letters)) {
          source ?  setSource(value.substring(0, 3)) :
          setDestination(value.substring(0, 3))
        } else {
          setSolution("Please complete each field");
        }
      };

 return (
      <>
         <form onSubmit={handleSubmit}>
        <p>Source Symbol</p>
        <input
          name="source"
          type="text"
          onkeyup="this.value = this.value.toUpperCase()" 
          maxlength="3"
          className="currency-source"
          onChange={(e) => validateCurrency(e.target.value.toUpperCase(),true)}
        />

        <button type="submit">
          Find Rate
        </button>
        <button  type="reset">
          Reset
        </button>
        <div>{solution}</div>
      </form>
      </>
    );
}
export default CurrencyConverte

i have answer

"currency-dest" should uppercase alphabetical input Test Failed Failure

Error: expect(received).toEqual(expected) // deep equality

Expected: "ABC" Received: ""

question from:https://stackoverflow.com/questions/66065265/simulate-change-not-working-not-have-answer-enzyme

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...