Skip to content

Commit 04cffc1

Browse files
committed
♻️: Refactored Drug class and created subclasses for each drug type
The Drug class now implements the logic of updating the drug depending on its rules. The class should be abstract but it is left usable due to exercise's instructions. Original unit tests are still working, output is not modified when program runs ensuring no regression.
1 parent 2916bad commit 04cffc1

18 files changed

+658
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Doliprane should be serializable to JSON 1`] = `
4+
Object {
5+
"benefit": 5,
6+
"expiresIn": 30,
7+
"name": "Doliprane",
8+
}
9+
`;
10+
11+
exports[`Doliprane should be serializable to string 1`] = `"{\\"name\\":\\"Doliprane\\",\\"expiresIn\\":30,\\"benefit\\":5}"`;
12+
13+
exports[`Doliprane should decrease benefit twice as much if expired 1`] = `
14+
Object {
15+
"benefit": 3,
16+
"expiresIn": -1,
17+
"name": "Doliprane",
18+
}
19+
`;
20+
21+
exports[`Doliprane should decrease expiresIn and benefit over time 1`] = `
22+
Object {
23+
"benefit": 4,
24+
"expiresIn": 29,
25+
"name": "Doliprane",
26+
}
27+
`;
28+
29+
exports[`Doliprane should not allow for a benefit greater than 50 1`] = `
30+
Object {
31+
"benefit": 50,
32+
"expiresIn": 30,
33+
"name": "Doliprane",
34+
}
35+
`;
36+
37+
exports[`Doliprane should not allow for a negative benefit 1`] = `
38+
Object {
39+
"benefit": 0,
40+
"expiresIn": 30,
41+
"name": "Doliprane",
42+
}
43+
`;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Drug should be serializable to JSON 1`] = `
4+
Object {
5+
"benefit": 5,
6+
"expiresIn": 30,
7+
"name": "test",
8+
}
9+
`;
10+
11+
exports[`Drug should be serializable to string 1`] = `"{\\"name\\":\\"test\\",\\"expiresIn\\":30,\\"benefit\\":5}"`;
12+
13+
exports[`Drug should decrease benefit twice as much if expired 1`] = `
14+
Object {
15+
"benefit": 3,
16+
"expiresIn": -1,
17+
"name": "test",
18+
}
19+
`;
20+
21+
exports[`Drug should decrease expiresIn and benefit over time 1`] = `
22+
Object {
23+
"benefit": 4,
24+
"expiresIn": 29,
25+
"name": "test",
26+
}
27+
`;
28+
29+
exports[`Drug should not allow for a benefit greater than 50 1`] = `
30+
Object {
31+
"benefit": 50,
32+
"expiresIn": 30,
33+
"name": "test",
34+
}
35+
`;
36+
37+
exports[`Drug should not allow for a negative benefit 1`] = `
38+
Object {
39+
"benefit": 0,
40+
"expiresIn": 30,
41+
"name": "test",
42+
}
43+
`;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Fervex benefit + 2 if 10 >= expiresIn > 5 1`] = `
4+
Object {
5+
"benefit": 7,
6+
"expiresIn": 9,
7+
"name": "Fervex",
8+
}
9+
`;
10+
11+
exports[`Fervex benefit + 2 if 10 >= expiresIn > 5 2`] = `
12+
Object {
13+
"benefit": 9,
14+
"expiresIn": 5,
15+
"name": "Fervex",
16+
}
17+
`;
18+
19+
exports[`Fervex benefit + 3 if 5 >= expiresIn > 0 1`] = `
20+
Object {
21+
"benefit": 8,
22+
"expiresIn": 4,
23+
"name": "Fervex",
24+
}
25+
`;
26+
27+
exports[`Fervex benefit + 3 if 5 >= expiresIn > 0 2`] = `
28+
Object {
29+
"benefit": 11,
30+
"expiresIn": 0,
31+
"name": "Fervex",
32+
}
33+
`;
34+
35+
exports[`Fervex benefit drops to 0 if 0 >= expiresIn > -∞ 1`] = `
36+
Object {
37+
"benefit": 0,
38+
"expiresIn": -1,
39+
"name": "Fervex",
40+
}
41+
`;
42+
43+
exports[`Fervex should be serializable to JSON 1`] = `
44+
Object {
45+
"benefit": 5,
46+
"expiresIn": 30,
47+
"name": "Fervex",
48+
}
49+
`;
50+
51+
exports[`Fervex should be serializable to string 1`] = `"{\\"name\\":\\"Fervex\\",\\"expiresIn\\":30,\\"benefit\\":5}"`;
52+
53+
exports[`Fervex should increase benefit with time 1`] = `
54+
Object {
55+
"benefit": 6,
56+
"expiresIn": 29,
57+
"name": "Fervex",
58+
}
59+
`;
60+
61+
exports[`Fervex should not allow for a benefit greater than 50 1`] = `
62+
Object {
63+
"benefit": 50,
64+
"expiresIn": 30,
65+
"name": "Fervex",
66+
}
67+
`;
68+
69+
exports[`Fervex should not allow for a negative benefit 1`] = `
70+
Object {
71+
"benefit": 0,
72+
"expiresIn": 30,
73+
"name": "Fervex",
74+
}
75+
`;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Herbal Tea should be serializable to JSON 1`] = `
4+
Object {
5+
"benefit": 5,
6+
"expiresIn": 30,
7+
"name": "Herbal Tea",
8+
}
9+
`;
10+
11+
exports[`Herbal Tea should be serializable to string 1`] = `"{\\"name\\":\\"Herbal Tea\\",\\"expiresIn\\":30,\\"benefit\\":5}"`;
12+
13+
exports[`Herbal Tea should increase benefit twice as much if expired 1`] = `
14+
Object {
15+
"benefit": 7,
16+
"expiresIn": -1,
17+
"name": "Herbal Tea",
18+
}
19+
`;
20+
21+
exports[`Herbal Tea should increase benefit with time 1`] = `
22+
Object {
23+
"benefit": 6,
24+
"expiresIn": 29,
25+
"name": "Herbal Tea",
26+
}
27+
`;
28+
29+
exports[`Herbal Tea should not allow for a benefit greater than 50 1`] = `
30+
Object {
31+
"benefit": 50,
32+
"expiresIn": 30,
33+
"name": "Herbal Tea",
34+
}
35+
`;
36+
37+
exports[`Herbal Tea should not allow for a negative benefit 1`] = `
38+
Object {
39+
"benefit": 0,
40+
"expiresIn": 30,
41+
"name": "Herbal Tea",
42+
}
43+
`;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Magic Pill should be serializable to JSON 1`] = `
4+
Object {
5+
"benefit": 5,
6+
"expiresIn": 30,
7+
"name": "Magic Pill",
8+
}
9+
`;
10+
11+
exports[`Magic Pill should be serializable to string 1`] = `"{\\"name\\":\\"Magic Pill\\",\\"expiresIn\\":30,\\"benefit\\":5}"`;
12+
13+
exports[`Magic Pill should not allow for a benefit greater than 50 1`] = `
14+
Object {
15+
"benefit": 50,
16+
"expiresIn": 30,
17+
"name": "Magic Pill",
18+
}
19+
`;
20+
21+
exports[`Magic Pill should not allow for a negative benefit 1`] = `
22+
Object {
23+
"benefit": 0,
24+
"expiresIn": 30,
25+
"name": "Magic Pill",
26+
}
27+
`;
28+
29+
exports[`Magic Pill should not change expiration or benefit with time 1`] = `
30+
Object {
31+
"benefit": 5,
32+
"expiresIn": 30,
33+
"name": "Magic Pill",
34+
}
35+
`;
36+
37+
exports[`Magic Pill should not change expiration or benefit with time 2`] = `
38+
Object {
39+
"benefit": 5,
40+
"expiresIn": 0,
41+
"name": "Magic Pill",
42+
}
43+
`;
44+
45+
exports[`Magic Pill should not change expiration or benefit with time 3`] = `
46+
Object {
47+
"benefit": 5,
48+
"expiresIn": 30,
49+
"name": "Magic Pill",
50+
}
51+
`;

src/model/drug/doliprane.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Doliprane } from './doliprane';
2+
3+
describe('Doliprane', () => {
4+
let doliprane: Doliprane;
5+
6+
beforeEach(() => {
7+
doliprane = new Doliprane(30, 5);
8+
});
9+
10+
it('should not allow for a negative benefit', () => {
11+
doliprane.benefit = -5;
12+
expect(doliprane).toMatchSnapshot();
13+
});
14+
15+
it('should not allow for a benefit greater than 50', () => {
16+
doliprane.benefit = 51;
17+
expect(doliprane).toMatchSnapshot();
18+
});
19+
20+
it('should return expiration status', () => {
21+
expect(doliprane.isExpired()).toBe(false);
22+
doliprane.expiresIn = 0;
23+
expect(doliprane.isExpired()).toBe(true);
24+
});
25+
26+
it('should decrease expiresIn and benefit over time', () => {
27+
doliprane.updateValues();
28+
expect(doliprane).toMatchSnapshot();
29+
});
30+
31+
it('should decrease benefit twice as much if expired', () => {
32+
doliprane.expiresIn = 0;
33+
doliprane.updateValues();
34+
expect(doliprane).toMatchSnapshot();
35+
});
36+
37+
it('should be serializable to JSON', () => {
38+
expect(doliprane.toJSON()).toMatchSnapshot();
39+
});
40+
41+
it('should be serializable to string', () => {
42+
expect(doliprane.toString()).toMatchSnapshot();
43+
});
44+
});

src/model/drug/doliprane.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { DrugName } from '../../type/DrugName';
2+
import { Drug } from './drug';
3+
4+
export class Doliprane extends Drug {
5+
constructor(expiresIn: number, benefit: number) {
6+
super(DrugName.DOLIPRANE, expiresIn, benefit);
7+
}
8+
}

src/model/drug/drug.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { DrugName } from '../../type/DrugName';
2+
import { Drug } from './drug';
3+
4+
describe('Drug', () => {
5+
let drug: Drug;
6+
7+
beforeEach(() => {
8+
drug = new Drug(DrugName.TEST, 30, 5);
9+
});
10+
11+
it('should not allow for a negative benefit', () => {
12+
drug.benefit = -1;
13+
expect(drug).toMatchSnapshot();
14+
});
15+
16+
it('should not allow for a benefit greater than 50', () => {
17+
drug.benefit = 51;
18+
expect(drug).toMatchSnapshot();
19+
});
20+
21+
it('should return expiration status', () => {
22+
expect(drug.isExpired()).toBe(false);
23+
drug.expiresIn = 0;
24+
expect(drug.isExpired()).toBe(true);
25+
});
26+
27+
it('should decrease expiresIn and benefit over time', () => {
28+
drug.updateValues();
29+
expect(drug).toMatchSnapshot();
30+
});
31+
32+
it('should decrease benefit twice as much if expired', () => {
33+
drug.expiresIn = 0;
34+
drug.updateValues();
35+
expect(drug).toMatchSnapshot();
36+
});
37+
38+
it('should be serializable to JSON', () => {
39+
expect(drug.toJSON()).toMatchSnapshot();
40+
});
41+
42+
it('should be serializable to string', () => {
43+
expect(drug.toString()).toMatchSnapshot();
44+
});
45+
});

0 commit comments

Comments
 (0)