Skip to content

Commit

Permalink
feat: done with user,data request template mocks and stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Dec 12, 2023
1 parent 38a604f commit 25e2a58
Show file tree
Hide file tree
Showing 15 changed files with 414 additions and 246 deletions.
23 changes: 23 additions & 0 deletions __mocks__/dataRequestTemplate.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DataRequestTemplate } from '../src/dataRequestsTemplate/dataRequestsTemplate';
import { dataRequestTemplateStub } from '../test/stubs/dataRequestTemplate.stub';

export const dataRequestTemplateMockService = (dataRequestTemplate: DataRequestTemplate) => ({
createDataRequestTemplateMock: jest
.spyOn(dataRequestTemplate.sdk, 'createDataRequestTemplate_mutation')
.mockResolvedValue({
createDataRequestTemplate: dataRequestTemplateStub(),
}),
getDataRequestTemplateMock: jest
.spyOn(dataRequestTemplate.sdk, 'dataRequestTemplate_query')
.mockResolvedValue({
dataRequestTemplate: dataRequestTemplateStub(),
}),
getDataRequestTemplatesMock: jest
.spyOn(dataRequestTemplate.sdk, 'dataRequestTemplates_query')
.mockResolvedValue({ dataRequestTemplates: [dataRequestTemplateStub()] }),
getDataRequestsTemplateCount: jest
.spyOn(dataRequestTemplate.sdk, 'dataRequestTemplatesCount_query')
.mockResolvedValue({
dataRequestTemplatesCount: 10,
}),
});
27 changes: 27 additions & 0 deletions __mocks__/organization.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Organization } from '../src/organization/organization';
import { organizationStub } from '../test/stubs/organization.stub';

export const OrganizationMockService = (organization: Organization) => ({
createOrganizationMock: jest
.spyOn(organization.sdk, 'createOrganization_mutation')
.mockResolvedValue({
createOrganization: organizationStub(),
}),
getOrganizationMock: jest
.spyOn(organization.sdk, 'organization_query')
.mockResolvedValue({
organization: organizationStub(),
}),
updateOrganizationMock: jest
.spyOn(organization.sdk, 'updateOrganization_mutation')
.mockResolvedValue({
updateOrganization: organizationStub({
description: 'updated description',
}),
}),
getOrganizationsMock: jest
.spyOn(organization.sdk, 'organizations_query')
.mockResolvedValue({
organizations: [organizationStub()],
}),
});
8 changes: 3 additions & 5 deletions __mocks__/pda.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { PDA } from '../src/pda/pda';
import { pdaStub } from '../test/stubs/pda.stub';

export const PDAMockService = (pda: PDA) => ({
createPDAMutationMock: jest
.spyOn(pda.sdk, 'createPDA_mutation')
.mockResolvedValue({
createPDA: pdaStub(),
}),
createPDAMock: jest.spyOn(pda.sdk, 'createPDA_mutation').mockResolvedValue({
createPDA: pdaStub(),
}),
changePDAStatusMock: jest
.spyOn(pda.sdk, 'changePDAStatus_mutation')
.mockResolvedValue({
Expand Down
25 changes: 25 additions & 0 deletions __mocks__/user.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Organization } from '../src/organization/organization';
import { User } from '../src/user/user';
import { organizationStub } from '../test/stubs/organization.stub';
import { userStub } from '../test/stubs/user.stub';

export const UserMockService = (user: User) => ({
meMock: jest.spyOn(user.sdk, 'me_query').mockResolvedValue({
me: userStub(),
}),
getSingleUserMock: jest.spyOn(user.sdk, 'user_query').mockResolvedValue({
user: userStub(),
}),
updateOrganizationMock: jest
.spyOn(organization.sdk, 'updateOrganization_mutation')
.mockResolvedValue({
updateOrganization: organizationStub({
description: 'updated description',
}),
}),
getOrganizationsMock: jest
.spyOn(organization.sdk, 'organizations_query')
.mockResolvedValue({
organizations: [organizationStub()],
}),
});
5 changes: 4 additions & 1 deletion src/dataRequestsTemplate/dataRequestsTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {
TemplateSchemaInput,
} from '../../.mesh';
import { errorHandler } from '../utils/errorHandler';
import { isUUIDValid, validateObjectProperties } from '../utils/validators';

export class DataRequestTemplate {
private sdk: Sdk;
public sdk: Sdk;

constructor(sdk: Sdk) {
this.sdk = sdk;
Expand All @@ -22,6 +23,7 @@ export class DataRequestTemplate {
*/
async createDataRequestTemplate(templateInput: TemplateSchemaInput) {
try {
validateObjectProperties(templateInput);
return await this.sdk.createDataRequestTemplate_mutation({
input: templateInput,
});
Expand All @@ -39,6 +41,7 @@ export class DataRequestTemplate {
*/
async getDataRequestTemplate(id: string) {
try {
isUUIDValid(id);
return await this.sdk.dataRequestTemplate_query({ id });
} catch (error) {
throw new Error(errorHandler(error));
Expand Down
6 changes: 5 additions & 1 deletion src/organization/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
} from '../../.mesh';
import { OrganizationIdentifierType } from '../types';
import { errorHandler } from '../utils/errorHandler';
import { isStringValid, validateObjectProperties } from '../utils/validators';

export class Organization {
private sdk: Sdk;
public sdk: Sdk;

constructor(sdk: Sdk) {
this.sdk = sdk;
Expand All @@ -28,6 +29,7 @@ export class Organization {
*/
async createOrganization(organizationInput: CreateOrganizationInput) {
try {
validateObjectProperties(organizationInput);
return await this.sdk.createOrganization_mutation({
input: organizationInput,
});
Expand Down Expand Up @@ -97,6 +99,7 @@ export class Organization {
*/
async updateOrganization(updatedOrganization: UpdateOrganizationInput) {
try {
validateObjectProperties(updatedOrganization);
return await this.sdk.updateOrganization_mutation({
input: updatedOrganization,
});
Expand All @@ -118,6 +121,7 @@ export class Organization {
*/
async getOrganization(type: OrganizationIdentifierType, value: string) {
try {
isStringValid(value);
return await this.sdk.organization_query({ input: { type, value } });
} catch (error) {
throw new Error(errorHandler(error));
Expand Down
12 changes: 11 additions & 1 deletion src/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import {
} from '../../.mesh';
import { PDAFilter, UserIdentifierType } from '../types';
import { errorHandler } from '../utils/errorHandler';
import {
isEmailValid,
isStringValid,
validateObjectProperties,
} from '../utils/validators';

export class User {
private sdk: Sdk;
public sdk: Sdk;

constructor(sdk: Sdk) {
this.sdk = sdk;
Expand Down Expand Up @@ -43,6 +48,7 @@ export class User {
value: string;
}) {
try {
isStringValid(value);
return await this.sdk.user_query({ input: { type, value } });
} catch (error) {
throw new Error(errorHandler(error));
Expand Down Expand Up @@ -124,6 +130,7 @@ export class User {
*/
async updateUser(updatedUser: UpdateUserInput) {
try {
validateObjectProperties(updatedUser);
return await this.sdk.updateUser_mutation({ input: updatedUser });
} catch (error) {
throw new Error(errorHandler(error));
Expand All @@ -140,6 +147,7 @@ export class User {
*/
async updateMyDisplayName(displayName: string) {
try {
isStringValid(displayName);
return await this.sdk.updateMyDisplayName_mutation({ displayName });
} catch (error) {
throw new Error(errorHandler(error));
Expand All @@ -156,6 +164,7 @@ export class User {
*/
async updateMyGatewayId(gatewayId: string) {
try {
isStringValid(gatewayId);
return await this.sdk.updateMyGatewayId_mutation({ gatewayId });
} catch (error) {
throw new Error(errorHandler(error));
Expand Down Expand Up @@ -187,6 +196,7 @@ export class User {
*/
async updateNotificationEmail(email: string) {
try {
isEmailValid(email);
return (await this.sdk.updateNotificationEmail_mutation({ email }))
.updateNotificationEmail;
} catch (error) {
Expand Down
1 change: 0 additions & 1 deletion test/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Auth } from '../src/auth/auth';
import { getMeshSDK } from '../.mesh';
import { Chain } from '../src/types';
import { AuthMockService } from '../__mocks__/auth.mock';
import { authStub } from './stubs/auth.stub';

Expand Down
133 changes: 65 additions & 68 deletions test/dataRequestsTemplate.test.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,70 @@
// import dotenv from 'dotenv';
// import { Gateway } from '../src/Gateway';
// dotenv.config();
import { getMeshSDK } from '../.mesh';
import { dataRequestTemplateMockService } from '../__mocks__/dataRequestTemplate.mock';
import { DataRequestTemplate } from '../src/dataRequestsTemplate/dataRequestsTemplate';
import {
dataRequestTemplateCreateStub,
dataRequestTemplateStub,
} from './stubs/dataRequestTemplate.stub';

// let api: Gateway;
// const DEFAULT_TIMEOUT = 10000;
let dataRequestTemplate: DataRequestTemplate;

// beforeAll(() => {
// api = new Gateway({
// apiKey: process.env.API_KEY!,
// token: process.env.BEARER_TOKEN!,
// });
// });
beforeAll(() => {
dataRequestTemplate = new DataRequestTemplate(getMeshSDK());
});

// describe('Data Requests Template test', () => {
// it(
// 'create data request template',
// async () => {
// const { createDataRequestTemplate } =
// await api.dataRequestTemplate.createDataRequestTemplate({
// title: 'Create Data Request Template Example',
// description: 'Lorem ipsum dolor sit amet.',
// dataModels: [
// {
// id: process.env.DATAMODEL_ID!,
// required: true,
// claimValidations: {
// type: 'object',
// properties: {
// gatewayUse: {
// type: 'string',
// },
// },
// required: ['gatewayUse'],
// },
// },
// ],
// });
// expect(createDataRequestTemplate.name).toEqual(
// 'Create Data Request Template Example',
// );
// const { dataRequestTemplate } =
// await api.dataRequestTemplate.getDataRequestTemplate(
// createDataRequestTemplate.id,
// );
// expect(dataRequestTemplate).toBeDefined();
// },
// DEFAULT_TIMEOUT,
// );
afterAll(() => {
jest.resetAllMocks();
});

// it(
// 'get data request templates',
// async () => {
// const { dataRequestTemplates } =
// await api.dataRequestTemplate.getDataRequestTemplates({
// skip: 0,
// take: 10,
// });
// expect(dataRequestTemplates?.length).toBeGreaterThan(0);
// },
// DEFAULT_TIMEOUT,
// );
describe('Data Requests Template test', () => {
it('create data request template', async () => {
const { createDataRequestTemplateMock } =
dataRequestTemplateMockService(dataRequestTemplate);

// it(
// 'get data request templates count',
// async () => {
// const count =
// await api.dataRequestTemplate.getDataRequestsTemplateCount();
// expect(count).toBeGreaterThan(0);
// },
// DEFAULT_TIMEOUT,
// );
// });
const { createDataRequestTemplate } =
await dataRequestTemplate.createDataRequestTemplate(
dataRequestTemplateCreateStub(),
);

expect(createDataRequestTemplate.name).toEqual(
dataRequestTemplateCreateStub().title,
);
expect(createDataRequestTemplateMock).toHaveBeenCalled();
});

it('get data request template by id', async () => {
const { getDataRequestTemplateMock } =
dataRequestTemplateMockService(dataRequestTemplate);

const res = await dataRequestTemplate.getDataRequestTemplate(
dataRequestTemplateStub().id,
);

expect(res.dataRequestTemplate?.id).toEqual(dataRequestTemplateStub().id);
expect(getDataRequestTemplateMock).toHaveBeenCalled();
});

it('get data request templates', async () => {
const { getDataRequestTemplatesMock } =
dataRequestTemplateMockService(dataRequestTemplate);

const { dataRequestTemplates } =
await dataRequestTemplate.getDataRequestTemplates({
skip: 0,
take: 10,
});

expect(dataRequestTemplates?.length).toBeGreaterThan(0);
expect(getDataRequestTemplatesMock).toHaveBeenCalled();
});

it('get data request templates count', async () => {
const { getDataRequestsTemplateCount } =
dataRequestTemplateMockService(dataRequestTemplate);

const count = await dataRequestTemplate.getDataRequestsTemplateCount();

expect(count).toBeGreaterThan(0);
expect(getDataRequestsTemplateCount).toHaveBeenCalled();
});
});
Loading

0 comments on commit 25e2a58

Please sign in to comment.