DigitalConsumer.ISS/src/helpers/validation-rules.spec.js

58 lines
1.7 KiB
JavaScript

import { required, regex } from '@/helpers/validation-rules';
describe('validation-rules.vue', () => {
test('required rules should return error if value missing', () => {
// Arrange
const testFn = required('an error');
// Act
const testResponse = testFn();
// Assert
expect(testResponse).toBe('an error');
});
test('required rules should return true if value present', () => {
// Arrange
const testFn = required('an error');
// Act
const testResponse = testFn('some value');
// Assert
expect(testResponse).toBe(true);
});
test('regex rules should return true if value is not present', () => {
// Arrange
const testFn = regex(/(^\d{5}$)|(^\d{5}-\d{4}$)/, 'an error'); // Using Zip regex
// Act
const testResponse = testFn();
// Assert
expect(testResponse).toBe(true);
});
test('regex rules should return false if value is present but does not match regular expression', () => {
// Arrange
const testFn = regex(/(^\d{5}$)|(^\d{5}-\d{4}$)/, 'an error'); // Using Zip regex
// Act
const testResponse = testFn('4321'); // needs to be 5 numbers
// Assert
expect(testResponse).toBe('an error');
});
test('regex rules should return true if value is present and does match regular expression', () => {
// Arrange
const testFn = regex(/(^\d{5}$)|(^\d{5}-\d{4}$)/, 'an error'); // Using Zip regex
// Act
const testResponse = testFn('43213'); // needs to be 5 numbers
// Assert
expect(testResponse).toBe(true);
});
});