Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
test(API): refactor and add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
skick1234 committed Dec 23, 2022
1 parent 63928b5 commit e894a02
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 168 deletions.
8 changes: 7 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
"coverageDirectory": "coverage",
"coverageReporters": ["text", "lcov", "clover"],
"testMatch": ["**/?(*.)+(spec|test).[jt]s?(x)"],
"setupFiles": ["dotenv/config"]
"setupFiles": ["dotenv/config"],
"verbose": true,
"roots": ["<rootDir>/tests/"],
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1",
"^@$": "<rootDir>/src"
}
}
163 changes: 0 additions & 163 deletions src/__test__/API.test.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/__test__/API.error.test.ts → tests/API.mock.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { API } from "../API";
import { API } from "@/API";

import * as _undici from "undici";
jest.mock("undici");
const undici = _undici as jest.Mocked<typeof _undici>;

describe("API error handling", () => {
describe("Error handling with mocking modules", () => {
test("invalid region code", () => {
expect(() => new API(undefined, undefined, "invalid-region-code")).toThrowError("Invalid region code");
expect(() => new API(undefined, undefined, "vn")).toThrowError("Invalid region code");
Expand Down Expand Up @@ -48,7 +48,7 @@ describe("API error handling", () => {

describe("getData", () => {
const api = new API();
test("invalid uri", async () => {
test("invalid url", async () => {
await expect(api.getData("invalid-url")).rejects.toThrowError("Invalid URL");
await expect(api.getData("https://open.spotify.com/show/")).rejects.toThrowError("Invalid URL");
await expect(api.getData("https://open.spotify.com/show/id")).rejects.toThrowError("Unsupported URL type");
Expand Down
104 changes: 104 additions & 0 deletions tests/API.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { API } from "@/API";

const TRACK_URL = "https://open.spotify.com/track/6Fbsn9471Xd0vVsMWwhePh?si=f992e1fe1f714674";
const ALBUM_URL = "https://open.spotify.com/album/5Gu0Ldddj2f6a0q5gitIok?si=781b3339b2bc4015";
const PLAYLIST_URL = "https://open.spotify.com/playlist/1IetTTs69jS1MQ3U6yxofB?si=182a1e01ff374fc8";
const ARTIST_URL = "https://open.spotify.com/artist/3FwYnbtGNt8hJfjNuOfpeG?si=d4c1d832636c4bb1";
const LONG_PLAYLIST_URL = "https://open.spotify.com/playlist/0HlfmkivifBxTcNeoev41s?si=8f7aa0dac31a406d";
const LONG_ALBUM_URL = "https://open.spotify.com/album/1fnZbc8ZrgyW6RpFvwXkwl";

const PRIVATE_ID = "1CReGsrMUgMQXinhawwIKX";

const expectTrack = {
type: "track",
name: expect.any(String),
artists: expect.arrayContaining([expect.objectContaining({ name: expect.any(String) })]),
};

const expectList = (type: "track" | "album" | "playlist" | "artist" | string) =>
type === "track"
? expectTrack
: {
type,
name: expect.any(String),
thumbnail: expect.any(String),
url: expect.any(String),
tracks: expect.arrayContaining([expect.objectContaining(expectTrack)]),
};

describe.each([
["Without", undefined, undefined],
["With", process.env.SPOTIFY_CLIENT_ID, process.env.SPOTIFY_CLIENT_SECRET],
])("%s client credentials", (_name, clientId, clientSecret) => {
describe.each([
["Using spotify api", true],
["Using spotify embed", false],
])("%s", (_name, useAPI) => {
const api = new API(clientId, clientSecret, "VN");
if (useAPI) {
test("refreshToken", async () => {
await api.refreshToken();
expect(api["_tokenAvailable"]).toBe(true);
expect(api["_hasCredentials"]).toBe(!!(clientId && clientSecret));
expect(api["_expirationTime"]).toBeGreaterThan(Date.now());
});
} else {
api["_tokenAvailable"] = false;
api["_hasCredentials"] = false;
api["_expirationTime"] = Infinity;
}

describe("getData", () => {
test.each([
["track", TRACK_URL],
["album", ALBUM_URL],
["playlist", PLAYLIST_URL],
["artist", ARTIST_URL],
])("get %s", async (type, url) => {
const data = await api.getData(url);
expect(data).toMatchObject(expectList(type));
if (data.type !== "track") {
expect(data.tracks.length).toBeGreaterThanOrEqual(1);
expect(data.tracks).not.toContain(null);
expect(data.tracks).not.toContainEqual(expect.objectContaining({ type: expect.not.stringMatching("track") }));
}
});

test.each([
["playlist", LONG_PLAYLIST_URL, 900],
["album", LONG_ALBUM_URL, 50],
])(
"get long %s",
async (type, url, length) => {
const data = await api.getData(url);
expect(data).toMatchObject(expectList(type));
expect(data.type).toBe(type);
if (data.type !== "track") {
expect(data.tracks.length).toBeGreaterThanOrEqual(useAPI ? length : 100);
}
},
15_000,
);
});
});
});

describe("API error handling", () => {
const api = new API();

test("refreshToken", async () => {
await api.refreshToken();
expect(api["_tokenAvailable"]).toBe(true);
expect(api["_hasCredentials"]).toBe(false);
expect(api["_expirationTime"]).toBeGreaterThan(Date.now());
});

test.each([
["track", `https://open.spotify.com/track/${PRIVATE_ID}`],
["album", `https://open.spotify.com/album/${PRIVATE_ID}`],
["playlist", `https://open.spotify.com/playlist/${PRIVATE_ID}`],
["artist", `https://open.spotify.com/artist/${PRIVATE_ID}`],
])("private %s", async (_type, url) => {
await expect(api.getData(url)).rejects.toThrow("The URL is private or unavailable.");
});
});
2 changes: 1 addition & 1 deletion src/__test__/index.test.ts → tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SpotifyPlugin } from "..";
import { SpotifyPlugin } from "@";

test.todo("Validate Options");
test.todo("SpotifyPlugin#play()");
Expand Down
11 changes: 11 additions & 0 deletions tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"paths": {
"@": ["../src"],
"@/*": ["../src/*"]
},
"strict": false
},
"include": ["**/*.ts"]
}

0 comments on commit e894a02

Please sign in to comment.