Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
How to test window functions within a react component?

Are there any test to this react functional component in Jest?

The problem is that window print open an iframe when I start this component with modalIsVisible active.

type SixLabelReportType = {
  labels: LabelProps[];
  modalIsVisible: boolean;
  setModalIsVisible: Dispatch<SetStateAction<boolean>>;
};
const SixLabelReport: React.FC<SixLabelReportType> = (
  params: SixLabelReportType
) => {
  const { labels, modalIsVisible, setModalIsVisible } = params;

  useEffect(() => {
    if (modalIsVisible) {
      window.print();
      setModalIsVisible(false);
    }
  }, [modalIsVisible]);

  return (
    <S.Modal show={modalIsVisible} data-testid='six-label-report-modal'>
      <S.Container data-testid='six-label-report-container'>
        {labels.map((label, idx) => {
          const { deliveryData, recipientData, senderData } = label;
          return (
            <Label
              key={idx}
              deliveryData={deliveryData}
              recipientData={recipientData}
              senderData={senderData}
              data-testid='six-label-report-label'
            />
          );
        })}
      </S.Container>
    </S.Modal>
  );
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just spy on window.open

const spy = jest.spyOn(window, 'open');
expect(spy).toHaveBeenCalled();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!