Вы находитесь здесь:
Пример набора тестов для настраиваемых сценариев Life Sciences
Этот пример предоставляет пакет тестов для настраиваемого сценария в Life Sciences Customer Engagement.
Данный пример пакета тестов Jest предназначен для тестирования образца настраиваемого сценария в разделе «Рекомендации по настраиваемым сценариям Life Sciences».
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',
}]);
});
});
