mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-19 18:18:27 -05:00
* fix(tools/scripts) convert validate and get-lines to typescript files * fix(tools/scripts) refactor validate and get-lines to typescript * remove any type and eslint ignore * Update utils/validate.ts Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * address comments * update .ignore files to ignore newly generated utils files * Update utils/get-lines.ts Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * remove unneeded ErrorInterface * add local type annotation to 'expected' variable Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
59 lines
2.2 KiB
TypeScript
59 lines
2.2 KiB
TypeScript
import {
|
|
isValidUsername,
|
|
usernameTooShort,
|
|
validationSuccess,
|
|
usernameIsHttpStatusCode,
|
|
invalidCharError
|
|
} from './validate';
|
|
|
|
function inRange(num: number, range: number[]) {
|
|
return num >= range[0] && num <= range[1];
|
|
}
|
|
|
|
describe('isValidUsername', () => {
|
|
it('rejects strings with less than 3 characters', () => {
|
|
expect(isValidUsername('')).toStrictEqual(usernameTooShort);
|
|
expect(isValidUsername('12')).toStrictEqual(usernameTooShort);
|
|
expect(isValidUsername('a')).toStrictEqual(usernameTooShort);
|
|
expect(isValidUsername('12a')).toStrictEqual(validationSuccess);
|
|
});
|
|
it('rejects strings which are http response status codes 100-599', () => {
|
|
expect(isValidUsername('429')).toStrictEqual(usernameIsHttpStatusCode);
|
|
expect(isValidUsername('789')).toStrictEqual(validationSuccess);
|
|
});
|
|
it('rejects non-ASCII characters', () => {
|
|
expect(isValidUsername('👀👂👄')).toStrictEqual(invalidCharError);
|
|
});
|
|
it('rejects with invalidCharError even if the string is too short', () => {
|
|
expect(isValidUsername('.')).toStrictEqual(invalidCharError);
|
|
});
|
|
it('accepts alphanumeric characters', () => {
|
|
expect(
|
|
isValidUsername('abcdefghijklmnopqrstuvwxyz0123456789')
|
|
).toStrictEqual(validationSuccess);
|
|
});
|
|
it('accepts hyphens and underscores', () => {
|
|
expect(isValidUsername('a-b')).toStrictEqual(validationSuccess);
|
|
expect(isValidUsername('a_b')).toStrictEqual(validationSuccess);
|
|
});
|
|
|
|
it('rejects all other ASCII characters', () => {
|
|
const allowedCharactersList = ['-', '_', '+'];
|
|
const numbers = [48, 57];
|
|
const upperCase = [65, 90];
|
|
const lowerCase = [97, 122];
|
|
const base = 'user';
|
|
const finalCode = 127;
|
|
|
|
for (let code = 0; code <= finalCode; code++) {
|
|
const char = String.fromCharCode(code);
|
|
let expected: { valid: boolean; error: null | string } = invalidCharError;
|
|
if (allowedCharactersList.includes(char)) expected = validationSuccess;
|
|
if (inRange(code, numbers)) expected = validationSuccess;
|
|
if (inRange(code, upperCase)) expected = validationSuccess;
|
|
if (inRange(code, lowerCase)) expected = validationSuccess;
|
|
expect(isValidUsername(base + char)).toStrictEqual(expected);
|
|
}
|
|
});
|
|
});
|