Skip to content
Merged
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
6 changes: 6 additions & 0 deletions test/client/eurofotbal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,11 @@ describe('SoccerBotEurofotbalClient', () => {
expect(result.data).toContainEqual(expect.objectContaining({ id: 'ondrej-kolar-32017', name: 'Ondřej Kolář' }));
}
});

it('should return an empty team when roster sections are missing', async () => {
vi.spyOn(SoccerBotEurofotbalClient.prototype as any, 'fetchPage').mockResolvedValue('<html></html>');

expect(await client.team('cesko/slavia-praha')).toEqual({ ok: true, data: [] });
});
});
});
65 changes: 65 additions & 0 deletions test/client/shared.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fetch from 'cross-fetch';
import { vi } from 'vitest';
import { SoccerBotClient, UserAgents } from '../../src/clients/shared';
import { SoccerBotPlayer, SoccerBotResponse, SoccerBotTeam } from '../../src/shared/interfaces';

vi.mock('cross-fetch', () => ({ default: vi.fn() }));

class TestClient extends SoccerBotClient {
public async league(): Promise<SoccerBotResponse<SoccerBotTeam[]>> {
return { ok: true, data: [] };
}

public async team(): Promise<SoccerBotResponse<SoccerBotPlayer[]>> {
return { ok: true, data: [] };
}

public getPage(url: string, silent?: boolean): Promise<string> {
return this.fetchPage(url, silent);
}
}

describe('SoccerBotClient', () => {
const fetchMock = vi.mocked(fetch);
let client: TestClient;

beforeEach(() => {
client = new TestClient();
fetchMock.mockReset();
});

afterEach(() => {
vi.restoreAllMocks();
});

it('should fetch a page with a browser user agent', async () => {
vi.spyOn(Math, 'random').mockReturnValue(0);
fetchMock.mockResolvedValue({
status: 200,
text: vi.fn().mockResolvedValue('<html>Success</html>')
} as any);

await expect(client.getPage('https://example.com')).resolves.toBe('<html>Success</html>');
expect(fetchMock).toHaveBeenCalledWith('https://example.com', {
headers: { 'User-Agent': UserAgents.Windows1 }
});
});

it('should report an HTTP error and return the error sentinel', async () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined);
fetchMock.mockResolvedValue({ status: 503 } as any);

await expect(client.getPage('https://example.com')).resolves.toBe('Error');
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.objectContaining({ message: 'Bad response 503 for url: https://example.com' })
);
});

it('should suppress errors for silent requests', async () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined);
fetchMock.mockRejectedValue(new Error('Network failure'));

await expect(client.getPage('https://example.com', true)).resolves.toBe('Error');
expect(consoleErrorSpy).not.toHaveBeenCalled();
});
});
13 changes: 12 additions & 1 deletion test/client/soccerway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { LEAGUE_DATA, LEAGUE_HTML } from '../mocks/soccerway/league';
import { PLAYER_DATA, PLAYER_HTML } from '../mocks/soccerway/player';
import { TEAM_DATA, TEAM_HTML } from '../mocks/soccerway/team';

vi.mock('../../src/helpers', async (importOriginal) => ({
...(await importOriginal<typeof import('../../src/helpers')>()),
sleep: vi.fn().mockResolvedValue(undefined)
}));

describe('SoccerBotSoccerwayClient', () => {
let client: SoccerBotSoccerwayClient;

Expand Down Expand Up @@ -78,6 +83,12 @@ describe('SoccerBotSoccerwayClient', () => {
expect(result.data).toContainEqual({ id: 'slavia-prague/viXGgnyB', name: 'Slavia Prague' });
}
});

it('should return an error when the league feed is missing', async () => {
vi.spyOn(SoccerBotSoccerwayClient.prototype as any, 'fetchPage').mockResolvedValue('<html></html>');

expect((await client.league('czech-republic/chance-liga/standings/bNFMkskm')).ok).toBe(false);
});
});

describe('team', () => {
Expand Down Expand Up @@ -107,7 +118,7 @@ describe('SoccerBotSoccerwayClient', () => {
expect(handlePlayerSpy).toHaveBeenCalledTimes(48);
expect(handlePlayerSpy).toHaveBeenCalledWith('kolar-ondrej/xfBGcS1U');
if (TEAM_DATA.ok) {
expect(handlePlayerSpy.mock.calls.map(([id]) => id)).toEqual(TEAM_DATA.data.map(({ id }) => id));
expect(handlePlayerSpy.mock.calls.map(([id]: [string]) => id)).toEqual(TEAM_DATA.data.map(({ id }) => id));
}
});
});
Expand Down
5 changes: 5 additions & 0 deletions test/client/sportnet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { LEAGUE_DATA, LEAGUE_HTML } from '../mocks/sportnet/league';
import { PLAYER_DATA, PLAYER_HTML } from '../mocks/sportnet/player';
import { TEAM_COMPETITION_1_HTML, TEAM_COMPETITION_2_HTML, TEAM_HTML } from '../mocks/sportnet/team';

vi.mock('../../src/helpers/shared', () => ({
sleep: vi.fn().mockResolvedValue(undefined)
}));

describe('SoccerBotSportnetClient', () => {
let client: SoccerBotSportnetClient;

Expand Down Expand Up @@ -75,6 +79,7 @@ describe('SoccerBotSportnetClient', () => {
<table>
<tbody>
<tr><td>Unrelated table row</td></tr>
<tr><td><a href="/futbalnet/k/">Malformed team link</a></td></tr>
<tr><td><a href="/futbalnet/k/example-fc/tim/dospeli-m-a/">Example FC</a></td></tr>
</tbody>
</table>
Expand Down
3 changes: 3 additions & 0 deletions test/client/transfermarkt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ describe('SoccerBotTransfermarktClient', () => {
describe('teamUrl', () => {
it('should return correct value', () => {
expect(client.teamUrl('533')).toEqual('https://www.transfermarkt.com/slug/kader/verein/533/plus/1');
expect(client.teamUrl('533', '2018')).toEqual(
'https://www.transfermarkt.com/slug/kader/verein/533/plus/1?saison_id=2018'
);
});

it('should return undefined', () => {
Expand Down