Ti trovi qui:
Esempio di suite di test per script personalizzati per le scienze della vita
Questo esempio fornisce una suite di test per uno script personalizzato in Life Sciences Cloud per Customer Engagement.
Questa suite di test Jest di esempio è progettata per testare lo script personalizzato di esempio in Best Practices for Life Sciences Custom Scripts.
import fs from 'fs';
import path from 'path';
// Load the script
const scriptContent = fs.readFileSync(path.resolve(__dirname, '../inquiryQuestionsValidation.js'));
const scriptFunction = new Function('env', 'record', 'db', 'ConditionBuilder', 'FieldCondition', `return ${scriptContent.toString()}`);
// Mock the classes and variables that are used in the script
const mockEnv = {
getOption: jest.fn(),
};
const mockDb = {
query: jest.fn(),
};
const mockRecord = {
stringValue: jest.fn(),
};
const mockConditionBuilder = jest.fn().mockImplementation(() => ({
build: jest.fn().mockResolvedValue({}),
}));
const mockFieldCondition = jest.fn();
describe('inquiryQuestionsValidation', () => {
beforeEach(() => {
// Clear all mocks and reset modules to ensure a clean state
jest.clearAllMocks();
});
test('should return Inquiry Questions Added when there are inquiry questions', async () => {
// Mock the db.query method to return the desired value
mockDb.query.mockResolvedValue([{ Id: '1', Name: 'Question 1' }]);
// The scriptFunction returns an array containing a Promise
const promiseArray = scriptFunction(mockEnv, mockRecord, mockDb, mockConditionBuilder, mockFieldCondition);
// Wait for the Promise in the array to resolve
const result = await Promise.all(promiseArray);
// Assert that the result is as expected
expect(result).toEqual([{
title: 'Inquiry Questions Added',
status: 'success',
}]);
});
test('should return No Inquiry Questions Found when there are no inquiry questions', async () => {
// Mock the db.query method to return the desired value
mockDb.query.mockResolvedValue([]);
// The scriptFunction returns an array containing a Promise
const promiseArray = scriptFunction(mockEnv, mockRecord, mockDb, mockConditionBuilder, mockFieldCondition);
// Wait for the Promise in the array to resolve
const result = await Promise.all(promiseArray);
// Assert that the result is as expected
expect(result).toEqual([{
title: 'No Inquiry Questions Found',
status: 'error',
}]);
});
test('should return Caught Exception During Inquiry Questions Validation when there is an error', async () => {
// Mock the db.query method to return the desired value
mockDb.query.mockRejectedValue(new Error('Error'));
// The scriptFunction returns an array containing a Promise
const promiseArray = scriptFunction(mockEnv, mockRecord, mockDb, mockConditionBuilder, mockFieldCondition);
// Wait for the Promise in the array to resolve
const result = await Promise.all(promiseArray);
// Assert that the result is as expected
expect(result).toEqual([{
title: 'Caught Exception During Inquiry Questions Validation',
status: 'error',
}]);
});
});

