Skip to content

Commit

Permalink
Unit testing + simplify API mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesliupenn committed May 29, 2024
1 parent 2b7e1a8 commit 2244b27
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 28 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ or use [yarn](https://classic.yarnpkg.com/en/package/@dimo-network/dimo-node-sdk
yarn add @dimo-network/dimo-node-sdk
```

## Unit Testing
Run `npm test` or `npm run test` to execute the Jest tests.

## API Documentation
Please visit the DIMO [Developer Documentation](https://docs.dimo.zone/developer-platform) to learn more about building on DIMO and detailed information on the API.

Expand Down
16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest', // Transform TypeScript files
'^.+\\.mjs$': 'babel-jest', // Transform .mjs files using Babel
'^.+\\.js$': 'babel-jest', // Transform JavaScript files
},
transformIgnorePatterns: ['<rootDir>/node_modules/'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node', 'mjs'],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1' // Adjust this if you're using path aliases
}
};
115 changes: 111 additions & 4 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions 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.4",
"version": "1.1.5",
"description": "DIMO SDK for JavaScript",
"main": "dist/index.js",
"author": "James Li",
Expand Down Expand Up @@ -29,12 +29,14 @@
},
"devDependencies": {
"@types/dotenv-safe": "^8.1.6",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.25",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.29.1",
"jest": "^29.7.0",
"typescript": "^5.4.2"
"ts-jest": "^29.1.4",
"typescript": "^5.4.5"
},
"types": "./dist/index.d.ts"
}
120 changes: 120 additions & 0 deletions src/api/Method.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import axios from 'axios';
import { Method } from './Method'; // Import the Method function to be tested
import { DimoError } from '../errors';
import { DimoEnvironment } from '../environments';

const PROD = 'Production';
const DEV = 'Dev';
const RESOURCE = {
method: 'GET',
path: '',
queryParams: { param1: true },
};
const PARAM = { param1: 'value1' };

describe('Method Function', () => {
test('Valid API Call - Device Data API Server is up and returning data', async () => {
jest.spyOn(axios, 'request').mockResolvedValue({ data: { key: 'value' } });

const devResponse = await Method(RESOURCE, DimoEnvironment.Dev.DeviceData, PARAM, DEV);
const prodResponse = await Method(RESOURCE, DimoEnvironment.Production.DeviceData, PARAM, PROD);

// Assertion - Check if the response data is returned correctly
expect(devResponse).toEqual({ code: 200, message: 'Server is up.' });
expect(prodResponse).toEqual({ code: 200, message: 'Server is up.' });
});

test('Valid API Call - Device Definitions API Server is up and returning data', async () => {
jest.spyOn(axios, 'request').mockResolvedValue({ data: { key: 'value' } });

const devResponse = await Method(RESOURCE, DimoEnvironment.Dev.DeviceDefinitions, PARAM, DEV);
const prodResponse = await Method(RESOURCE, DimoEnvironment.Production.DeviceDefinitions, PARAM, PROD);

// Assertion - Check if the response data is returned correctly
expect(devResponse).toEqual('device definitions api running!');
expect(prodResponse).toEqual('device definitions api running!');
});


test('Valid API Call - Devices API Server is up and returning data', async () => {
jest.spyOn(axios, 'request').mockResolvedValue({ data: { key: 'value' } });

const devResponse = await Method(RESOURCE, DimoEnvironment.Dev.Devices, PARAM, DEV);
const prodResponse = await Method(RESOURCE, DimoEnvironment.Production.Devices, PARAM, PROD);

// Assertion - Check if the response data is returned correctly
expect(devResponse).toEqual({ data: 'Server is up and running' });
expect(prodResponse).toEqual({ data: 'Server is up and running' });
});

test('Valid API Call - Events API Server is up and returning data', async () => {
jest.spyOn(axios, 'request').mockResolvedValue({ data: { key: 'value' } });

const devResponse = await Method(RESOURCE, DimoEnvironment.Dev.Events, PARAM, DEV);
const prodResponse = await Method(RESOURCE, DimoEnvironment.Production.Events, PARAM, PROD);

// Assertion - Check if the response data is returned correctly
expect(devResponse).toEqual({ data: 'Server is up and running' });
expect(prodResponse).toEqual({ data: 'Server is up and running' });
});

test('Valid API Call - Token Exchange API Server is up and returning data', async () => {
jest.spyOn(axios, 'request').mockResolvedValue({ data: { key: 'value' } });

const devResponse = await Method(RESOURCE, DimoEnvironment.Dev.TokenExchange, PARAM, DEV);
const prodResponse = await Method(RESOURCE, DimoEnvironment.Production.TokenExchange, PARAM, PROD);

// Assertion - Check if the response data is returned correctly
expect(devResponse).toEqual({ data: 'Server is up and running' });
expect(prodResponse).toEqual({ data: 'Server is up and running' });
});

test('Valid API Call - Users API Server is up and returning data', async () => {
jest.spyOn(axios, 'request').mockResolvedValue({ data: { key: 'value' } });

const devResponse = await Method(RESOURCE, DimoEnvironment.Dev.User, PARAM, DEV);
const prodResponse = await Method(RESOURCE, DimoEnvironment.Production.User, PARAM, PROD);

// Assertion - Check if the response data is returned correctly
expect(devResponse).toEqual({ data: 'Server is up and running' });
expect(prodResponse).toEqual({ data: 'Server is up and running' });
});

test('Valid API Call - Valuations API Server is up and returning data', async () => {
jest.spyOn(axios, 'request').mockResolvedValue({ data: { key: 'value' } });

const devResponse = await Method(RESOURCE, DimoEnvironment.Dev.Valuations, PARAM, DEV);
const prodResponse = await Method(RESOURCE, DimoEnvironment.Production.Valuations, PARAM, PROD);

// Assertion - Check if the response data is returned correctly
expect(devResponse).toEqual({ code: 200, message: 'Server is up.' });
expect(prodResponse).toEqual({ code: 200, message: 'Server is up.' });
});

test('Valid API Call - Vehicle Signal Decoding API Server is up and returning data', async () => {
jest.spyOn(axios, 'request').mockResolvedValue({ data: { key: 'value' } });

const devResponse = await Method(RESOURCE, DimoEnvironment.Dev.VehicleSignalDecoding, PARAM, DEV);
const prodResponse = await Method(RESOURCE, DimoEnvironment.Production.VehicleSignalDecoding, PARAM, PROD);

// Assertion - Check if the response data is returned correctly
expect(devResponse).toEqual('healthy');
expect(prodResponse).toEqual('healthy');
});


test('Missing Required Query Parameter - Throws Error', async () => {
// Mock input data with missing required query parameter
const resource = {
method: 'GET',
path: '/example/endpoint',
queryParams: { expectedParam: true }, // Expect expectedParam
};
const baseUrl = 'https://example.com/api';
const params = { unexpectedParam: 'value1' };

// Call the Method function and expect it to throw an error
await expect(Method(resource, baseUrl, params, DEV)).rejects.toThrowError(DimoError);
await expect(Method(resource, baseUrl, params, PROD)).rejects.toThrowError(DimoError);
});
});
8 changes: 4 additions & 4 deletions src/api/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export interface Resource {
}

export class Resource {
protected api: any;
protected resourceName: any;
protected env: any;
public api: any;
public resourceName: any;
public env: any;

constructor(api: any, resourceName: string, env: keyof typeof DimoEnvironment ) {
this.api = api;
Expand All @@ -20,7 +20,7 @@ export class Resource {
Object.keys(resources).forEach(key => {
this[key] = (params: any = {}) => Method(
resources[key], // Setup the endpoint resources
this.api[this.resourceName], // Setup the base URL
this.api, // Setup the base URL
params, // Pass through the params
this.env // Identiy the environment
);
Expand Down
Loading

0 comments on commit 2244b27

Please sign in to comment.