Skip to content

Commit

Permalink
Add jest, various tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgb-io committed May 26, 2022
1 parent 7787440 commit 388ac39
Show file tree
Hide file tree
Showing 8 changed files with 2,967 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
yarn-error.log
lib
lib
coverage
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
collectCoverageFrom: ["src/**/*"],
};
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
"author": "Sam Brown (https://github.com/sgb-io)",
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.18.2",
"@babel/preset-env": "^7.18.2",
"@babel/preset-typescript": "^7.17.12",
"@types/jest": "^27.5.1",
"babel-jest": "^28.1.0",
"eslint": "^8.16.0",
"jest": "^28.1.0",
"prettier": "^2.6.2",
"typescript": "^4.7.2"
},
"scripts": {
"build:watch": "tsc --watch",
"build": "tsc"
"build": "tsc",
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage"
},
"repository": {
"type": "git",
Expand Down
77 changes: 77 additions & 0 deletions src/incomeTax.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { calculateIncomeTax } from "./incomeTax";
import { calculatePersonalAllowance } from "./personalAllowance";

const expectations = [
{ taxableAnnualIncome: 15_000, basic: 486, higher: 0, additional: 0 },
{ taxableAnnualIncome: 17_500, basic: 986, higher: 0, additional: 0 },
{ taxableAnnualIncome: 20_000, basic: 1486, higher: 0, additional: 0 },
{ taxableAnnualIncome: 22_500, basic: 1986, higher: 0, additional: 0 },
{ taxableAnnualIncome: 25_000, basic: 2486, higher: 0, additional: 0 },
{ taxableAnnualIncome: 50_000, basic: 7486, higher: 0, additional: 0 },
{ taxableAnnualIncome: 55_000, basic: 7540, higher: 1892, additional: 0 },
{ taxableAnnualIncome: 60_000, basic: 7540, higher: 3892, additional: 0 },
{ taxableAnnualIncome: 75_000, basic: 7540, higher: 9892, additional: 0 },
{ taxableAnnualIncome: 90_000, basic: 7540, higher: 15892, additional: 0 },
{ taxableAnnualIncome: 110_000, basic: 7540, higher: 25892, additional: 0 },
{ taxableAnnualIncome: 130_000, basic: 7540, higher: 36920, additional: 0 },
{
taxableAnnualIncome: 145_000,
basic: 7540,
higher: 39892,
additional: 3406.5,
},
{
taxableAnnualIncome: 160_000,
basic: 7540,
higher: 39892,
additional: 10156.5,
},
{
taxableAnnualIncome: 175_000,
basic: 7540,
higher: 39892,
additional: 16906.5,
},
{
taxableAnnualIncome: 200_000,
basic: 7540,
higher: 39892,
additional: 28156.5,
},
{
taxableAnnualIncome: 250_000,
basic: 7540,
higher: 39892,
additional: 50656.5,
},
{
taxableAnnualIncome: 500_000,
basic: 7540,
higher: 39892,
additional: 163156.5,
},
{
taxableAnnualIncome: 1_000_000,
basic: 7540,
higher: 39892,
additional: 388156.5,
},
];

describe("calculateIncomeTax", () => {
expectations.forEach((expectation) => {
const { taxableAnnualIncome, basic, higher, additional } = expectation;
test(taxableAnnualIncome.toString(), () => {
const personalAllowance = calculatePersonalAllowance({
taxableAnnualIncome,
});
expect(
calculateIncomeTax({ taxableAnnualIncome, personalAllowance })
).toEqual({
basicRateTax: basic,
higherRateTax: higher,
additionalRateTax: additional,
});
});
});
});
33 changes: 33 additions & 0 deletions src/nationalInsurance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { calculateEmployeeNationalInsurance } from "./nationalInsurance";

const expectations = [
{ taxableAnnualIncome: 15_000, nics: 678.4 },
{ taxableAnnualIncome: 17_500, nics: 1009.6500000000001 },
{ taxableAnnualIncome: 20_000, nics: 1340.9000000000003 },
{ taxableAnnualIncome: 22_500, nics: 1672.15 },
{ taxableAnnualIncome: 25_000, nics: 2003.3999999999999 },
{ taxableAnnualIncome: 50_000, nics: 5315.900000000001 },
{ taxableAnnualIncome: 55_000, nics: 5506.8 },
{ taxableAnnualIncome: 60_000, nics: 5669.3 },
{ taxableAnnualIncome: 75_000, nics: 6156.8 },
{ taxableAnnualIncome: 90_000, nics: 6644.3 },
{ taxableAnnualIncome: 110_000, nics: 7294.3 },
{ taxableAnnualIncome: 130_000, nics: 7944.3 },
{ taxableAnnualIncome: 150_000, nics: 8594.300000000001 },
{ taxableAnnualIncome: 175_000, nics: 9406.800000000001 },
{ taxableAnnualIncome: 200_000, nics: 10219.300000000001 },
{ taxableAnnualIncome: 250_000, nics: 11844.300000000001 },
{ taxableAnnualIncome: 500_000, nics: 19969.3 },
{ taxableAnnualIncome: 1_000_000, nics: 36219.299999999996 },
];

describe("calculateEmployeeNationalInsurance", () => {
expectations.forEach((expectation) => {
const { taxableAnnualIncome, nics } = expectation;
test(taxableAnnualIncome.toString(), () => {
expect(
calculateEmployeeNationalInsurance({ taxableAnnualIncome })
).toEqual(nics);
});
});
});
37 changes: 37 additions & 0 deletions src/personalAllowance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { calculatePersonalAllowance } from "./personalAllowance";

const expectations = [
{ taxableAnnualIncome: 15_000, allowance: 12570 },
{ taxableAnnualIncome: 17_500, allowance: 12570 },
{ taxableAnnualIncome: 20_000, allowance: 12570 },
{ taxableAnnualIncome: 22_500, allowance: 12570 },
{ taxableAnnualIncome: 25_000, allowance: 12570 },
{ taxableAnnualIncome: 50_000, allowance: 12570 },
{ taxableAnnualIncome: 55_000, allowance: 12570 },
{ taxableAnnualIncome: 60_000, allowance: 12570 },
{ taxableAnnualIncome: 75_000, allowance: 12570 },
{ taxableAnnualIncome: 90_000, allowance: 12570 },
{ taxableAnnualIncome: 110_000, allowance: 7570 },
{ taxableAnnualIncome: 120_000, allowance: 2570 },
{ taxableAnnualIncome: 124_500, allowance: 320 },
{ taxableAnnualIncome: 125_000, allowance: 70 },
{ taxableAnnualIncome: 130_000, allowance: 0 },
{ taxableAnnualIncome: 145_000, allowance: 0 },
{ taxableAnnualIncome: 160_000, allowance: 0 },
{ taxableAnnualIncome: 175_000, allowance: 0 },
{ taxableAnnualIncome: 200_000, allowance: 0 },
{ taxableAnnualIncome: 250_000, allowance: 0 },
{ taxableAnnualIncome: 500_000, allowance: 0 },
{ taxableAnnualIncome: 1_000_000, allowance: 0 },
];

describe("calculatePersonalAllowance", () => {
expectations.forEach((expectation) => {
const { taxableAnnualIncome, allowance } = expectation;
test(taxableAnnualIncome.toString(), () => {
expect(calculatePersonalAllowance({ taxableAnnualIncome })).toEqual(
allowance
);
});
});
});
Loading

0 comments on commit 388ac39

Please sign in to comment.