-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from lucasgdb/test/add-jest
test: add unit tests with jest
- Loading branch information
Showing
14 changed files
with
1,823 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
dist | ||
coverage | ||
|
||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.