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

Commit

Permalink
Clean tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Aug 26, 2024
1 parent 9dc09ad commit 159016b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/API/getLeaderboards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ test('getLeaderboards', async () => {

test('getLeaderboards (Missing Data)', () => {
const client = new Client(process.env.HYPIXEL_KEY ?? '');
const mockRequest = { status: 200, data: { success: true } };
vi.spyOn(axios, 'get').mockResolvedValue(mockRequest);
vi.spyOn(axios, 'get').mockResolvedValue({ status: 200, data: { success: true } });
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(() => client.getLeaderboards()).rejects.toThrowError(
Expand Down
3 changes: 1 addition & 2 deletions src/API/getPlayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ import RecentGame from '../structures/RecentGame';

test('getPlayer (never joinned hypixel)', async () => {
const client = new Client(process.env.HYPIXEL_KEY ?? '', { cache: false, checkForUpdates: false });
const mockRequest = { status: 200, data: { success: true } };
vi.spyOn(axios, 'get').mockResolvedValue(mockRequest);
vi.spyOn(axios, 'get').mockResolvedValue({ status: 200, data: { success: true } });
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await expect(() => client.getPlayer('14727faefbdc4aff848cd2713eb9939e')).rejects.toThrowError(
Expand Down
17 changes: 4 additions & 13 deletions src/Private/RateLimit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,11 @@ test('RateLimit (Auto)', async () => {
test('Ratelimit (Sync)', async () => {
const client = new Client(process.env.HYPIXEL_KEY ?? '');
client.updater.currentVersion = '1.0.0';
const mockRequest = {
vi.spyOn(axios, 'get').mockResolvedValue({
status: 200,
data: { success: true },
headers: {
'ratelimit-limit': 30,
'ratelimit-remaining': 27
}
};
vi.spyOn(axios, 'get').mockResolvedValue(mockRequest);
headers: { 'ratelimit-limit': 30, 'ratelimit-remaining': 27 }
});
expect(() => client.rateLimit.sync()).not.toThrowError();
await client.rateLimit.sync();
expect(client.rateLimit.requests).toBe(3);
Expand All @@ -87,12 +83,7 @@ test('Ratelimit (Sync)', async () => {
test('Ratelimit (Bad Sync Data)', () => {
const client = new Client(process.env.HYPIXEL_KEY ?? '');
client.updater.currentVersion = '1.0.0';
const mockRequest = {
status: 200,
data: { success: true },
headers: { hello: 100 }
};
vi.spyOn(axios, 'get').mockResolvedValue(mockRequest);
vi.spyOn(axios, 'get').mockResolvedValue({ status: 200, data: { success: true }, headers: { hello: 100 } });
expect(() => client.rateLimit.sync()).rejects.toThrowError(client.errors.RATE_LIMIT_INIT_ERROR);
vi.restoreAllMocks();
client.destroy();
Expand Down
9 changes: 3 additions & 6 deletions src/Private/Requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ test('Requests (Invalid API Key)', () => {
const client = new Client(process.env.HYPIXEL_KEY ?? '');
expect(client.requests.request).toBeDefined();
expectTypeOf(client.requests.request).toBeFunction();
const mockRequest = { status: 403, data: {} };
vi.spyOn(axios, 'get').mockResolvedValue(mockRequest);
vi.spyOn(axios, 'get').mockResolvedValue({ status: 403, data: {} });
expect(() => client.requests.request('/boosters')).rejects.toThrowError(client.errors.INVALID_API_KEY);
vi.restoreAllMocks();
client.destroy();
Expand All @@ -40,8 +39,7 @@ test('Requests (Unprocessable Entity)', () => {
const client = new Client(process.env.HYPIXEL_KEY ?? '');
expect(client.requests.request).toBeDefined();
expectTypeOf(client.requests.request).toBeFunction();
const mockRequest = { status: 422, data: {} };
vi.spyOn(axios, 'get').mockResolvedValue(mockRequest);
vi.spyOn(axios, 'get').mockResolvedValue({ status: 422, data: {} });
expect(() => client.requests.request('/boosters')).rejects.toThrowError(client.errors.UNEXPECTED_ERROR);
vi.restoreAllMocks();
client.destroy();
Expand All @@ -51,8 +49,7 @@ test('Requests (Rate Limited)', () => {
const client = new Client(process.env.HYPIXEL_KEY ?? '');
expect(client.requests.request).toBeDefined();
expectTypeOf(client.requests.request).toBeFunction();
const mockRequest = { status: 429, data: {} };
vi.spyOn(axios, 'get').mockResolvedValue(mockRequest);
vi.spyOn(axios, 'get').mockResolvedValue({ status: 429, data: {} });
expect(() => client.requests.request('/boosters')).rejects.toThrowError(client.errors.RATE_LIMIT_EXCEEDED);
vi.restoreAllMocks();
client.destroy();
Expand Down
9 changes: 3 additions & 6 deletions src/Private/Updater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ test('Updater', () => {

test('Updater (get latest version)', async () => {
const client = new Client(process.env.HYPIXEL_KEY ?? '');
const mockRequest = { status: 200, data: { 'dist-tags': { latest: '1.0.0' } } };
vi.spyOn(axios, 'get').mockResolvedValue(mockRequest);
vi.spyOn(axios, 'get').mockResolvedValue({ status: 200, data: { 'dist-tags': { latest: '1.0.0' } } });
const data = await client.updater.getLatestVersion();
expect(data).toBe('1.0.0');
vi.restoreAllMocks();
Expand All @@ -46,8 +45,7 @@ test('Updater (get latest version)', async () => {

test('Updater (get latest version error)', () => {
const client = new Client(process.env.HYPIXEL_KEY ?? '');
const mockRequest = { status: 404, data: { 'dist-tags': { latest: '1.0.0' } } };
vi.spyOn(axios, 'get').mockResolvedValue(mockRequest);
vi.spyOn(axios, 'get').mockResolvedValue({ status: 404, data: { 'dist-tags': { latest: '1.0.0' } } });
expect(() => client.updater.getLatestVersion()).rejects.toThrowError(client.errors.UPDATER_REQUEST_NOT_OK);
vi.restoreAllMocks();
client.destroy();
Expand All @@ -57,8 +55,7 @@ test('Updater (check version)', async () => {
const client = new Client(process.env.HYPIXEL_KEY ?? '');
const consoleLogSpy = vi.spyOn(console, 'log');
client.updater.currentVersion = '1.0.0';
const mockRequest = { status: 200, data: { 'dist-tags': { latest: version } } };
vi.spyOn(axios, 'get').mockResolvedValue(mockRequest);
vi.spyOn(axios, 'get').mockResolvedValue({ status: 200, data: { 'dist-tags': { latest: version } } });
await client.updater.checkForUpdates();
expect(consoleLogSpy).toHaveBeenCalledWith(
`New version of hypixel-api-reborn is available! Current version: 1.0.0, Latest version: ${version}`
Expand Down

0 comments on commit 159016b

Please sign in to comment.