Skip to content

Commit

Permalink
Adding GraphQL Unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesliupenn committed May 31, 2024
1 parent 9ba95b0 commit 94bad6b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dimo-network/dimo-node-sdk",
"version": "1.1.6",
"version": "1.1.7",
"description": "DIMO SDK for JavaScript",
"main": "dist/index.js",
"author": "James Li",
Expand Down
53 changes: 53 additions & 0 deletions src/graphql/Query.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import axios from 'axios';
import { CustomQuery, Query } from './Query'; // Import the Query function to be tested
import { DimoError } from '../errors';
import { DimoEnvironment } from '../environments';

const RESOURCE = {
method: 'POST',
path: '',
queryParams: { param1: true },
};
const PARAM = { query: `{
vehicles (first:10) {
totalCount
}
}`};

describe('Query Function', () => {
test('Valid API Call - Identity API Server is up and returning data', async () => {
jest.spyOn(axios, 'request').mockResolvedValue({ query: `{
vehicles (first:10) {
totalCount
}
}`});

const devResponse = await CustomQuery(RESOURCE, DimoEnvironment.Dev.Identity, PARAM);
const prodResponse = await CustomQuery(RESOURCE, DimoEnvironment.Production.Identity, PARAM);

// Assertion - Check if the response data is defined
expect(devResponse.data).toBeDefined();
expect(prodResponse.data).toBeDefined();
});

test('Missing Required Query Parameter - Throws Error', async () => {
// Mock input data with missing required query parameter
const devResource = {
Query: 'POST',
path: '',
queryParams: { expectedParam: true },
};
const prodResource = {
Query: 'POST',
path: '',
queryParams: { expectedParam: true },
}
const params = { unexpectedParam: 'value1' };

// Call the Query function and expect it to throw an error
await expect(Query(devResource, DimoEnvironment.Dev.Identity, params)).rejects.toThrow(DimoError);
await expect(Query(prodResource, DimoEnvironment.Production.Identity, params)).rejects.toThrow(DimoError);
await expect(Query(devResource, DimoEnvironment.Dev.Telemetry, params)).rejects.toThrow(DimoError);
await expect(Query(prodResource, DimoEnvironment.Production.Telemetry, params)).rejects.toThrow(DimoError);
});
});

0 comments on commit 94bad6b

Please sign in to comment.