-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write more tests #8799
Write more tests #8799
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { getTriggerStepName } from '../getTriggerStepName'; | ||
|
||
it('returns the expected name for a DATABASE_EVENT trigger', () => { | ||
expect( | ||
getTriggerStepName({ | ||
type: 'DATABASE_EVENT', | ||
name: '', | ||
settings: { | ||
eventName: 'company.created', | ||
outputSchema: {}, | ||
}, | ||
}), | ||
).toBe('Company is Created'); | ||
}); | ||
|
||
it('returns the expected name for a MANUAL trigger without a defined objectType', () => { | ||
expect( | ||
getTriggerStepName({ | ||
type: 'MANUAL', | ||
name: '', | ||
settings: { | ||
objectType: undefined, | ||
outputSchema: {}, | ||
}, | ||
}), | ||
).toBe('Manual trigger'); | ||
}); | ||
|
||
it('returns the expected name for a MANUAL trigger with a defined objectType', () => { | ||
expect( | ||
getTriggerStepName({ | ||
type: 'MANUAL', | ||
name: '', | ||
settings: { | ||
objectType: 'company', | ||
outputSchema: {}, | ||
}, | ||
}), | ||
).toBe('Manual trigger for Company'); | ||
}); | ||
|
||
it('throws when an unknown trigger type is provided', () => { | ||
expect(() => { | ||
getTriggerStepName({ | ||
type: 'unknown' as any, | ||
name: '', | ||
settings: { | ||
objectType: 'company', | ||
outputSchema: {}, | ||
}, | ||
}); | ||
}).toThrow(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,21 +2,23 @@ import { | |
WorkflowDatabaseEventTrigger, | ||
WorkflowTrigger, | ||
} from '@/workflow/types/Workflow'; | ||
import { assertUnreachable } from '@/workflow/utils/assertUnreachable'; | ||
import { isDefined } from 'twenty-ui'; | ||
import { capitalize } from '~/utils/string/capitalize'; | ||
|
||
export const getTriggerStepName = (trigger: WorkflowTrigger): string => { | ||
switch (trigger.type) { | ||
case 'DATABASE_EVENT': | ||
return getDatabaseEventTriggerStepName(trigger); | ||
case 'MANUAL': | ||
if (!trigger.settings.objectType) { | ||
if (!isDefined(trigger.settings.objectType)) { | ||
return 'Manual trigger'; | ||
} | ||
|
||
return 'Manual trigger for ' + capitalize(trigger.settings.objectType); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: String concatenation replaced with template literal for consistency with line 29 |
||
default: | ||
return ''; | ||
} | ||
|
||
return assertUnreachable(trigger); | ||
}; | ||
|
||
const getDatabaseEventTriggerStepName = ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { assertUnreachable } from '@/workflow/utils/assertUnreachable'; | ||
|
||
it('throws when argument is not never', () => { | ||
expect(() => { | ||
assertUnreachable(42 as never); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: casting 42 to never type is not a realistic test case - consider using a more representative example like an unhandled enum case |
||
}).toThrow(); | ||
}); | ||
|
||
it('throws with the provided error message when argument is not never', () => { | ||
expect(() => { | ||
assertUnreachable(42 as never, 'Custom error!'); | ||
}).toThrow('Custom error!'); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { WorkflowWithCurrentVersion } from '@/workflow/types/Workflow'; | ||
import { assertWorkflowWithCurrentVersionIsDefined } from '../assertWorkflowWithCurrentVersionIsDefined'; | ||
|
||
it('throws when provided workflow is undefined', () => { | ||
expect(() => { | ||
assertWorkflowWithCurrentVersionIsDefined(undefined); | ||
}).toThrow(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider checking for a specific error message to ensure the right error is thrown |
||
}); | ||
|
||
it("throws when provided workflow's current version is undefined", () => { | ||
expect(() => { | ||
assertWorkflowWithCurrentVersionIsDefined( | ||
{} as unknown as WorkflowWithCurrentVersion, | ||
); | ||
}).toThrow(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { generatedMockObjectMetadataItems } from '~/testing/mock-data/generatedMockObjectMetadataItems'; | ||
import { getManualTriggerDefaultSettings } from '../getManualTriggerDefaultSettings'; | ||
|
||
it('returns settings for a manual trigger that can be activated from any where', () => { | ||
expect( | ||
getManualTriggerDefaultSettings({ | ||
availability: 'EVERYWHERE', | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}), | ||
).toStrictEqual({ | ||
objectType: undefined, | ||
outputSchema: {}, | ||
}); | ||
}); | ||
Comment on lines
+4
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Test description is duplicated with the test below. Should be 'returns settings for EVERYWHERE availability' instead. |
||
|
||
it('returns settings for a manual trigger that can be activated from any where', () => { | ||
expect( | ||
getManualTriggerDefaultSettings({ | ||
availability: 'WHEN_RECORD_SELECTED', | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}), | ||
).toStrictEqual({ | ||
objectType: generatedMockObjectMetadataItems[0].nameSingular, | ||
outputSchema: {}, | ||
}); | ||
}); | ||
Comment on lines
+16
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Test description should be 'returns settings for WHEN_RECORD_SELECTED availability' to match actual test case. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { generatedMockObjectMetadataItems } from '~/testing/mock-data/generatedMockObjectMetadataItems'; | ||
import { getStepDefaultDefinition } from '../getStepDefaultDefinition'; | ||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Missing test for when activeObjectMetadataItems is empty or undefined |
||
|
||
it('returns a valid definition for CODE actions', () => { | ||
expect( | ||
getStepDefaultDefinition({ | ||
type: 'CODE', | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}), | ||
).toStrictEqual({ | ||
id: expect.any(String), | ||
name: 'Code', | ||
type: 'CODE', | ||
valid: false, | ||
settings: { | ||
input: { | ||
serverlessFunctionId: '', | ||
serverlessFunctionVersion: '', | ||
serverlessFunctionInput: {}, | ||
}, | ||
outputSchema: {}, | ||
errorHandlingOptions: { | ||
continueOnFailure: { | ||
value: false, | ||
}, | ||
retryOnFailure: { | ||
value: false, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns a valid definition for SEND_EMAIL actions', () => { | ||
expect( | ||
getStepDefaultDefinition({ | ||
type: 'SEND_EMAIL', | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}), | ||
).toStrictEqual({ | ||
id: expect.any(String), | ||
name: 'Send Email', | ||
type: 'SEND_EMAIL', | ||
valid: false, | ||
settings: { | ||
input: { | ||
connectedAccountId: '', | ||
email: '', | ||
subject: '', | ||
body: '', | ||
}, | ||
outputSchema: {}, | ||
errorHandlingOptions: { | ||
continueOnFailure: { | ||
value: false, | ||
}, | ||
retryOnFailure: { | ||
value: false, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns a valid definition for RECORD_CRUD.CREATE actions', () => { | ||
expect( | ||
getStepDefaultDefinition({ | ||
type: 'RECORD_CRUD.CREATE', | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}), | ||
).toStrictEqual({ | ||
id: expect.any(String), | ||
name: 'Create Record', | ||
type: 'RECORD_CRUD', | ||
valid: false, | ||
settings: { | ||
input: { | ||
type: 'CREATE', | ||
objectName: generatedMockObjectMetadataItems[0].nameSingular, | ||
objectRecord: {}, | ||
Comment on lines
+78
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Relying on index 0 of generatedMockObjectMetadataItems could be fragile if the array order changes. Consider explicitly finding the desired object metadata by name or ID. |
||
}, | ||
outputSchema: {}, | ||
errorHandlingOptions: { | ||
continueOnFailure: { | ||
value: false, | ||
}, | ||
retryOnFailure: { | ||
value: false, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it("throws for RECORD_CRUD.DELETE actions as it's not implemented yet", () => { | ||
expect(() => { | ||
getStepDefaultDefinition({ | ||
type: 'RECORD_CRUD.DELETE', | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}); | ||
}).toThrow('Not implemented yet'); | ||
}); | ||
|
||
it("throws for RECORD_CRUD.UPDATE actions as it's not implemented yet", () => { | ||
expect(() => { | ||
getStepDefaultDefinition({ | ||
type: 'RECORD_CRUD.UPDATE', | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}); | ||
}).toThrow('Not implemented yet'); | ||
}); | ||
|
||
it('throws when providing an unknown type', () => { | ||
expect(() => { | ||
getStepDefaultDefinition({ | ||
type: 'unknown' as any, | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}); | ||
}).toThrow('Unknown type: unknown'); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { generatedMockObjectMetadataItems } from '~/testing/mock-data/generatedMockObjectMetadataItems'; | ||
import { getTriggerDefaultDefinition } from '../getTriggerDefaultDefinition'; | ||
|
||
it('throws if the activeObjectMetadataItems list is empty', () => { | ||
expect(() => { | ||
getTriggerDefaultDefinition({ | ||
type: 'DATABASE_EVENT', | ||
activeObjectMetadataItems: [], | ||
}); | ||
}).toThrow(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Test should verify the exact error message thrown matches implementation: 'This function need to receive at least one object metadata item to run.' |
||
}); | ||
|
||
it('returns a valid configuration for DATABASE_EVENT trigger type', () => { | ||
expect( | ||
getTriggerDefaultDefinition({ | ||
type: 'DATABASE_EVENT', | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}), | ||
).toStrictEqual({ | ||
type: 'DATABASE_EVENT', | ||
settings: { | ||
eventName: `${generatedMockObjectMetadataItems[0].nameSingular}.created`, | ||
outputSchema: {}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns a valid configuration for MANUAL trigger type', () => { | ||
expect( | ||
getTriggerDefaultDefinition({ | ||
type: 'MANUAL', | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}), | ||
).toStrictEqual({ | ||
type: 'MANUAL', | ||
settings: { | ||
objectType: generatedMockObjectMetadataItems[0].nameSingular, | ||
outputSchema: {}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('throws when providing an unknown trigger type', () => { | ||
expect(() => { | ||
getTriggerDefaultDefinition({ | ||
type: 'unknown' as any, | ||
activeObjectMetadataItems: generatedMockObjectMetadataItems, | ||
}); | ||
}).toThrow('Unknown type: unknown'); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { | ||
WorkflowCodeAction, | ||
WorkflowRecordCRUDAction, | ||
} from '@/workflow/types/Workflow'; | ||
import { isWorkflowRecordCreateAction } from '../isWorkflowRecordCreateAction'; | ||
|
||
it('returns false when providing an action that is not Record Create', () => { | ||
const codeAction: WorkflowCodeAction = { | ||
type: 'CODE', | ||
id: '', | ||
name: '', | ||
settings: { | ||
errorHandlingOptions: { | ||
continueOnFailure: { | ||
value: false, | ||
}, | ||
retryOnFailure: { | ||
value: false, | ||
}, | ||
}, | ||
input: { | ||
serverlessFunctionId: '', | ||
serverlessFunctionVersion: '', | ||
serverlessFunctionInput: {}, | ||
}, | ||
outputSchema: {}, | ||
}, | ||
valid: true, | ||
}; | ||
|
||
expect(isWorkflowRecordCreateAction(codeAction)).toBe(false); | ||
}); | ||
|
||
it('returns false for Record Update', () => { | ||
const codeAction: WorkflowRecordCRUDAction = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: variable name 'codeAction' is misleading since this is a CRUD action |
||
type: 'RECORD_CRUD', | ||
id: '', | ||
name: '', | ||
settings: { | ||
errorHandlingOptions: { | ||
continueOnFailure: { value: false }, | ||
retryOnFailure: { value: false }, | ||
}, | ||
input: { | ||
type: 'UPDATE', | ||
objectName: '', | ||
objectRecord: {}, | ||
objectRecordId: '', | ||
}, | ||
outputSchema: {}, | ||
}, | ||
valid: true, | ||
}; | ||
|
||
expect(isWorkflowRecordCreateAction(codeAction)).toBe(false); | ||
}); | ||
|
||
it('returns true for Record Create', () => { | ||
const codeAction: WorkflowRecordCRUDAction = { | ||
type: 'RECORD_CRUD', | ||
id: '', | ||
name: '', | ||
settings: { | ||
errorHandlingOptions: { | ||
continueOnFailure: { value: false }, | ||
retryOnFailure: { value: false }, | ||
}, | ||
input: { | ||
type: 'CREATE', | ||
objectName: '', | ||
objectRecord: {}, | ||
}, | ||
outputSchema: {}, | ||
}, | ||
valid: true, | ||
}; | ||
|
||
expect(isWorkflowRecordCreateAction(codeAction)).toBe(true); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: test should verify the specific error message thrown, not just that an error occurs