2
votes

I am new to the Jest testing framework and so I wan't too mock a function that returns a Promise to test my click event button. I am receiving an error that says my method has not been called but I have it defined below as a jest.fn() and expect it to be called once. Any help in solving this or some clarity on why my code is wrong.

const props = {
        activitiesStore: {
          isFirst:false,
          isLast:false,
          isLaunching:false,
          isLoaded:false,
          currentActivityIndex:115,
          markLessonFinished:() => {

          },
          setContent:{
            description: "In the CompTIA A+ training course, you will learn the fundamentals of computer technology, repair, basic networking, installation and configuration of PCs, laptops and related hardware, as well as configurations for the mobile OS's Android and Apple iOS."
          },
          activityRespone:{
            description:"**TCP/IP Configuration Default Gateway** For this lesson, we take a closer more intimate look at the Default Gateway and how that process works. This is where the computer goes to get out of a network…",
            lesson_id:116,
            no_iframe:false,
            slug:"tcpip-configurations-default-gateway",
            type:"Video Activity",
            url:"https://player.vimeo.com/video/113658225"
          },
          activities:[{
            completed:true,
            content_description_id:2,
            course_id:178,
            moduleId:4649,
            thumbnail:"https://dev.cybrary.it/wp-content/themes/cybrary/teamsv2/img/certification_labs.png",
            title:"About Anthony Harris"
          }],
          advertisements:[{
            href:"https://www.cybrary.it",
            image:"https://www.cybrary.it/wp-content/uploads/2018/12/VideoPage-XMAS40.png",
            objectID:"787592",
            section:"immersive",
            weight:0
          }],
          currentActivity:{
            completed:true,
            content_description_id:117,
            course_id:178,
            moduleId:4665,
            thumbnail:"https://dev.cybrary.it/wp-content/themes/cybrary/teamsv2/img/certification_labs.png",
            title:"TCP/IP Configurations Default Gateway",
            currentActivityIndex:115
          },
          enrollment:{
            estimatedCompletionDate: "Aug 21, 2019",
            estimatedTimeRemaining: "31 hours 38 minutes",
            id: 37,
            isLate: true,
            overallProgress: 33,
            programDeadline: "May 17, 2018",
            status: "Started",
            thumbnail: "/images/assignment_16.jpg",
            title: "testing admin curry",
            totalCompletedCurriculumItems: 0,
            totalCurriculumItems: 1,
            totalStartedCurriculumItems: 1,
            type: "Assignment",
            settings:[{

            }],
            progressData:[{
              progress: 100,
              title: "Session 1",
              duration:null,
              checkpointDate: "Sep 30, 2018",
              content:[{
                content:{
                  duration: " 1 minute",
                  status: "Completed",
                  title: "Instructor Background - About Anthony Harris"
                },
                icon:"play"
              }],
              headerColumns:[{
                title: "Progress",
                value: "100%"
              }],
              headerRow:[{
                content:"LAUNCH"
              }]
            }],
            moduleActivities:[[{
              duration: 110,
              id: 2,
              progress: 100,
              title: "About Anthony Harris",
              _course_id: 178,
              _formatted_title: "Instructor Background - About Anthony Harris",
              _is_completed: true
            }]],
            enrollmentTableData:[{
              duration: "44 hours 16 minutes",
              estimatedTimeRemaining: "31 hours 38 minutes",
              progress: 33,
              remainingDuration: "31 hours 38 minutes",
              status: "In Progress",
              thumbnail: "https://dev.cybrary.it/wp-content/themes/cybrary/teamsv2/img/certification_labs.png",
              title: "Comptia A+ (Archive)",
              type: "Course",
              curriculumItem:{
                legacy_id: 167,
                video_intro_url: "https://player.vimeo.com/video/208046939",
                id:178,
                content_description_id:4648,
                content_description:{
                  courseId: 178,
                  duration_seconds: 154800,
                  id: 4648,
                  is_hidden: 1,
                  content_type:{
                    human_readable_name: "Course",
                    id: 1,
                    name: "course",
                    thumbnail_url: "https://dev.cybrary.it/wp-content/themes/cybrary/teamsv2/img/certification_labs.png"
                  }
                }
              },
              contentDescription:{
                courseId: 178,
                duration_seconds: 154800,
                id: 4648,
                is_hidden: 1,
                content_type:{
                  human_readable_name: "Course",
                  id: 1,
                  name: "course",
                  thumbnail_url: "https://dev.cybrary.it/wp-content/themes/cybrary/teamsv2/img/certification_labs.png"
                }
              }
            }],
            curriculumItems:[{
              duration: "44 hours 16 minutes",
              estimatedTimeRemaining: "31 hours 38 minutes",
              progress: 33,
              remainingDuration: "31 hours 38 minutes",
              status: "In Progress",
              thumbnail: "https://dev.cybrary.it/wp-content/themes/cybrary/teamsv2/img/certification_labs.png",
              title: "Comptia A+ (Archive)",
              type: "Course",
              curriculumItem:{
                legacy_id: 167,
                video_intro_url: "https://player.vimeo.com/video/208046939",
                id:178,
                content_description_id:4648,
                content_description:{
                  courseId: 178,
                  duration_seconds: 154800,
                  id: 4648,
                  is_hidden: 1,
                  content_type:{
                    human_readable_name: "Course",
                    id: 1,
                    name: "course",
                    thumbnail_url: "https://dev.cybrary.it/wp-content/themes/cybrary/teamsv2/img/certification_labs.png"
                  }
                }
              },
              contentDescription:{
                courseId: 178,
                duration_seconds: 154800,
                id: 4648,
                is_hidden: 1,
                content_type:{
                  human_readable_name: "Course",
                  id: 1,
                  name: "course",
                  thumbnail_url: "https://dev.cybrary.it/wp-content/themes/cybrary/teamsv2/img/certification_labs.png"
                }
              }
            }],
            loading:{

            }
          }
        }
markLessonFinished() {
    const currentActivity = this.currentActivity;
    return new Promise(resolve => {
      if (
        this.activityResponse &&
        this.activityResponse.lesson_id &&
        currentActivity.course_id
      ) {
        Agents.catalog
          .finishLesson(
            this.activityResponse.lesson_id,
            currentActivity.course_id
          )
          .then(() => {
            resolve(true);
          });
      }
      resolve(false);
    });
  }
it("should click next button after lesson finished", async () => {

        const { getByText } = render(<ImmersivePage {...props}/>)

        // checks if 'Next' button is rendered
        expect(getByText("Next")).toBeTruthy()

        const markLessonFinished = jest.fn()

        await waitForElement(() => {
            expect(markLessonFinished).toHaveBeenCalledTimes(1)
        }).then(() => {
            fireEvent.click(getByText("Next"))
        })
    })
1

1 Answers

-1
votes

You have to pass your mocked method to the component you are rendering:

const markLessonFinished = jest.fn().mockResolvedValue(true)
const { getByText } = render(<ImmersivePage {...props} markLessonFinished={markLessonFinished} />)

At this point, I'm not sure how your component works but I'm assuming first you click the button and then the lesson is marked as finished. In this case, you would do:

fireEvent.click(getByLabelText('Next'))
await wait(()=> expect(markLessonFinished).toHaveBeenCalledTimes(1))