Skip to content

Commit

Permalink
test: improving tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Jun 6, 2024
1 parent 0faf7d8 commit 9163b17
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 15 deletions.
10 changes: 5 additions & 5 deletions packages/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ Feel free to find bugs and report them to me. Your feedback is highly appreciate

jsDelivr:
```bash
https://cdn.jsdelivr.net/npm/[email protected].1/dist/bundle.min.js
https://cdn.jsdelivr.net/npm/[email protected].2/dist/bundle.min.js
```
```html
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].2/dist/bundle.min.js"></script>
```

unpkg:
```bash
https://unpkg.com/[email protected].1/dist/bundle.js
https://unpkg.com/[email protected].2/dist/bundle.js
```
```html
<script src="https://unpkg.com/[email protected].1/dist/bundle.js"></script>
<script src="https://unpkg.com/[email protected].2/dist/bundle.js"></script>
```

### Example of use with CDN

```html
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].2/dist/bundle.min.js"></script>
<script>
const emailResult = isEmail('123456');
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multiform-validator",
"version": "2.1.1",
"version": "2.1.2",
"description": "Javascript library made to validate, several form fields, such as: email, phone, password, cpf etc.",
"main": "./dist/index.js",
"types": "./types/index.d.ts",
Expand Down
51 changes: 51 additions & 0 deletions packages/typescript/src/validateUsername.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,57 @@ function validateUsername(
errorMsg: getErrorMessage(2),
};
} // Tamanho da palavra não pode ser maior que o tamanho máximo

// Define os caracteres especiais
const specialChars: string[] = [
"!",
"@",
"#",
"$",
"%",
"^",
"&",
"*",
"(",
")",
"-",
"_",
"=",
"+",
"[",
"]",
"{",
"}",
"|",
"\\",
";",
":",
"'",
'"',
",",
".",
"<",
">",
"/",
"?",
];

// Cria um objeto para contar a ocorrência de cada caractere especial
const charCount: { [key: string]: number } = {};

// Itera sobre a string para contar os caracteres especiais
for (const char of username) {
if (specialChars.includes(char)) {
charCount[char] = (charCount[char] || 0) + 1;
if (charCount[char] > 2) {
return {
isValid: false,
errorMsg: "Username cannot contain multiple special characters",
};
}
}
}

return {
isValid: true,
errorMsg: null,
Expand Down
118 changes: 109 additions & 9 deletions packages/typescript/tests/src/isDecimal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import isDecimal from '../../src/isDecimal';
describe('isDecimal', () => {
const errorToThrow = "Input value must be a string or a number."; // Mensagem de erro a ser lançada

it('should return true when the input is a valid decimal number', () => {
const result = isDecimal('123.45');
expect(result).toBe(true);
});
it('should return true when the input is a valid decimal number', () => {
const result = isDecimal('123.45');
expect(result).toBe(true);
});

it('should return false when the input is not a valid decimal number', () => {
const result = isDecimal('123.456.789');
expect(result).toBe(false);
});
it('should return false when the input is not a valid decimal number', () => {
const result = isDecimal('123.456.789');
expect(result).toBe(false);
});

it('should return false when the input is an integer', () => {
const result = isDecimal('123');
Expand Down Expand Up @@ -65,12 +65,112 @@ describe('isDecimal', () => {
});

it('should throw error when the input is a function', () => {
function func() {}
function func() { }

expect(() => isDecimal(func() as any) as any).toThrow(errorToThrow);
});

it('should throw error when the input is a symbol', () => {
expect(() => isDecimal(Symbol() as any) as any).toThrow(errorToThrow);
});

it('should throw error when the input is a date', () => {
expect(() => isDecimal(new Date() as any)).toThrow(errorToThrow);
});

it('should throw error when the input is an empty array', () => {
expect(() => isDecimal([] as any)).toThrow(errorToThrow);
});

it('should throw error when the input is an empty object', () => {
expect(() => isDecimal({} as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a Map', () => {
expect(() => isDecimal(new Map() as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a Set', () => {
expect(() => isDecimal(new Set() as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a WeakMap', () => {
expect(() => isDecimal(new WeakMap() as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a WeakSet', () => {
expect(() => isDecimal(new WeakSet() as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a regular expression', () => {
expect(() => isDecimal(/abc/g as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a class', () => {
class A { }

expect(() => isDecimal(A as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a class instance', () => {
class A { }

expect(() => isDecimal(new A() as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a BigInt', () => {
expect(() => isDecimal(BigInt(10) as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a URL', () => {
expect(() => isDecimal(new URL('https://example.com') as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a Buffer', () => {
expect(() => isDecimal(Buffer.from('hello') as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a DataView', () => {
expect(() => isDecimal(new DataView(new ArrayBuffer(2)) as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a Float32Array', () => {
expect(() => isDecimal(new Float32Array(2) as any)).toThrow(errorToThrow);
});

it('should throw error when the input is a Float64Array', () => {
expect(() => isDecimal(new Float64Array(2) as any)).toThrow(errorToThrow);
});

it('should return false when the input is a integer number', () => {
const result = isDecimal(123);
expect(result).toBe(false);
});

it('should return true when the input is a valid decimal number', () => {
const result = isDecimal(123.45);
expect(result).toBe(true);
});

it('should throw error when the input is a boolean', () => {
expect(() => isDecimal(true as any)).toThrow("Input value must be a string or a number.");
});

it('should throw error when the input is an array', () => {
expect(() => isDecimal([] as any)).toThrow("Input value must be a string or a number.");
});

it('should throw error when the input is an object', () => {
expect(() => isDecimal({} as any)).toThrow("Input value must be a string or a number.");
});

it('should throw error when the input is a function', () => {
function func() { }

expect(() => isDecimal(func as any)).toThrow("Input value must be a string or a number.");
});

it('should throw error when the input is a symbol', () => {
expect(() => isDecimal(Symbol() as any)).toThrow("Input value must be a string or a number.");
});
});
26 changes: 26 additions & 0 deletions packages/typescript/tests/src/validateName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,30 @@ describe('validateName', () => {
minLength: 1, maxLength: 20, errorMsg: [123 as unknown as string]
})).toThrow('All values within the array must be strings or null/undefined.');
});

it('should return true for valid names with custom min and max length', () => {
const result: ValidateFunctions = validateName('John', { minLength: 1, maxLength: 20 });
expect(result.isValid).toBe(true);
});

it('should return false for names that are too short', () => {
const result: ValidateFunctions = validateName('J', { minLength: 2, maxLength: 20 });
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("This name is not valid");
});

it('should return false for names that are too long', () => {
const result: ValidateFunctions = validateName('JohnJohnJohnJohnJohnJohnJohnJohnJohnJohn', { minLength: 2, maxLength: 20 });
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("Name too big, try again");
});

it('Should return true for a valid name Peter, Pedro, or Gabriel', () => {
const result1: ValidateFunctions = validateName('Peter');
const result2: ValidateFunctions = validateName('Pedro');
const result3: ValidateFunctions = validateName('Gabriel');
expect(result1.isValid).toBe(true);
expect(result2.isValid).toBe(true);
expect(result3.isValid).toBe(true);
});
});
44 changes: 44 additions & 0 deletions packages/typescript/tests/src/validatePassword.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,48 @@ describe('validatePassword', () => {
});
expect(result2.errorMsg).toBe("Password must be greater than 20 characters");
});

it('Returns correct error message for password without uppercase', () => {
const result = validatePassword('passw0rd!', {
options: { requireUppercase: true }
});
expect(result.errorMsg).toBe("Password requires at least one capital letter");
});

it('Returns correct error message for password without special character', () => {
const result = validatePassword('Passw0rd', {
options: { requireSpecialChar: true }
});
expect(result.errorMsg).toBe("Password requires at least one special character");
});

it('Returns correct error message for password without number', () => {
const result = validatePassword('Password!', {
options: { requireNumber: true }
});
expect(result.errorMsg).toBe("Password requires at least one number");
});

it('Returns correct error message for password without string', () => {
const result = validatePassword('12345678!', {
options: { requireString: true }
});
expect(result.errorMsg).toBe("Password requires at least one letter");
});

it('should throw error for invalid errorMsg parameter', () => {
expect(() => validatePassword('Passw0rd!', { minLength: 8, errorMsg: [123 as any] })).toThrow('All values within the array must be strings or null/undefined.');
});

it('should throw error for invalid password parameter', () => {
expect(() => validatePassword(123 as any)).toThrow('The input should be a string.');
});

it('should throw error for invalid minLength parameter', () => {
expect(() => validatePassword('Passw0rd!', { minLength: 8, maxLength: 6 })).toThrow("the minimum size cannot be larger than the maximum");
});

it('should throw error for invalid errorMsg parameter', () => {
expect(() => validatePassword('Passw0rd!', { minLength: 8, maxLength: 20, errorMsg: [123 as any] })).toThrow('All values within the array must be strings or null/undefined.');
});
});
26 changes: 26 additions & 0 deletions packages/typescript/tests/src/validateSurname.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,30 @@ describe('validateSurname', () => {
it('throws error for invalid errorMsg parameter', () => {
expect(() => validateSurname('Jackson', { minLength: 3, maxLength: 25, errorMsg: [123 as unknown as string] })).toThrow('All values within the array must be strings or null/undefined.');
});

it('should return true for valid surnames with custom min and max length', () => {
const result = validateSurname('Jackson', { minLength: 1, maxLength: 20 });
expect(result.isValid).toBe(true);
});

it('should return false for surnames that are too short', () => {
const result = validateSurname('J', { minLength: 2, maxLength: 20 });
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("This surname is not valid");
});

it('should return false for surnames that are too long', () => {
const result = validateSurname('JacksonJacksonJacksonJacksonJackson', { minLength: 2, maxLength: 20 });
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe("Surname too big, try again");
});

it('Should return true for a valid surname Jackson, Johnson, or Smith', () => {
const result1 = validateSurname('Jackson');
const result2 = validateSurname('Johnson');
const result3 = validateSurname('Smith');
expect(result1.isValid).toBe(true);
expect(result2.isValid).toBe(true);
expect(result3.isValid).toBe(true);
});
});
35 changes: 35 additions & 0 deletions packages/typescript/tests/src/validateUsername.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,39 @@ describe('validateUsername', () => {
it('throws error for invalid errorMsg parameter', () => {
expect(() => validateUsername('User123', { minLength: 3, maxLength: 25, errorMsg: [123 as any] })).toThrow('All values within the array must be strings or null/undefined.');
});

it('should return true for valid usernames with custom min and max length', () => {
const result = validateUsername('User123', { minLength: 1, maxLength: 20 });
expect(result.isValid).toBe(true);
});

it('should return false for usernames that are too short', () => {
const result = validateUsername('U', { minLength: 2, maxLength: 20 });
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('Username must be between 2 and 20 characters');
});

it('should return false for usernames that are too long', () => {
const result = validateUsername('User123User123User123User123', { minLength: 2, maxLength: 20 });
expect(result.isValid).toBe(false);
expect(result.errorMsg).toBe('Username must be between 2 and 20 characters');
});

it('Should return true for a valid username User123, User, or User1234', () => {
const result1 = validateUsername('User123');
const result2 = validateUsername('User');
const result3 = validateUsername('User1234');
expect(result1.isValid).toBe(true);
expect(result2.isValid).toBe(true);
expect(result3.isValid).toBe(true);
});

it('Should return false for invalid usernames', () => {
const result1 = validateUsername('User 123');
const result2 = validateUsername('123User');
const result3 = validateUsername('@@@@@@');
expect(result1.isValid).toBe(false);
expect(result2.isValid).toBe(false);
expect(result3.isValid).toBe(false);
});
});

0 comments on commit 9163b17

Please sign in to comment.