Skip to content

Commit

Permalink
Format mock-integration, basically.
Browse files Browse the repository at this point in the history
  • Loading branch information
seanspeaks committed Oct 17, 2024
1 parent 9bc9068 commit 26bcbd1
Showing 1 changed file with 61 additions and 56 deletions.
117 changes: 61 additions & 56 deletions packages/devtools/test/mock-integration.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,88 @@
const { Auther, Credential, Entity, IntegrationFactory, createObjectId } = require('@friggframework/core');
const {
Auther,
Credential,
Entity,
IntegrationFactory,
createObjectId,
} = require('@friggframework/core');


async function createMockIntegration(IntegrationClassDef, userId = null, config = {},) {
const integrationFactory = new IntegrationFactory([IntegrationClassDef]);
async function createMockIntegration(
IntegrationClass,
userId = null,
config = { type: IntegrationClass.Definition.name }
) {
const integrationFactory = new IntegrationFactory([IntegrationClass]);
userId = userId || createObjectId();

const insertOptions = {
new: true,
upsert: true,
setDefaultsOnInsert: true,
}
const user = {user: userId}

const credential = await Credential.findOneAndUpdate(
user,
{ $set: user },
insertOptions
);
const entity1 = await Entity.findOneAndUpdate(
user,
{
$set: {
credential: credential.id,
user: userId,
name: 'Test user',
externalId: '1234567890123456',
},
},
insertOptions
);
const entity2 = await Entity.findOneAndUpdate(
user,
{
$set: {
credential: credential.id,
user: userId,
},
},
insertOptions
);
};
const user = { user: userId };

const entities = [entity1, entity2]

const integration =
await integrationFactory.createIntegration(
entities,
userId,
config,
const entities = [];
for (const moduleName in IntegrationClass.modules) {
const ModuleDef = IntegrationClass.Definition.modules[moduleName];
const module = await Auther.getInstance({
definition: ModuleDef,
userId: userId,
});
const credential = await module.CredentialModel.findOneAndUpdate(
user,
{ $set: user },
insertOptions
);
entities.push(
(
await module.EntityModel.findOneAndUpdate(
user,
{
$set: {
credential,
user: userId,
name: `Test ${moduleName}`,
externalId: `1234567890123456_${moduleName}`,
},
},
insertOptions
)
).id
);
}

integration.id = integration.record._id
const integration = await integrationFactory.createIntegration(
entities,
userId,
config
);

for (const i in entities){
if (Object.entries(IntegrationClassDef.modules).length <= i) break
const [moduleName, ModuleDef] = Object.entries(IntegrationClassDef.modules)[i];
const module = await Auther.getInstance({definition: ModuleDef, userId: userId})
module.entity = entities[i];
integration[moduleName] = module;
}
integration.id = integration.record._id;

return integration
return integration;
}

function createMockApiObject(jest, api = {}, mockMethodMap) {
// take in an api class and object with keys that are method names
// and values which are the mock response (or implementation)
const clone = (data) => JSON.parse(JSON.stringify(data));

for (const [methodName, mockDataOrImplementation] of Object.entries(mockMethodMap)) {
for (const [methodName, mockDataOrImplementation] of Object.entries(
mockMethodMap
)) {
if (mockDataOrImplementation instanceof Function) {
api[methodName] = jest.fn(mockDataOrImplementation);
}
else if (api[methodName]?.constructor?.name === "AsyncFunction") {
api[methodName] = jest.fn().mockResolvedValue(clone(mockDataOrImplementation));
} else if (api[methodName]?.constructor?.name === 'AsyncFunction') {
api[methodName] = jest
.fn()
.mockResolvedValue(clone(mockDataOrImplementation));
} else {
api[methodName] = jest.fn().mockReturnValue(clone(mockDataOrImplementation));
api[methodName] = jest
.fn()
.mockReturnValue(clone(mockDataOrImplementation));
}
}
return api;
}

module.exports = {createMockIntegration, createMockApiObject};
module.exports = { createMockIntegration, createMockApiObject };

0 comments on commit 26bcbd1

Please sign in to comment.