Skip to content

Commit c7f0d12

Browse files
committed
ex-2: extract historyLengthFactor out of Rating.voyageAndHistoryLengthFactor
1 parent b523010 commit c7f0d12

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/using-polymorphism-for-variation/rating/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ export class Rating {
3636

3737
get voyageAndHistoryLengthFactor() {
3838
let result = 0;
39-
if (this.history.length > 8) result += 1;
39+
result += this.historyLengthFactor;
4040
if (this.voyage.length > 14) result -= 1;
4141
return result;
4242
}
4343

44+
get historyLengthFactor() {
45+
return this.history.length > 8 ? 1 : 0;
46+
}
47+
4448
get hasChinaHistory() {
4549
return this.history.some(v => 'china' === v.zone);
4650
}

src/using-polymorphism-for-variation/rating/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ describe('Rating', () => {
140140
expect(rating.voyageAndHistoryLengthFactor).toEqual(-1);
141141
});
142142
});
143+
144+
describe('historyLengthFactor', () => {
145+
it('should return 0 if history length is less than 8', () => {
146+
const history = Array.from({ length: 7 }, () => latamVoyage);
147+
const rating = new Rating(latamVoyage, history);
148+
expect(rating.historyLengthFactor).toEqual(0);
149+
});
150+
151+
it('should return 1 if history length is more than 8', () => {
152+
const history = Array.from({ length: 9 }, () => latamVoyage);
153+
const rating = new Rating(latamVoyage, history);
154+
expect(rating.historyLengthFactor).toEqual(1);
155+
});
156+
});
143157
});
144158

145159
describe('ExperiencedChinaRating', () => {

0 commit comments

Comments
 (0)