React Native Expo Jest Tests: No “.then()” statements are entered in loading in data tests

I am writing feature specs for my application. The application is a music player app, and the user can edit their playlists. The feature in question I am testing edits a track, or all the tracks, in the playlist based on the argument passed in…


  const [playlist, setPlaylist] = useState<Track[]>(Array(10).fill(undefined));

  const [editMode, setEditMode] = useState<[boolean, number | null]>([false, null]);

  const { trackNumToEdit } = useLocalSearchParams();

...

  const useStoredPlaylist = async (storedPlaylist: Track[]) => {
    // store the tracks from storage to playlist item
    const newPlaylist = Array(10).fill(undefined)

    storedPlaylist.map((track) => {
      newPlaylist[track.questionNo] = track
    })

    setPlaylist(newPlaylist);
  }

...

  // If the user has come from clicking to "edit" a 
  // song from their album, load in their playlist
  // and enter "edit" mode
  useFocusEffect(
    useCallback(() => {
      if (trackNumToEdit) {
        retrievePlaylistAnswers()
          .then((res) => {
            setEditMode([true, null]);

            if (trackNumToEdit != "all") {
              // Must cast to number as converting from route param
              const trackNum = Number(trackNumToEdit);
              const editingTrack = res[trackNum];
              
              setEditMode([true, editingTrack]);
              setFocusedQuestion(editingTrack.questionNo);
            }
            useStoredPlaylist(res);
          })
          .catch((error) => {
            console.log("Failed to load track data for editing", error);
            return Promise.reject(error);
          })
      }
    }, [useStoredPlaylist])
  );

...

return (
...
)

)



…Here are the corresponding specs for this feature…


...

const mockSpecificTrackParams = { trackNumToEdit: "0" };
const mockAllTrackParams = { trackNumToEdit: "all" };

...

  describe("Editing a playlist", () => {
    describe("Editing a specifc track", () => {
      beforeEach(() => {
        (useLocalSearchParams as jest.Mock).mockReturnValue(mockSpecificTrackParams);
      });

      it("loads in the stored playlist and sets the page to edit the specified track", async () => {
        const firstMockTrack: Track = {
          id: "1",
          name: "ABC",
          uri: "XYZ",
          imageUrl: "WEB",
          artists: "John",
          questionNo: 0,
          preview: "URL"
        }
        const secondMockTrack: Track = {
          id: "2",
          name: "DEF",
          uri: "XYZ",
          imageUrl: "WEB",
          artists: "Joe",
          questionNo: 1,
          preview: "URL"
        }
        
        const mockResponse = [
          firstMockTrack,
          secondMockTrack
        ];
  
        const mockRetrievePlaylistAnswers = retrievePlaylistAnswers as jest.Mock;
        mockRetrievePlaylistAnswers.mockImplementation(() => Promise.resolve(mockResponse));

        render(<SetPlaylist />);

        await waitFor(() => {
          // Expect screen to be configured to editing a specific track
        });
      })
    })

    describe("Editing all tracks", () => {
      beforeEach(() => {
        (useLocalSearchParams as jest.Mock).mockReturnValue(mockAllTrackParams);
      });

      it("loads in the stored playlist and sets the page to edit from question 1", async () => {
        const firstMockTrack: Track = {
          id: "1",
          name: "ABC",
          uri: "XYZ",
          imageUrl: "WEB",
          artists: "John",
          questionNo: 0,
          preview: "URL"
        }
        const secondMockTrack: Track = {
          id: "2",
          name: "DEF",
          uri: "XYZ",
          imageUrl: "WEB",
          artists: "Joe",
          questionNo: 1,
          preview: "URL"
        }
        
        const mockResponse = [
          firstMockTrack,
          secondMockTrack
        ];
  
        const mockRetrievePlaylistAnswers = retrievePlaylistAnswers as jest.Mock;
        mockRetrievePlaylistAnswers.mockImplementation(() => Promise.resolve(mockResponse));

        render(<SetPlaylist />);

        await waitFor(() => {
          // Expect screen to be configured to editing a specific track
        });
      })
    })
  })

The problem is that the screen matches what it should look like when the user has no stored playlist, and is not editing. I debugged this and found that, despite mocking the stored playlist answers, that the playlist’s state does not change and useStoredPlaylist() is not called. When I debug for the case “loads in the stored playlist and sets the page to edit the specified track”, what happens is the body of “then(res)” is skipped, so the state of playlist remains unchanged. No breakpoints in useStoredPlaylist are hit either…

What is also strange is that the spec keeps “running” after the “await” test is done, and it seems that in these debug runs, playlist is changed and useStoredPlaylist is hit…

I am baffled by what is happening. Why is some of the “rendered” code being run after the spec body is complete? Why is the “then(res)” statement not being entered on the first instance? I am unsure if this is a weird React Native problem or a Jest problem – hence the generalised title!

I’d appreciate any help with this or even some insight! Thank you so much.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật