-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bc9068
commit 26bcbd1
Showing
1 changed file
with
61 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |