Skip to content
Open
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
8 changes: 8 additions & 0 deletions modulo-6/testes-unitarios-mocks/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
roots: ["<rootDir>/tests"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
};
16 changes: 16 additions & 0 deletions modulo-6/testes-unitarios-mocks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"scripts": {
"test": "jest",
"dev-start": "tsnd --transpile-only --ignore-watch node_modules ./src/index.ts"
},
"dependencies": {
"jest": "^27.0.4"
},
"devDependencies": {
"@types/jest": "^26.0.23",
"ts-node-dev": "^1.1.6",
"@types/node": "^15.12.4",
"ts-jest": "^27.0.3",
"typescript": "^4.3.4"
}
}
Binary file added modulo-6/testes-unitarios-mocks/src/.DS_Store
Binary file not shown.
43 changes: 43 additions & 0 deletions modulo-6/testes-unitarios-mocks/src/calculateEmployeeSalary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
validateEmptyProperties,
ValidateEmptyPropertiesOutput,
} from "./validateEmptyProperties";


interface CalculateEmployeeSalaryInput {
employeeName: string;
baseSalary: number;
benefits: number[];
extraHours: number;
}

export const calculateEmployeeSalary = (
input: CalculateEmployeeSalaryInput
) => {
const validationResult: ValidateEmptyPropertiesOutput = validateEmptyProperties(input);

if (!validationResult.isValid) {
throw new Error("Missing Properties");
}

if (input.baseSalary < 0) {
throw new Error("Invalid BaseSalary");
}

let fullSalary = input.baseSalary;

for (const benefit of input.benefits) {
if (benefit < 0) {
throw new Error("Invalid Benefit");
}
fullSalary += benefit;
}

if (input.extraHours < 0) {
throw new Error("Invalid ExtraHours");
}

fullSalary += input.extraHours;

return fullSalary;
};
Empty file.
28 changes: 28 additions & 0 deletions modulo-6/testes-unitarios-mocks/src/validateEmptyProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const validateEmptyProperties = (
input: any
): ValidateEmptyPropertiesOutput => {
let errors: ValidateEmptyPropertiesError[] = [];
for (const key in input) {
if (input[key] !== false && !input[key]) {
errors.push({
key,
value: input[key],
});
}
}

return {
isValid: errors.length === 0,
errors,
};
};

export interface ValidateEmptyPropertiesOutput {
isValid: boolean;
errors: ValidateEmptyPropertiesError[];
}

interface ValidateEmptyPropertiesError {
key: string;
value: string;
}
Empty file.
11 changes: 11 additions & 0 deletions modulo-6/testes-unitarios-mocks/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./build",
"rootDir": "./",
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
}