Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit tests with jest #8

Merged
merged 16 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
coverage

*.log
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js

sudo: false

node_js:
- 10
- 12
- 14
- 15

install:
- yarn

script:
- yarn test
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
`generate-string` is a string generator that build random strings, strings with mask and passwords with password-strength tester.
It is lightweight, extensible, has no dependencies, typescript support and can be used on the server with NodeJS or in-browser with JS.

[![Build Status](https://travis-ci.com/lucasgdb/generate-strings.svg?branch=master)](https://travis-ci.com/lucasgdb/generate-strings)

## Installing

### Server-side (NodeJS)
Expand Down
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module.exports = {
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-export-namespace-from',
],
ignore: [/node_modules/, /build/],
ignore: [/node_modules/, /dist/],
};
12 changes: 12 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
preset: 'ts-jest',
globals: {
'ts-jest': {
babelConfig: true,
},
},
testEnvironment: 'node',
transformIgnorePatterns: ['/node_modules/(?!@babel/runtime)', 'dist'],
testPathIgnorePatterns: ['node_modules', 'dist'],
setupFiles: ['core-js'],
};
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Generate random strings, strings with mask and strength passwords with password-strength tester",
"main": "src/index.ts",
"scripts": {
"build": "webpack"
"build": "webpack",
"test": "jest"
},
"devDependencies": {
"@babel/cli": "^7.15.7",
Expand All @@ -13,16 +14,21 @@
"@babel/plugin-proposal-export-namespace-from": "^7.14.5",
"@babel/preset-env": "^7.15.6",
"@babel/preset-typescript": "^7.15.0",
"@types/jest": "^27.0.2",
"@types/node": "^16.10.2",
"@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0",
"babel-jest": "^27.2.5",
"babel-loader": "^8.2.2",
"core-js": "^3.18.2",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.2",
"jest": "^27.2.5",
"prettier": "^2.4.1",
"sucrase": "^3.20.1",
"ts-jest": "^27.0.5",
"typescript": "^4.4.3",
"webpack": "^5.56.1",
"webpack-cli": "^4.8.0"
Expand Down
49 changes: 49 additions & 0 deletions src/helpers/__tests__/getCharacters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import getCharacters, { getCharactersProps } from '../getCharacters';

describe('getCharacters', () => {
test('getCharacters without props', () => {
const possibleCharacters = getCharacters();

expect(possibleCharacters).toBe(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
);
});

test('getCharacters with inverted characters', () => {
const config: getCharactersProps = {
upperCase: false,
lowerCase: false,
special: true,
number: false,
};

const possibleCharacters = getCharacters(config);

expect(possibleCharacters).toBe('!@#$%&*()=[]{}');
});

test('getCharacters with changed default characters', () => {
const config: getCharactersProps = {
upperCaseCharacters: 'ABCDEF',
lowerCaseCharacters: 'abcdef',
numberCharacters: '012345',
};

const possibleCharacters = getCharacters(config);

expect(possibleCharacters).toBe('ABCDEFabcdef012345');
});

it('should throw an "You must set at least 1 character type" error', () => {
const config: getCharactersProps = {
upperCase: false,
lowerCase: false,
special: false,
number: false,
};

expect(() => getCharacters(config)).toThrowError(
'You must set at least 1 character type.'
);
});
});
17 changes: 17 additions & 0 deletions src/helpers/__tests__/getRandomInteger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getRandomInteger from '../getRandomInteger';

describe('getRandomInteger', () => {
it('should generate random number between 1 and 5', () => {
const randomInteger = getRandomInteger(1, 5);

expect(randomInteger).toBeGreaterThanOrEqual(1);
expect(randomInteger).toBeLessThanOrEqual(5);
});

it('should generate random number between -5 and 5', () => {
const randomInteger = getRandomInteger(-5, 5);

expect(randomInteger).toBeGreaterThanOrEqual(-5);
expect(randomInteger).toBeLessThanOrEqual(5);
});
});
8 changes: 3 additions & 5 deletions src/helpers/getCharacters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
DEFAULT_NUMBER_CHARACTERS,
} from '../constants/defaults';

type getCharactersProps = {
export type getCharactersProps = {
upperCase?: boolean;
upperCaseCharacters?: string;
lowerCase?: boolean;
Expand All @@ -20,7 +20,7 @@ type getCharactersProps = {
numberCharacters?: string;
};

const getCharacters = (props?: getCharactersProps) => {
export default function getCharacters(props?: getCharactersProps) {
const hasUpperCases = props?.upperCase ?? DEFAULT_HAS_UPPERCASE_CHARACTERS;
const hasLowerCases = props?.lowerCase ?? DEFAULT_HAS_LOWERCASE_CHARACTERS;
const hasSpecials = props?.special ?? DEFAULT_HAS_SPECIAL_CHARACTERS;
Expand Down Expand Up @@ -58,6 +58,4 @@ const getCharacters = (props?: getCharactersProps) => {
}

return characters;
};

export default getCharacters;
}
4 changes: 1 addition & 3 deletions src/helpers/getRandomInteger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
function getRandomInteger(min: number, max: number) {
export default function getRandomInteger(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

export default getRandomInteger;
64 changes: 64 additions & 0 deletions src/utils/__tests__/generateRandomPassword.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { generateRandomPassword } from '../generateRandomPassword';

describe('generateRandomPassword', () => {
it('should generate a password with 15 characters', () => {
const randomPassword = generateRandomPassword({ passwordLength: 15 });
expect(randomPassword.password.length).toBe(15);
});

it('should throw "passwordLength should be greater than 0" error', () => {
const generateRandomPasswordCaller = (passwordLength: number) => () => {
generateRandomPassword({ passwordLength });
};

expect(generateRandomPasswordCaller(0)).toThrowError(
'passwordLength must be greater than 0.'
);

expect(generateRandomPasswordCaller(-5)).toThrowError(
'passwordLength must be greater than 0.'
);
});

it('should throw "You must set at least 1 character type" error', () => {
const config = {
upperCase: false,
lowerCase: false,
special: false,
number: false,
};

expect(() => generateRandomPassword(config)).toThrowError(
'You must set at least 1 character type.'
);
});

it('should throw "You must at least 1 character" error', () => {
const config = {
upperCaseCharacters: '',
lowerCaseCharacters: '',
specialCharacters: '',
numberCharacters: '',
};

expect(() => {
generateRandomPassword({ firstCharType: 'random', ...config });
}).toThrowError('You must at least 1 character');

expect(() => {
generateRandomPassword({ firstCharType: 'upperCase', ...config });
}).toThrowError('You must at least 1 character for upperCase');

expect(() => {
generateRandomPassword({ firstCharType: 'lowerCase', ...config });
}).toThrowError('You must at least 1 character for lowerCase');

expect(() => {
generateRandomPassword({ firstCharType: 'special', ...config });
}).toThrowError('You must at least 1 character for special');

expect(() => {
generateRandomPassword({ firstCharType: 'number', ...config });
}).toThrowError('You must at least 1 character for number');
});
});
22 changes: 22 additions & 0 deletions src/utils/__tests__/generateRandomString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { generateRandomString } from '../generateRandomString';

describe('generateRandomString', () => {
it('should generate a password with 15 characters', () => {
const randomString = generateRandomString({ stringLength: 15 });
expect(randomString.length).toBe(15);
});

it('should throw "stringLength must be greater than 0" error', () => {
const generateRandomStringCaller = (stringLength: number) => () => {
generateRandomString({ stringLength });
};

expect(generateRandomStringCaller(0)).toThrowError(
'stringLength must be greater than 0.'
);

expect(generateRandomStringCaller(-5)).toThrowError(
'stringLength must be greater than 0.'
);
});
});
11 changes: 8 additions & 3 deletions src/utils/generateRandomPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function getFirstCharacter(props?: getFirstCharacterProps) {
.reduce((str, char) => `${str}${char}`, '');

if (characters === '') {
throw new Error('You must at least 1 character');
throw new Error('You must at least 1 character.');
}

const randomPosition = getRandomInteger(0, characters.length - 1);
Expand All @@ -108,7 +108,7 @@ function getFirstCharacter(props?: getFirstCharacterProps) {

const characters = character[firstCharType];
if (characters === '') {
throw new Error(`You must at least 1 character for ${firstCharType}`);
throw new Error(`You must at least 1 character for ${firstCharType}.`);
}

const randomPosition = getRandomInteger(0, characters.length - 1);
Expand All @@ -133,9 +133,14 @@ type getRandomPasswordProps = {
};

function getRandomPassword(props?: getRandomPasswordProps) {
const passwordLength = props?.passwordLength ?? DEFAULT_PASSWORD_LENGTH;

if (passwordLength <= 0) {
throw new Error('passwordLength must be greater than 0.');
}

const possibleCharacters = getCharacters(props);
const firstChar = getFirstCharacter(props);
const passwordLength = props?.passwordLength ?? DEFAULT_PASSWORD_LENGTH;
const excludeEqualChars =
props?.excludeEqualChars ?? DEFAULT_EXCLUDE_EQUAL_CHARS;

Expand Down
Loading