chesscom-ts-client
Version:
A TypeScript client for the Chess.com API
84 lines (83 loc) • 3.77 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const client_1 = require("../client");
const axios_1 = __importDefault(require("axios"));
// Cast axios to a mocked version
const mockedAxios = axios_1.default;
describe('ChessComClient', () => {
let client;
beforeEach(() => {
// Clear all mocks before each test
mockedAxios.get.mockClear();
// Mock axios.create to return a mocked instance
mockedAxios.create.mockReturnValue({
get: jest.fn(), // Mock the get method of the created instance
}); // Cast to any to avoid complex typing issues with partial mocks
client = new client_1.ChessComClient();
});
it('should fetch player profile successfully', () => __awaiter(void 0, void 0, void 0, function* () {
const mockProfile = {
avatar: 'https://www.chess.com/bundles/web/images/user-image.007dad08.svg',
player_id: 12345,
url: 'https://www.chess.com/member/erik',
username: 'erik',
followers: 100000,
country: 'https://api.chess.com/pub/country/US',
last_online: 1678886400,
joined: 1234567890,
status: 'staff',
is_streamer: false,
league: 'legend',
};
// Access the get method of the *actual* axios instance used by the client
// This is the instance returned by mockedAxios.create()
client.axiosInstance.get.mockResolvedValueOnce({
data: mockProfile,
status: 200,
statusText: 'OK',
headers: {},
config: {},
});
const username = 'erik';
const profile = yield client.getPlayerProfile(username);
expect(profile).toEqual(mockProfile);
expect(client.axiosInstance.get).toHaveBeenCalledTimes(1);
expect(client.axiosInstance.get).toHaveBeenCalledWith(`/player/${username}`);
}));
it('should fetch player stats successfully', () => __awaiter(void 0, void 0, void 0, function* () {
const mockStats = {
chess_blitz: {
last: { rating: 2000, date: 1678886400, rd: 50 },
record: { win: 100, loss: 50, draw: 10 },
},
chess_bullet: {
last: { rating: 1800, date: 1678886400, rd: 50 },
record: { win: 80, loss: 40, draw: 5 },
},
};
client.axiosInstance.get.mockResolvedValueOnce({
data: mockStats,
status: 200,
statusText: 'OK',
headers: {},
config: {},
});
const username = 'erik';
const stats = yield client.getPlayerStats(username);
expect(stats).toEqual(mockStats);
expect(client.axiosInstance.get).toHaveBeenCalledTimes(1);
expect(client.axiosInstance.get).toHaveBeenCalledWith(`/player/${username}/stats`);
}));
});