@chargetrip/mcp
Version:
Chargetrip MCP server
118 lines (86 loc) • 3.51 kB
text/typescript
import { GraphQLClient } from '../graphql-client';
jest.mock('graphql-ws', () => ({
createClient: jest.fn(),
}));
describe('GraphQLClient', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {
...originalEnv,
GRAPHQL_ENDPOINT: 'http://localhost:4000/graphql',
GRAPHQL_WS_ENDPOINT: 'ws://localhost:4000/graphql',
};
});
afterAll(() => {
process.env = originalEnv;
});
describe('getInstance', () => {
it('should return the existing instance', () => {
const instance = GraphQLClient.getInstance();
expect(instance).toBe(GraphQLClient['instance']);
});
});
describe('query', () => {
it('should execute GraphQL query with variables', async () => {
const client = new GraphQLClient();
const mockResult = { data: { test: 'value' } };
jest.spyOn(client['client'], 'executeQuery').mockReturnValue({
toPromise: jest.fn().mockResolvedValue(mockResult),
} as any);
const result = await client.query('query Test($id: ID!) { test(id: $id) }', { id: '123' });
expect(result).toBe(mockResult);
});
it('should execute GraphQL query without variables', async () => {
const client = new GraphQLClient();
const mockResult = { data: { test: 'value' } };
jest.spyOn(client['client'], 'executeQuery').mockReturnValue({
toPromise: jest.fn().mockResolvedValue(mockResult),
} as any);
const result = await client.query('query { test }');
expect(result).toBe(mockResult);
});
});
describe('mutate', () => {
it('should execute GraphQL mutation with variables', async () => {
const client = new GraphQLClient();
const mockResult = { data: { createTest: { id: '123' } } };
jest.spyOn(client['client'], 'executeMutation').mockReturnValue({
toPromise: jest.fn().mockResolvedValue(mockResult),
} as any);
const result = await client.mutate(
'mutation CreateTest($input: TestInput!) { createTest(input: $input) { id } }',
{ input: {} },
);
expect(result).toBe(mockResult);
});
it('should execute GraphQL mutation without variables', async () => {
const client = new GraphQLClient();
const mockResult = { data: { deleteTest: true } };
jest.spyOn(client['client'], 'executeMutation').mockReturnValue({
toPromise: jest.fn().mockResolvedValue(mockResult),
} as any);
const result = await client.mutate('mutation { deleteTest }');
expect(result).toBe(mockResult);
});
});
describe('subscription', () => {
it('should execute GraphQL subscription with variables', () => {
const client = new GraphQLClient();
const mockObservable = { subscribe: jest.fn() };
jest.spyOn(client['client'], 'executeSubscription').mockReturnValue(mockObservable as any);
const result = client.subscription(
'subscription TestSub($id: ID!) { testUpdated(id: $id) }',
{ id: '123' },
);
expect(result).toBe(mockObservable);
});
it('should execute GraphQL subscription without variables', () => {
const client = new GraphQLClient();
const mockObservable = { subscribe: jest.fn() };
jest.spyOn(client['client'], 'executeSubscription').mockReturnValue(mockObservable as any);
const result = client.subscription('subscription { testUpdated }');
expect(result).toBe(mockObservable);
});
});
});