To create Jest tests for the createVariableValueValidation function using the Yup validation schema, we'll focus on a few scenarios to ensure that the validation logic works correctly. Here are some tests you can consider:
javascript
import * as yup from 'yup';
import { createVariableValueValidation } from './path/to/your/module';
import { GENERAL_REQUIRED_ERROR_MESSAGE } from '$ufs-common';
describe('createVariableValueValidation', () => {
const periodRateSubType = { label: 'Some Label', value: 'someValue' }; // Example instance of Labeled<RateSubTypes>
const validationSchema = createVariableValueValidation(periodRateSubType);
test('should pass validation with valid absoluteRate and rateValue', async () => {
const validData = {
absoluteRate: { id: 'ABSOLUTE_VALUE', name: 'Test Rate' },
rateValue: 10,
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
};
await expect(validationSchema.validate(validData)).resolves.toBe(validData);
});
test('should fail validation if rateValue is not a number when absoluteRate ID is ABSOLUTE_VALUE', async () => {
const invalidData = {
absoluteRate: { id: 'ABSOLUTE_VALUE', name: 'Test Rate' },
rateValue: null, // This should cause validation to fail
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
};
await expect(validationSchema.validate(invalidData)).rejects.toThrow(GENERAL_REQUIRED_ERROR_MESSAGE);
});
test('should not fail validation for rateValue if absoluteRate ID is not ABSOLUTE_VALUE', async () => {
const validData = {
absoluteRate: { id: 'OTHER_VALUE', name: 'Test Rate' },
rateValue: null, // This should be valid due to the ID check
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
};
await expect(validationSchema.validate(validData)).resolves.toBe(validData);
});
test('should fail validation if absoluteRate.id is missing', async () => {
const invalidData = {
absoluteRate: { name: 'Test Rate' },
rateValue: 10,
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
};
await expect(validationSchema.validate(invalidData)).rejects.toThrow();
});
// Add more tests for grossMargin, netMargin, eva, lgd validations if needed
});
User
import { number, object, string } from 'yup';
import { GENERAL_REQUIRED_ERROR_MESSAGE } from '$ufs-common';
import type { AbsoluteRateType } from '$interest-rates/entities/rates';
To test the variableIncreasedValueValidation schema you provided, we can write a set of Jest tests that cover various scenarios based on the validation logic. Below are the Jest tests that ensure our validation logic behaves correctly for both valid and invalid cases:
javascript
User
write test import { boolean, number, object, string } from 'yup';
import { GENERAL_REQUIRED_ERROR_MESSAGE } from '$ufs-common';
import type { BaseIndicatorTypeLabel } from '$interest-rates/entities/rates';
To create Jest tests for the floatingBaseIndicatorValidation schema, we'll cover various scenarios to ensure that each field is validated correctly according to the specified rules. Below are the Jest tests that implement these scenarios.
Jest Tests
javascript
User
import { number, object, string } from 'yup';
import { GENERAL_REQUIRED_ERROR_MESSAGE } from '$ufs-common';
import type { AbsoluteRateType, RateSubTypes } from '$interest-rates/entities/rates';
import { createSizeSpecialValueValidation } from '../../commons';
To create Jest tests for the createFloatingValueValidation schema, we want to ensure that the schema validates various properties correctly according to the specified rules. The following Jest tests cover a variety of scenarios to ensure that the validation logic behaves as expected.
Jest Tests
javascript
User
import type {
AbsoluteRateType,
RateSubTypes,
SpecialRateValue,
} from '$interest-rates/entities/rates';
To create Jest tests for the testSizeSpecialValues function, we need to cover various scenarios that consider the conditions specified within the function. The function aims to validate the presence of various special rate values based on the provided parameters, especially when certain conditions regarding absoluteRate and periodRateSubType are met.
Here are the Jest tests for your function:
Jest Tests
javascript
User
import { number, object, string } from 'yup';
import { GENERAL_REQUIRED_ERROR_MESSAGE } from '$ufs-common';
import type { AbsoluteRateType, RateSubTypes } from '$interest-rates/entities/rates';
import { createSizeSpecialValueValidation } from '../../commons';
To create Jest tests for the createEscrowValueValidation schema, we need to validate that the properties defined in the schema adhere to the specified validation rules. Below are Jest tests that cover various scenarios for this schema, similar to previous examples:
Valid Data Test: This test checks if valid data is accepted without any issues.
Invalid rateValue Test when absoluteRate.id is "ABSOLUTE_VALUE": This test ensures that if the absoluteRate.id is "ABSOLUTE_VALUE" and the rateValue is not a number (e.g., null), the validation fails.
Valid rateValue Test when absoluteRate.id is not "ABSOLUTE_VALUE": This verifies that if absoluteRate.id is not "ABSOLUTE_VALUE", the rateValue can be null without causing validation to fail.
Missing absoluteRate.id Test: This checks that if absoluteRate.id is absent, the validation fails.
You can expand on these tests based on your application's requirements and the specific behavior of the createSizeSpecialValueValidation function if it is also validating fields associated with it.
import * as yup from 'yup';
import { variableIncreasedValueValidation } from './path/to/your/module';
import { GENERAL_REQUIRED_ERROR_MESSAGE } from '$ufs-common';
describe('variableIncreasedValueValidation', () => {
const validAbsoluteRate = { id: 'ABSOLUTE_VALUE', name: 'Test Rate' };
const validIncrementType = { id: 'incrementTypeId', name: 'Increment Type Name' };
const invalidIncrementType = { id: '', name: 'Invalid Increment Type' };
test('should pass validation with valid absoluteRate, rateValue, and incrementCondition', async () => {
const validData = {
absoluteRate: validAbsoluteRate,
rateValue: 10,
incrementCondition: {
incrementValue: 5,
incrementType: validIncrementType,
},
};
await expect(variableIncreasedValueValidation.validate(validData)).resolves.toBe(validData);
});
test('should fail validation if rateValue is not a number when absoluteRate ID is ABSOLUTE_VALUE', async () => {
const invalidData = {
absoluteRate: validAbsoluteRate,
rateValue: null, // This should cause validation to fail
incrementCondition: {
incrementValue: 5,
incrementType: validIncrementType,
},
};
await expect(variableIncreasedValueValidation.validate(invalidData)).rejects.toThrow(GENERAL_REQUIRED_ERROR_MESSAGE);
});
test('should pass validation if absoluteRate ID is NOT ABSOLUTE_VALUE with null rateValue', async () => {
const validData = {
absoluteRate: { id: 'OTHER_VALUE', name: 'Test Rate' },
rateValue: null, // This should be valid
incrementCondition: {
incrementValue: 5,
incrementType: validIncrementType,
},
};
await expect(variableIncreasedValueValidation.validate(validData)).resolves.toBe(validData);
});
test('should require incrementValue if absoluteRate ID is WITHOUT_ABSOLUTE_VALUE', async () => {
const invalidData = {
absoluteRate: { id: 'WITHOUT_ABSOLUTE_VALUE', name: 'Test Rate' },
rateValue: 10,
incrementCondition: {
incrementType: validIncrementType, // Missing incrementValue
},
};
await expect(variableIncreasedValueValidation.validate(invalidData)).rejects.toThrow(GENERAL_REQUIRED_ERROR_MESSAGE);
});
test('should fail validation if incrementType ID is missing when absoluteRate ID is WITHOUT_ABSOLUTE_VALUE', async () => {
const invalidData = {
absoluteRate: { id: 'WITHOUT_ABSOLUTE_VALUE', name: 'Test Rate' },
rateValue: 10,
incrementCondition: {
incrementValue: 5,
incrementType: invalidIncrementType, // invalid incrementType
},
};
await expect(variableIncreasedValueValidation.validate(invalidData)).rejects.toThrow(GENERAL_REQUIRED_ERROR_MESSAGE);
});
test('should pass validation if incrementCondition is not defined and absoluteRate is WITHOUT_ABSOLUTE_VALUE', async () => {
const validData = {
absoluteRate: { id: 'WITHOUT_ABSOLUTE_VALUE', name: 'Test Rate' },
rateValue: 10,
incrementCondition: undefined, // incrementCondition can be not provided
};
await expect(variableIncreasedValueValidation.validate(validData)).resolves.toBe(validData);
});
});
Explanation of Test Cases
Valid Input Test: This checks that valid data passes validation without any issues.
Invalid rateValue Test: This ensures that if absoluteRate.id is "ABSOLUTE_VALUE" and the rateValue is not a number (e.g., null), the validation fails with the appropriate error message.
Valid Case for Non-Absolute Rate: This checks that if the absoluteRate.id is not "ABSOLUTE_VALUE", the rateValue can be null without causing validation to fail.
Missing incrementValue Test: This test checks if incrementValue is required when absoluteRate.id is "WITHOUT_ABSOLUTE_VALUE".
Invalid Increment Type Test: This checks that the incrementType must have a valid id when absoluteRate.id is "WITHOUT_ABSOLUTE_VALUE".
Optional Increment Condition Test: This ensures that if the incrementCondition is not defined for an absoluteRate.id of "WITHOUT_ABSOLUTE_VALUE", the validation passes.
These tests should cover the key validation cases for your Yup schema, ensuring your validation logic operates as expected under various scenarios. Adjust the paths and messages as necessary for your specific implementation.
Valid Input Test: This checks that valid data passes validation without any issues.
Missing Kind ID Test: This checks for failure when kind.id is missing.
Missing Term Index ID Test: This looks specifically at scenarios where termIndex.id is required based on the kind value, ensuring that it is validated correctly.
Not Required Term Index for Other Kinds: Validating that termIndex.id being not present does not affect validation when using a non-relevant kind.
Lookback Requirement for SOFR: Tests that lookback is required when kind is set to SOFR.
Valid Lookback for Non-SOFR: Ensures other kinds can have null lookback.
Cap Requirement Check: Tests that if capChecked is true but cap is not provided, validation should fail.
Floor Requirement Check: Similar to the cap, this checks if floor is required when floorChecked is true.
Replacement Rate Requirement: Tests that replacementRate is required unless kind is CBR_KEY_RATE.
These tests ensure that the floatingBaseIndicatorValidation schema works correctly across a variety of inputs and validation rules. Adjust the paths and messages as necessary for your specific implementation.
import { createFloatingValueValidation } from './path/to/your/module';
import { GENERAL_REQUIRED_ERROR_MESSAGE } from '$ufs-common';
describe('createFloatingValueValidation', () => {
const periodRateSubType = { label: 'Some Label', value: 'someValue' }; // Example of a Labeled<RateSubTypes>
const validationSchema = createFloatingValueValidation(periodRateSubType);
test('should pass validation with valid absoluteRate and rateValue', async () => {
const validData = {
absoluteRate: { id: 'ABSOLUTE_VALUE', name: 'Test Rate' },
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
rateValue: 10,
maxValue: 100,
};
await expect(validationSchema.validate(validData)).resolves.toBe(validData);
});
test('should fail validation if rateValue is not a number when absoluteRate ID is ABSOLUTE_VALUE', async () => {
const invalidData = {
absoluteRate: { id: 'ABSOLUTE_VALUE', name: 'Test Rate' },
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
rateValue: null, // Invalid since it's supposed to be a number
maxValue: 100,
};
await expect(validationSchema.validate(invalidData)).rejects.toThrow(GENERAL_REQUIRED_ERROR_MESSAGE);
});
test('should not fail validation for rateValue if absoluteRate ID is not ABSOLUTE_VALUE', async () => {
const validData = {
absoluteRate: { id: 'OTHER_VALUE', name: 'Test Rate' },
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
rateValue: null, // This should be valid
maxValue: 100,
};
await expect(validationSchema.validate(validData)).resolves.toBe(validData);
});
test('should require maxValue if absoluteRate ID is WITHOUT_ABSOLUTE_VALUE', async () => {
const invalidData = {
absoluteRate: { id: 'WITHOUT_ABSOLUTE_VALUE', name: 'Test Rate' },
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
rateValue: 10,
maxValue: undefined, // Should cause validation to fail
};
await expect(validationSchema.validate(invalidData)).rejects.toThrow(GENERAL_REQUIRED_ERROR_MESSAGE);
});
test('should pass validation if maxValue is provided when absoluteRate ID is WITHOUT_ABSOLUTE_VALUE', async () => {
const validData = {
absoluteRate: { id: 'WITHOUT_ABSOLUTE_VALUE', name: 'Test Rate' },
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
rateValue: 10,
maxValue: 100, // Valid as maxValue is provided
};
await expect(validationSchema.validate(validData)).resolves.toBe(validData);
});
// Add more tests for grossMargin, netMargin, eva, lgd values if needed
});
Explanation of Test Cases
Valid Input Test: This test ensures that valid data with the required structure passes validation without errors.
Invalid rateValue Test when absoluteRate.id is "ABSOLUTE_VALUE": This tests that when rateValue is null (not a number) and absoluteRate.id is ABSOLUTE_VALUE, validation fails.
Valid rateValue Test when absoluteRate.id is not "ABSOLUTE_VALUE": This checks that if absoluteRate.id is different, having rateValue as null is acceptable.
Max Value Requirement Test: This tests that maxValue is required when absoluteRate.id is WITHOUT_ABSOLUTE_VALUE, and ensures validation fails if it's not provided.
Valid maxValue Test when absoluteRate.id is "WITHOUT_ABSOLUTE_VALUE": This checks that if maxValue is provided, validation passes, even if absoluteRate.id is WITHOUT_ABSOLUTE_VALUE.
You can further expand on these tests based on the specific functionality and constraints provided by the createSizeSpecialValueValidation function for grossMargin, netMargin, eva, and lgd fields if you have validation logic for them as well. Adjust paths and error messages according to your implementation as needed.
import { testSizeSpecialValues } from './path/to/your/module';
type SpecialRateValue = {
value: number | null; // Assuming value can be a number or null
};
type Labeled<T> = {
id: string;
name: string;
};
describe('testSizeSpecialValues', () => {
const absoluteRateWithoutValue: Labeled<AbsoluteRateType> = { id: 'WITHOUT_ABSOLUTE_VALUE', name: 'Without Absolute Value' };
const periodRateFixed: Labeled<RateSubTypes> = { id: 'FIXED', name: 'Fixed' };
test('should fail when all special values are missing for FIXED periodRateSubType and WITHOUT_ABSOLUTE_VALUE', () => {
const result = testSizeSpecialValues({
grossMargin: { value: null },
netMargin: { value: null },
eva: { value: null },
lgd: { value: null },
absoluteRate: absoluteRateWithoutValue,
periodRateSubType: periodRateFixed,
});
expect(result).toBe(false);
});
test('should pass when at least one special value is present for FIXED periodRateSubType and WITHOUT_ABSOLUTE_VALUE', () => {
const result = testSizeSpecialValues({
grossMargin: { value: 10 },
netMargin: { value: null },
eva: { value: null },
lgd: { value: null },
absoluteRate: absoluteRateWithoutValue,
periodRateSubType: periodRateFixed,
});
expect(result).toBe(true);
});
test('should pass when periodRateSubType is not provided and all special values are missing', () => {
const result = testSizeSpecialValues({
grossMargin: { value: null },
netMargin: { value: null },
eva: { value: null },
lgd: { value: null },
absoluteRate: absoluteRateWithoutValue,
periodRateSubType: undefined,
});
expect(result).toBe(false);
});
test('should pass when periodRateSubType is not provided with at least one special value', () => {
const result = testSizeSpecialValues({
grossMargin: { value: null },
netMargin: { value: 10 },
eva: { value: null },
lgd: { value: null },
absoluteRate: absoluteRateWithoutValue,
periodRateSubType: undefined,
});
expect(result).toBe(true);
});
test('should pass when absoluteRate is not WITHOUT_ABSOLUTE_VALUE', () => {
const result = testSizeSpecialValues({
grossMargin: { value: null },
netMargin: { value: null },
eva: { value: null },
lgd: { value: null },
absoluteRate: { id: 'OTHER_VALUE', name: 'Other Value' },
periodRateSubType: periodRateFixed,
});
expect(result).toBe(true);
});
test('should pass when NO absoluteRate is provided', () => {
const result = testSizeSpecialValues({
grossMargin: { value: null },
netMargin: { value: null },
eva: { value: null },
lgd: { value: null },
absoluteRate: { id: 'OTHER_VALUE', name: 'Other Value' },
periodRateSubType: undefined,
});
expect(result).toBe(true);
});
});
Explanation of Test Cases
Fail When All Special Values Are Missing: This test checks that the function returns false if all special values (grossMargin, netMargin, eva, lgd) are null when both periodRateSubType is "FIXED" and absoluteRate.id is "WITHOUT_ABSOLUTE_VALUE".
Pass When At Least One Special Value Is Present: This test ensures that if any one of the special values is present (not null), it will pass.
Fail When No Special Values Are Missing and Not Providing periodRateSubType: This tests the case when periodRateSubType is undefined along with missing special values.
Pass When At Least One Special Value Is Present Without periodRateSubType: Similar to the other tests, checks that if at least one special value is present, the validation will pass even if periodRateSubType is not specified.
Pass When absoluteRate is Not WITHOUT_ABSOLUTE_VALUE: This case checks that as long as absoluteRate is not set to "WITHOUT_ABSOLUTE_VALUE", the function should return true regardless of the special values.
Pass When No absoluteRate is Provided: This tests that the function can return true even if absoluteRate has a different value or is not strictly bound to the conditions of absence of special values.
You can adjust the type definitions in the tests according to how you've defined SpecialRateValue and Labeled<T> in your codebase, as the above tests are based on assumptions about those types.
import { createEscrowValueValidation } from './path/to/your/module';
import { GENERAL_REQUIRED_ERROR_MESSAGE } from '$ufs-common';
describe('createEscrowValueValidation', () => {
const periodRateSubType = { label: 'Some Label', value: 'someValue' }; // Example of a Labeled<RateSubTypes>
const validationSchema = createEscrowValueValidation(periodRateSubType);
test('should pass validation with valid absoluteRate and rateValue', async () => {
const validData = {
absoluteRate: { id: 'ABSOLUTE_VALUE', name: 'Test Rate' },
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
rateValue: 10,
};
await expect(validationSchema.validate(validData)).resolves.toBe(validData);
});
test('should fail validation if rateValue is not a number when absoluteRate ID is ABSOLUTE_VALUE', async () => {
const invalidData = {
absoluteRate: { id: 'ABSOLUTE_VALUE', name: 'Test Rate' },
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
rateValue: null, // Invalid since it's supposed to be a number
};
await expect(validationSchema.validate(invalidData)).rejects.toThrow(GENERAL_REQUIRED_ERROR_MESSAGE);
});
test('should not fail validation for rateValue if absoluteRate ID is not ABSOLUTE_VALUE', async () => {
const validData = {
absoluteRate: { id: 'OTHER_VALUE', name: 'Test Rate' },
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
rateValue: null, // This should be valid
};
await expect(validationSchema.validate(validData)).resolves.toBe(validData);
});
// More tests can be added for grossMargin, netMargin, eva, and lgd as they are validated by createSizeSpecialValueValidation
test('should pass validation for special value fields', async () => {
const validData = {
absoluteRate: { id: 'ABSOLUTE_VALUE', name: 'Test Rate' },
grossMargin: 'valid-special-value', // Assuming this is a valid special value
netMargin: 'valid-special-value',
eva: 'valid-special-value',
lgd: 'valid-special-value',
rateValue: 15,
};
await expect(validationSchema.validate(validData)).resolves.toBe(validData);
});
test('should fail validation if absoluteRate.id is missing', async () => {
const invalidData = {
absoluteRate: { id: '', name: 'Test Rate' }, // Missing id
grossMargin: 'valid',
netMargin: 'valid',
eva: 'valid',
lgd: 'valid',
rateValue: 10,
};
await expect(validationSchema.validate(invalidData)).rejects.toThrow();
});
// You can add more tests for other edge cases or specific scenarios based on createSizeSpecialValueValidation logic if needed.
});
Explanation of Test Cases
Valid Input Test: This test ensures that valid data with all required fields passes validation successfully.
Invalid rateValue Test: This test checks that if rateValue is not a number (set to null) when absoluteRate.id is "ABSOLUTE_VALUE", the validation fails with an appropriate error message.
Valid rateValue Test When absoluteRate.id is Not "ABSOLUTE_VALUE": Validates that having rateValue as null is acceptable when absoluteRate.id is not equal to "ABSOLUTE_VALUE".
Other Valid Cases to Validate Special Fields: This test checks that all special fields can hold valid values based on the assumed validation of createSizeSpecialValueValidation.
Invalid Absolute Rate ID Test: This tests that if the absoluteRate is missing an id, the validation fails.
Further Tests
You can expand the tests further to cover:
The specifics of the grossMargin, netMargin, eva, and lgd special value validations depending on their implementations within createSizeSpecialValueValidation.
Scenarios where periodRateSubType might affect validations.
Edge cases involving optional and nullable fields.
You can adjust paths and error messages as necessary based on your project's structure and requirements.