diff --git a/frontend/js/tests/user/stats/UserArtistMap.test.tsx b/frontend/js/tests/user/stats/UserArtistMap.test.tsx index d0e46cf665..a6f20dd838 100644 --- a/frontend/js/tests/user/stats/UserArtistMap.test.tsx +++ b/frontend/js/tests/user/stats/UserArtistMap.test.tsx @@ -40,6 +40,20 @@ const reactQueryWrapper = ({ children }: any) => ( {children} ); +const setQueryData = ( + userName: string | undefined, + range: string, + data: any +) => { + const queryKey = ["user-stats-map", range, userName]; + queryClient.ensureQueryData({ + queryKey, + queryFn: () => { + return data; + }, + }); +}; + describe.each([ ["User Stats", userProps], ["Sitewide Stats", sitewideProps], @@ -66,16 +80,10 @@ describe.each([ }); it("renders correctly", async () => { - const queryKey = ["user-stats-map", "week", props.user?.name]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: userArtistMapResponse, - hasError: false, - errorMessage: "", - }; - }, + setQueryData(props.user?.name, "week", { + data: userArtistMapResponse, + hasError: false, + errorMessage: "", }); renderWithProviders( , @@ -98,16 +106,10 @@ describe.each([ // eslint-disable-next-line jest/no-disabled-tests xit("displays error message when API call fails", async () => { const errorMessage = "API Error"; - const queryKey = ["user-stats-map", "week", props.user?.name]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: {}, - hasError: true, - errorMessage, - }; - }, + setQueryData(props.user?.name, "week", { + data: {}, + hasError: true, + errorMessage, }); renderWithProviders( @@ -128,17 +130,12 @@ describe.each([ }); it("renders choropleth with processed data", async () => { - const queryKey = ["user-stats-map", "week", props.user?.name]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: userArtistMapResponse, - hasError: false, - errorMessage: "", - }; - }, + setQueryData(props.user?.name, "week", { + data: userArtistMapResponse, + hasError: false, + errorMessage: "", }); + renderWithProviders( , {}, diff --git a/frontend/js/tests/user/stats/UserDailyActivity.test.tsx b/frontend/js/tests/user/stats/UserDailyActivity.test.tsx index aef8c2dda5..30ad38daba 100644 --- a/frontend/js/tests/user/stats/UserDailyActivity.test.tsx +++ b/frontend/js/tests/user/stats/UserDailyActivity.test.tsx @@ -31,7 +31,7 @@ const queryClient = new QueryClient({ }, }); const queryKey = ["userDailyActivity", props.user.name, "week"]; -// preload data + const reactQueryWrapper = ({ children }: any) => ( {children} ); diff --git a/frontend/js/tests/user/stats/UserListeningActivity.test.tsx b/frontend/js/tests/user/stats/UserListeningActivity.test.tsx index d9618027fc..397220ca52 100644 --- a/frontend/js/tests/user/stats/UserListeningActivity.test.tsx +++ b/frontend/js/tests/user/stats/UserListeningActivity.test.tsx @@ -38,6 +38,20 @@ const reactQueryWrapper = ({ children }: any) => ( {children} ); +const setQueryData = ( + userName: string | undefined, + range: string, + data: any +) => { + const queryKey = ["userListeningActivity", userName, range]; + queryClient.ensureQueryData({ + queryKey, + queryFn: () => { + return data; + }, + }); +}; + const getResponse = (range: string) => { let response; switch (range) { @@ -91,16 +105,10 @@ describe.each([ server.close(); }); it("renders correctly for week", async () => { - const queryKey = ["userListeningActivity", props.user?.name, "week"]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: userListeningActivityResponseWeek, - hasError: false, - errorMessage: "", - }; - }, + setQueryData(props.user?.name, "week", { + data: userListeningActivityResponseWeek, + hasError: false, + errorMessage: "", }); renderWithProviders( @@ -125,16 +133,10 @@ describe.each([ }); it("renders correctly for month", async () => { - const queryKey = ["userListeningActivity", props.user?.name, "month"]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: userListeningActivityResponseMonth, - hasError: false, - errorMessage: "", - }; - }, + setQueryData(props.user?.name, "month", { + data: userListeningActivityResponseMonth, + hasError: false, + errorMessage: "", }); renderWithProviders( @@ -159,16 +161,10 @@ describe.each([ }); it("renders correctly for year", async () => { - const queryKey = ["userListeningActivity", props.user?.name, "year"]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: userListeningActivityResponseYear, - hasError: false, - errorMessage: "", - }; - }, + setQueryData(props.user?.name, "year", { + data: userListeningActivityResponseYear, + hasError: false, + errorMessage: "", }); renderWithProviders( @@ -193,16 +189,10 @@ describe.each([ }); it("renders correctly for all_time", async () => { - const queryKey = ["userListeningActivity", props.user?.name, "all_time"]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: userListeningActivityResponseAllTime, - hasError: false, - errorMessage: "", - }; - }, + setQueryData(props.user?.name, "all_time", { + data: userListeningActivityResponseAllTime, + hasError: false, + errorMessage: "", }); renderWithProviders( @@ -228,18 +218,11 @@ describe.each([ it("displays error message when API call fails", async () => { const errorMessage = "API Error"; - const queryKey = ["userListeningActivity", props.user?.name, "week"]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: {}, - hasError: true, - errorMessage, - }; - }, + setQueryData(props.user?.name, "week", { + data: {}, + hasError: true, + errorMessage, }); - renderWithProviders( diff --git a/frontend/js/tests/user/stats/UserTopEntity.test.tsx b/frontend/js/tests/user/stats/UserTopEntity.test.tsx index e43d578a37..22bd9a0579 100644 --- a/frontend/js/tests/user/stats/UserTopEntity.test.tsx +++ b/frontend/js/tests/user/stats/UserTopEntity.test.tsx @@ -40,6 +40,21 @@ const reactQueryWrapper = ({ children }: any) => ( {children} ); +const setQueryData = ( + userName: string | undefined, + entity: Entity, + range: string, + data: any +) => { + const queryKey = ["user-top-entity", entity, range, userName]; + queryClient.ensureQueryData({ + queryKey, + queryFn: () => { + return data; + }, + }); +}; + describe.each([ ["User Stats", userProps], ["Sitewide Stats", sitewideProps], @@ -83,17 +98,10 @@ describe.each([ queryClient.clear(); }); it("renders correctly for artist", async () => { - const queryKey = ["user-top-entity", "artist", "week", props.user?.name]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: userArtists, - loading: false, - hasError: false, - errorMessage: "", - }; - }, + setQueryData(props.user?.name, "artist", "week", { + data: userArtists, + hasError: false, + errorMessage: "", }); renderWithProviders( , @@ -113,17 +121,10 @@ describe.each([ }); it("renders correctly for release", async () => { - const queryKey = ["user-top-entity", "release", "week", props.user?.name]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: userReleases, - loading: false, - hasError: false, - errorMessage: "", - }; - }, + setQueryData(props.user?.name, "release", "week", { + data: userReleases, + hasError: false, + errorMessage: "", }); renderWithProviders( , @@ -143,22 +144,10 @@ describe.each([ }); it("renders correctly for release group", async () => { - const queryKey = [ - "user-top-entity", - "release-group", - "week", - props.user?.name, - ]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: userReleaseGroups, - loading: false, - hasError: false, - errorMessage: "", - }; - }, + setQueryData(props.user?.name, "release-group", "week", { + data: userReleaseGroups, + hasError: false, + errorMessage: "", }); renderWithProviders( { - const queryKey = [ - "user-top-entity", - "recording", - "week", - props.user?.name, - ]; - queryClient.ensureQueryData({ - queryKey, - queryFn: () => { - return { - data: userRecordings, - loading: false, - hasError: false, - errorMessage: "", - }; - }, + setQueryData(props.user?.name, "recording", "week", { + data: userRecordings, + hasError: false, + errorMessage: "", }); renderWithProviders( , @@ -220,17 +197,10 @@ describe.each([ it("displays error message when API call fails", async () => { const errorMessage = "API Error"; - - queryClient.ensureQueryData({ - queryKey: ["user-top-entity", "artist", "week", props.user?.name], - queryFn: () => { - return { - data: {}, - hasError: true, - errorMessage, - loading: false, - }; - }, + setQueryData(props.user?.name, "artist", "week", { + data: {}, + hasError: true, + errorMessage, }); renderWithProviders(