From 82968c960c5de1b41c9b7b785c01f6cc74edfedc Mon Sep 17 00:00:00 2001
From: davidcrair <115373655+davidcrair@users.noreply.github.com>
Date: Sat, 13 Apr 2024 15:27:34 -0400
Subject: [PATCH] Add tests for share cassette and share unify
---
__tests__/share_cassette.t.js | 93 +++++++++++++++++++++++++++++++++++
__tests__/share_unify.t.js | 93 +++++++++++++++++++++++++++++++++++
2 files changed, 186 insertions(+)
create mode 100644 __tests__/share_cassette.t.js
create mode 100644 __tests__/share_unify.t.js
diff --git a/__tests__/share_cassette.t.js b/__tests__/share_cassette.t.js
new file mode 100644
index 0000000..17fc285
--- /dev/null
+++ b/__tests__/share_cassette.t.js
@@ -0,0 +1,93 @@
+/* eslint-disable react/jsx-filename-extension */
+
+import React from "react";
+import { render, fireEvent, waitFor, screen } from "@testing-library/react";
+import UserPage from "@/app/user/[slug]/page";
+
+import userData from "./userData.json";
+
+// Mocking the supabase client and its methods
+jest.mock("../src/utils/supabase/client", () => {
+ return jest.fn(() => ({
+ from: jest.fn().mockReturnThis(),
+ select: jest.fn().mockReturnThis(),
+ eq: jest.fn().mockResolvedValue({
+ data: [
+ {
+ spotify_data: userData,
+ },
+ ],
+ error: null,
+ }),
+ }));
+});
+
+jest.mock("@nivo/radar", () => ({
+ ResponsiveRadar: () => (
+
Mock Responsive Radar
+ ),
+}));
+
+jest.mock("@nivo/pie", () => ({
+ ResponsivePie: () => (
+ Mock Responsive Pie
+ ),
+}));
+
+// Mocking the navigator.share API
+global.navigator.share = jest.fn();
+
+global.Image = class {
+ constructor() {
+ setTimeout(() => {
+ this.onload();
+ }, 50); // Simulate async image loading
+ }
+};
+
+// Mock the getContext and other canvas methods
+HTMLCanvasElement.prototype.getContext = () => ({
+ drawImage: jest.fn(), // Mock drawImage to avoid jsdom type errors
+ fillText: jest.fn(),
+ clearRect: jest.fn(),
+});
+
+HTMLCanvasElement.prototype.toBlob = jest.fn((callback, type, quality) => {
+ setTimeout(() => {
+ callback(new Blob(["test"], { type }));
+ }, 0);
+});
+
+beforeEach(() => {
+ global.navigator.share = jest.fn(() => Promise.resolve());
+});
+
+afterEach(() => {
+ jest.clearAllMocks();
+});
+
+describe("shareCassette", () => {
+ it("should attempt to share using navigator.share when image is ready", async () => {
+ render();
+
+ // Wait for the button with specific text and style to appear in the document
+ const shareButton = await screen.findByRole("button", {
+ name: /share cassette/i,
+ });
+
+ fireEvent.click(shareButton);
+
+ await waitFor(() => {
+ expect(navigator.share).toHaveBeenCalled();
+ });
+
+ // Check navigator.share is called with the expected parameters
+ expect(navigator.share).toHaveBeenCalledWith(
+ expect.objectContaining({
+ title: "Unify with me!",
+ text: "Compare our stats on Uni.fy",
+ url: expect.stringContaining("unify/"), // Check if URL is formed correctly
+ }),
+ );
+ });
+});
diff --git a/__tests__/share_unify.t.js b/__tests__/share_unify.t.js
new file mode 100644
index 0000000..1230ed5
--- /dev/null
+++ b/__tests__/share_unify.t.js
@@ -0,0 +1,93 @@
+/* eslint-disable react/jsx-filename-extension */
+
+import React from "react";
+import { render, fireEvent, waitFor, screen } from "@testing-library/react";
+import { UnifyContent } from "@/app/unify/[users]/UnifyContent";
+
+import userData from "./userData.json";
+
+// Mocking the supabase client and its methods
+jest.mock("../src/utils/supabase/client", () => {
+ return jest.fn(() => ({
+ from: jest.fn().mockReturnThis(),
+ select: jest.fn().mockReturnThis(),
+ eq: jest.fn().mockResolvedValue({
+ data: [
+ {
+ spotify_data: userData,
+ },
+ ],
+ error: null,
+ }),
+ }));
+});
+
+jest.mock("@nivo/radar", () => ({
+ ResponsiveRadar: () => (
+ Mock Responsive Radar
+ ),
+}));
+
+jest.mock("@nivo/pie", () => ({
+ ResponsivePie: () => (
+ Mock Responsive Pie
+ ),
+}));
+
+// Mocking the navigator.share API
+global.navigator.share = jest.fn();
+
+global.Image = class {
+ constructor() {
+ setTimeout(() => {
+ this.onload();
+ }, 50); // Simulate async image loading
+ }
+};
+
+// Mock the getContext and other canvas methods
+HTMLCanvasElement.prototype.getContext = () => ({
+ drawImage: jest.fn(), // Mock drawImage to avoid jsdom type errors
+ fillText: jest.fn(),
+ clearRect: jest.fn(),
+ strokeText: jest.fn(),
+});
+
+HTMLCanvasElement.prototype.toBlob = jest.fn((callback, type, quality) => {
+ setTimeout(() => {
+ callback(new Blob(["test"], { type }));
+ }, 0);
+});
+
+beforeEach(() => {
+ global.navigator.share = jest.fn(() => Promise.resolve());
+});
+
+afterEach(() => {
+ jest.clearAllMocks();
+});
+
+describe("shareCassette", () => {
+ it("should attempt to share using navigator.share when image is ready", async () => {
+ render();
+
+ // Wait for the button with specific text and style to appear in the document
+ const shareButton = await screen.findByRole("button", {
+ name: /share results/i,
+ });
+
+ fireEvent.click(shareButton);
+
+ await waitFor(() => {
+ expect(navigator.share).toHaveBeenCalled();
+ });
+
+ // Check navigator.share is called with the expected parameters
+ expect(navigator.share).toHaveBeenCalledWith(
+ expect.objectContaining({
+ title: "Unify with me!",
+ text: "Compare our stats on Unify",
+ }),
+ );
+ });
+});