Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More cocktailClass Tests #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 205 additions & 1 deletion __tests__/helpers/cocktailClass.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Cocktail } from "@/interfaces/cocktails";
import { CocktailDbClient } from "@/helpers/cocktailClass";
import { CocktailDbClient, profanityWords } from "@/helpers/cocktailClass";
import fetchMock from "jest-fetch-mock";
import { Ingredient } from "@/interfaces/ingredient";
import { User } from "@/app/page";

beforeEach(() => {
fetchMock.resetMocks();
Expand All @@ -18,6 +19,11 @@ const mockIngredients = ["Egg", "Gin"];
const mockAllergies = ["Vodka", "Lemon"];
const cocktailDBClient = new CocktailDbClient();
const mockCocktailID = "123";
const mockUserData = {
sub: "user-sub",
allergies: ["Vodka", "Lemon"],
favoriteCocktails: [],
} as User;

// Mock cocktails with duplicate entry for testing purposes
const mockCocktails = {
Expand Down Expand Up @@ -403,6 +409,166 @@ test("fetchIngredientDataById response error", async () => {
);
});

test("fetchPopularCocktails", async () => {
fetchMock.mockResponseOnce(JSON.stringify(mockCocktails));

const resultPromise = cocktailDBClient.fetchPopularCocktails();

await expect(resultPromise).resolves.toEqual(expectedOutput);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/popular.php`
);
});

test("fetchPopularCocktails with no results found", async () => {
fetchMock.mockResponseOnce(JSON.stringify({ drinks: [] as Cocktail[] }));

const resultPromise = cocktailDBClient.fetchPopularCocktails();

await expect(resultPromise).resolves.toEqual([] as Cocktail[]);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/popular.php`
);
});

test("fetchPopularCocktails response error", async () => {
fetchMock.mockReject(new Error("API Error"));

const resultPromise = cocktailDBClient.fetchPopularCocktails();

await expect(resultPromise).resolves.toEqual([] as Cocktail[]);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/popular.php`
);
});

test("fetchNewCocktails", async () => {
fetchMock.mockResponseOnce(JSON.stringify(mockCocktails));

const resultPromise = cocktailDBClient.fetchNewCocktails();

await expect(resultPromise).resolves.toEqual(expectedOutput);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/latest.php`
);
});

test("fetchNewCocktails with no results found", async () => {
fetchMock.mockResponseOnce(JSON.stringify({ drinks: [] as Cocktail[] }));

const resultPromise = cocktailDBClient.fetchNewCocktails();

await expect(resultPromise).resolves.toEqual([] as Cocktail[]);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/latest.php`
);
});

test("fetchNewCocktails response error", async () => {
fetchMock.mockReject(new Error("API Error"));

const resultPromise = cocktailDBClient.fetchNewCocktails();

await expect(resultPromise).resolves.toEqual([] as Cocktail[]);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/latest.php`
);
});

test("fetchRandomCocktails", async () => {
fetchMock.mockResponseOnce(JSON.stringify(mockCocktails));

const resultPromise = cocktailDBClient.fetchRandomCocktails();

await expect(resultPromise).resolves.toEqual(expectedOutput);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/randomselection.php`
);
});

test("fetchRandomCocktails with no results found", async () => {
fetchMock.mockResponseOnce(JSON.stringify({ drinks: [] as Cocktail[] }));

const resultPromise = cocktailDBClient.fetchRandomCocktails();

await expect(resultPromise).resolves.toEqual([] as Cocktail[]);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/randomselection.php`
);
});

test("fetchRandomCocktails response error", async () => {
fetchMock.mockReject(new Error("API Error"));

const resultPromise = cocktailDBClient.fetchRandomCocktails();

await expect(resultPromise).resolves.toEqual([] as Cocktail[]);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/randomselection.php`
);
});

test("fetchSingleRandomCocktail", async () => {
fetchMock.mockResponseOnce(JSON.stringify(mockCocktails));

const resultPromise = cocktailDBClient.fetchSingleRandomCocktail();

await expect(resultPromise).resolves.toEqual(expectedOutput[0]);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/random.php`
);
});

test("fetchSingleRandomCocktail with no results found", async () => {
fetchMock.mockResponseOnce(JSON.stringify({ drinks: [] as Cocktail[] }));

const resultPromise = cocktailDBClient.fetchSingleRandomCocktail();

await expect(resultPromise).resolves.toEqual({} as Cocktail);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/random.php`
);
});

test("fetchSingleRandomCocktail response error", async () => {
fetchMock.mockReject(new Error("API Error"));

const resultPromise = cocktailDBClient.fetchSingleRandomCocktail();

await expect(resultPromise).resolves.toEqual({} as Cocktail);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/random.php`
);
});

test("fetchSingleRandomCocktail with profanity result", async () => {
fetchMock.mockResponseOnce(
JSON.stringify({
drinks: [{ strDrink: profanityWords.values().next() }],
})
);

const resultPromise = cocktailDBClient.fetchSingleRandomCocktail();

await expect(resultPromise).resolves.toEqual({} as Cocktail);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
`https://www.thecocktaildb.com/api/json/v2/${API_KEY}/random.php`
);
});

test("filterCocktails by allergies", () => {
const result = cocktailDBClient.filterCocktails(
mockAllergies,
Expand Down Expand Up @@ -442,3 +608,41 @@ test("filterCocktails by ingredients with no cocktails", () => {

expect(result).toEqual([]);
});

test("handleFavoriteCocktail", () => {
const mockCocktail = expectedOutput[0];

const result = cocktailDBClient.handleFavoriteCocktail(
mockCocktail.cocktailId,
mockUserData
);

const expectedUserData = {
...mockUserData,
favoriteCocktails: [
...mockUserData.favoriteCocktails,
mockCocktail.cocktailId,
],
};

expect(result).toEqual(expectedUserData);
});

test("handleFavoriteCocktail to remove favorite", () => {
const mockCocktail = expectedOutput[0];

const result = cocktailDBClient.handleFavoriteCocktail(
mockCocktail.cocktailId,
{
...mockUserData,
favoriteCocktails: [mockCocktail.cocktailId],
}
);

const expectedUserData = {
...mockUserData,
favoriteCocktails: [],
};

expect(result).toEqual(expectedUserData);
});
35 changes: 34 additions & 1 deletion __tests__/helpers/textFuncs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import "@testing-library/jest-dom";
import { toSentenceCase } from "@/helpers/textFuncs";
import {
toSentenceCase,
breakTextIntoTwoSentenceChunks,
} from "@/helpers/textFuncs";

describe("toSentenceCase", () => {
it("should convert the first character of a string to uppercase and the rest to lowercase", () => {
Expand All @@ -18,3 +21,33 @@ describe("toSentenceCase", () => {
expect(toSentenceCase("A")).toBe("A");
});
});

describe("breakTextIntoTwoSentenceChunks", () => {
it("should handle a string with only one sentence", () => {
const testString = "This is a test.";
const expectedResult = ["This is a test."];
expect(breakTextIntoTwoSentenceChunks(testString)).toEqual(expectedResult);
});

it("should not break text that is less than 3 sentences in length", () => {
const testString = "This is a test. This is another test.";
const expectedResult = ["This is a test. This is another test."];
expect(breakTextIntoTwoSentenceChunks(testString)).toEqual(expectedResult);
});

it("should handle an empty string", () => {
const testString = "";
const expectedResult = [""];
expect(breakTextIntoTwoSentenceChunks(testString)).toEqual(expectedResult);
});

it("should handle a string with more than 3 sentences", () => {
const testString =
"This is a test. This is another test. This is yet another test. This is the final test.";
const expectedResult = [
"This is a test. This is another test.",
"This is yet another test. This is the final test.",
];
expect(breakTextIntoTwoSentenceChunks(testString)).toEqual(expectedResult);
});
});
83 changes: 0 additions & 83 deletions components/carousel.tsx

This file was deleted.

4 changes: 3 additions & 1 deletion helpers/cocktailClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Cocktail, OriginalCocktail } from "@/interfaces/cocktails";
import { Ingredient, OriginalIngredient } from "@/interfaces/ingredient";

// Words that should not be included in any cocktail names
const profanityWords = new Set([
export const profanityWords = new Set([
"fuck",
"f**k",
"ass",
Expand Down Expand Up @@ -316,13 +316,15 @@ export class CocktailDbClient {
if (data.drinks.length === 0) {
return {} as Cocktail;
}
cocktailName = data.drinks[0].strDrink;
return this.formatCocktails(data.drinks)[0];
} catch (error) {
console.error(error);
return {} as Cocktail;
}
}

// This should only be reached if the maximum number of retries is reached
return {} as Cocktail;
}

Expand Down
Loading