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

Commit e894a02

Browse files
committed
test(API): refactor and add more test cases
1 parent 63928b5 commit e894a02

File tree

6 files changed

+126
-168
lines changed

6 files changed

+126
-168
lines changed

jest.config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
"coverageDirectory": "coverage",
66
"coverageReporters": ["text", "lcov", "clover"],
77
"testMatch": ["**/?(*.)+(spec|test).[jt]s?(x)"],
8-
"setupFiles": ["dotenv/config"]
8+
"setupFiles": ["dotenv/config"],
9+
"verbose": true,
10+
"roots": ["<rootDir>/tests/"],
11+
"moduleNameMapper": {
12+
"^@/(.*)$": "<rootDir>/src/$1",
13+
"^@$": "<rootDir>/src"
14+
}
915
}

src/__test__/API.test.ts

Lines changed: 0 additions & 163 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { API } from "../API";
1+
import { API } from "@/API";
22

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

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

4949
describe("getData", () => {
5050
const api = new API();
51-
test("invalid uri", async () => {
51+
test("invalid url", async () => {
5252
await expect(api.getData("invalid-url")).rejects.toThrowError("Invalid URL");
5353
await expect(api.getData("https://open.spotify.com/show/")).rejects.toThrowError("Invalid URL");
5454
await expect(api.getData("https://open.spotify.com/show/id")).rejects.toThrowError("Unsupported URL type");

tests/API.test.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { API } from "@/API";
2+
3+
const TRACK_URL = "https://open.spotify.com/track/6Fbsn9471Xd0vVsMWwhePh?si=f992e1fe1f714674";
4+
const ALBUM_URL = "https://open.spotify.com/album/5Gu0Ldddj2f6a0q5gitIok?si=781b3339b2bc4015";
5+
const PLAYLIST_URL = "https://open.spotify.com/playlist/1IetTTs69jS1MQ3U6yxofB?si=182a1e01ff374fc8";
6+
const ARTIST_URL = "https://open.spotify.com/artist/3FwYnbtGNt8hJfjNuOfpeG?si=d4c1d832636c4bb1";
7+
const LONG_PLAYLIST_URL = "https://open.spotify.com/playlist/0HlfmkivifBxTcNeoev41s?si=8f7aa0dac31a406d";
8+
const LONG_ALBUM_URL = "https://open.spotify.com/album/1fnZbc8ZrgyW6RpFvwXkwl";
9+
10+
const PRIVATE_ID = "1CReGsrMUgMQXinhawwIKX";
11+
12+
const expectTrack = {
13+
type: "track",
14+
name: expect.any(String),
15+
artists: expect.arrayContaining([expect.objectContaining({ name: expect.any(String) })]),
16+
};
17+
18+
const expectList = (type: "track" | "album" | "playlist" | "artist" | string) =>
19+
type === "track"
20+
? expectTrack
21+
: {
22+
type,
23+
name: expect.any(String),
24+
thumbnail: expect.any(String),
25+
url: expect.any(String),
26+
tracks: expect.arrayContaining([expect.objectContaining(expectTrack)]),
27+
};
28+
29+
describe.each([
30+
["Without", undefined, undefined],
31+
["With", process.env.SPOTIFY_CLIENT_ID, process.env.SPOTIFY_CLIENT_SECRET],
32+
])("%s client credentials", (_name, clientId, clientSecret) => {
33+
describe.each([
34+
["Using spotify api", true],
35+
["Using spotify embed", false],
36+
])("%s", (_name, useAPI) => {
37+
const api = new API(clientId, clientSecret, "VN");
38+
if (useAPI) {
39+
test("refreshToken", async () => {
40+
await api.refreshToken();
41+
expect(api["_tokenAvailable"]).toBe(true);
42+
expect(api["_hasCredentials"]).toBe(!!(clientId && clientSecret));
43+
expect(api["_expirationTime"]).toBeGreaterThan(Date.now());
44+
});
45+
} else {
46+
api["_tokenAvailable"] = false;
47+
api["_hasCredentials"] = false;
48+
api["_expirationTime"] = Infinity;
49+
}
50+
51+
describe("getData", () => {
52+
test.each([
53+
["track", TRACK_URL],
54+
["album", ALBUM_URL],
55+
["playlist", PLAYLIST_URL],
56+
["artist", ARTIST_URL],
57+
])("get %s", async (type, url) => {
58+
const data = await api.getData(url);
59+
expect(data).toMatchObject(expectList(type));
60+
if (data.type !== "track") {
61+
expect(data.tracks.length).toBeGreaterThanOrEqual(1);
62+
expect(data.tracks).not.toContain(null);
63+
expect(data.tracks).not.toContainEqual(expect.objectContaining({ type: expect.not.stringMatching("track") }));
64+
}
65+
});
66+
67+
test.each([
68+
["playlist", LONG_PLAYLIST_URL, 900],
69+
["album", LONG_ALBUM_URL, 50],
70+
])(
71+
"get long %s",
72+
async (type, url, length) => {
73+
const data = await api.getData(url);
74+
expect(data).toMatchObject(expectList(type));
75+
expect(data.type).toBe(type);
76+
if (data.type !== "track") {
77+
expect(data.tracks.length).toBeGreaterThanOrEqual(useAPI ? length : 100);
78+
}
79+
},
80+
15_000,
81+
);
82+
});
83+
});
84+
});
85+
86+
describe("API error handling", () => {
87+
const api = new API();
88+
89+
test("refreshToken", async () => {
90+
await api.refreshToken();
91+
expect(api["_tokenAvailable"]).toBe(true);
92+
expect(api["_hasCredentials"]).toBe(false);
93+
expect(api["_expirationTime"]).toBeGreaterThan(Date.now());
94+
});
95+
96+
test.each([
97+
["track", `https://open.spotify.com/track/${PRIVATE_ID}`],
98+
["album", `https://open.spotify.com/album/${PRIVATE_ID}`],
99+
["playlist", `https://open.spotify.com/playlist/${PRIVATE_ID}`],
100+
["artist", `https://open.spotify.com/artist/${PRIVATE_ID}`],
101+
])("private %s", async (_type, url) => {
102+
await expect(api.getData(url)).rejects.toThrow("The URL is private or unavailable.");
103+
});
104+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SpotifyPlugin } from "..";
1+
import { SpotifyPlugin } from "@";
22

33
test.todo("Validate Options");
44
test.todo("SpotifyPlugin#play()");

tests/tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"paths": {
5+
"@": ["../src"],
6+
"@/*": ["../src/*"]
7+
},
8+
"strict": false
9+
},
10+
"include": ["**/*.ts"]
11+
}

0 commit comments

Comments
 (0)