mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 09:11:39 +00:00
Move Legendary item logic to legendary-item-behavior
This commit is contained in:
parent
6c4e56174c
commit
09e9ec6e17
@ -3,6 +3,7 @@ import { getUpdateBehaviorFor } from "./behavior-resolver";
|
||||
import { AgedBrieBehavior } from "./implementations/aged-brie/aged-brie-behavior";
|
||||
import { LegacyBehavior } from "./implementations/legacy-behavior";
|
||||
import { BackstagePassBehavior } from "./implementations/backstage-pass/backstage-pass-behavior";
|
||||
import { LegendaryItemBehavior } from "./implementations/legendary-item/legendary-item-behavior";
|
||||
|
||||
describe("Behavior resolver", () => {
|
||||
it("should correctly resolve Aged Brie", () => {
|
||||
@ -19,6 +20,12 @@ describe("Behavior resolver", () => {
|
||||
).toBeInstanceOf(BackstagePassBehavior);
|
||||
});
|
||||
|
||||
it("should correctly resolve Legendary Items", () => {
|
||||
expect(
|
||||
getUpdateBehaviorFor(new Item("Sulfuras, Hand of Ragnaros", 0, 0))
|
||||
).toBeInstanceOf(LegendaryItemBehavior);
|
||||
});
|
||||
|
||||
it("should correctly resolve the rest to Legacy behavior", () => {
|
||||
expect(
|
||||
getUpdateBehaviorFor(new Item("some other item", 0, 0))
|
||||
|
||||
@ -3,6 +3,7 @@ import { IUpdateBehavior } from "./update-behavior.interface";
|
||||
import { LegacyBehavior } from "./implementations/legacy-behavior";
|
||||
import { AgedBrieBehavior } from "./implementations/aged-brie/aged-brie-behavior";
|
||||
import { BackstagePassBehavior } from "./implementations/backstage-pass/backstage-pass-behavior";
|
||||
import { LegendaryItemBehavior } from "./implementations/legendary-item/legendary-item-behavior";
|
||||
|
||||
export function getUpdateBehaviorFor(item: Item): IUpdateBehavior {
|
||||
switch (item.name) {
|
||||
@ -10,6 +11,8 @@ export function getUpdateBehaviorFor(item: Item): IUpdateBehavior {
|
||||
return new AgedBrieBehavior(item);
|
||||
case "Backstage passes to a TAFKAL80ETC concert":
|
||||
return new BackstagePassBehavior(item);
|
||||
case "Sulfuras, Hand of Ragnaros":
|
||||
return new LegendaryItemBehavior(item);
|
||||
default:
|
||||
return new LegacyBehavior(item);
|
||||
}
|
||||
|
||||
@ -37,18 +37,4 @@ describe("Legacy Behavior", () => {
|
||||
quality: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("should not change quality of Sulfuras", () => {
|
||||
const behavior = new LegacyBehavior(
|
||||
new Item("Sulfuras, Hand of Ragnaros", 0, 80)
|
||||
);
|
||||
|
||||
const result = behavior.update();
|
||||
|
||||
expect(result).toMatchObject({
|
||||
name: "Sulfuras, Hand of Ragnaros",
|
||||
sellIn: 0,
|
||||
quality: 80,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -6,19 +6,13 @@ export class LegacyBehavior implements IUpdateBehavior {
|
||||
|
||||
update(): Item {
|
||||
if (this.item.quality > 0) {
|
||||
if (this.item.name !== "Sulfuras, Hand of Ragnaros") {
|
||||
this.item.quality = this.item.quality - 1;
|
||||
}
|
||||
this.item.quality = this.item.quality - 1;
|
||||
}
|
||||
|
||||
if (this.item.name !== "Sulfuras, Hand of Ragnaros") {
|
||||
this.item.sellIn = this.item.sellIn - 1;
|
||||
}
|
||||
this.item.sellIn = this.item.sellIn - 1;
|
||||
if (this.item.sellIn < 0) {
|
||||
if (this.item.quality > 0) {
|
||||
if (this.item.name !== "Sulfuras, Hand of Ragnaros") {
|
||||
this.item.quality = this.item.quality - 1;
|
||||
}
|
||||
this.item.quality = this.item.quality - 1;
|
||||
}
|
||||
}
|
||||
return this.item;
|
||||
|
||||
@ -0,0 +1,13 @@
|
||||
import { config } from "@app/config";
|
||||
import { Item } from "@app/item";
|
||||
import { IUpdateBehavior } from "@app/update-behaviors/update-behavior.interface";
|
||||
|
||||
export class LegendaryItemBehavior implements IUpdateBehavior {
|
||||
constructor(public item: Item) {}
|
||||
update(): Item {
|
||||
if (this.item.quality !== 80) {
|
||||
throw new Error("A Legendary Item cannot have a quality other than 80");
|
||||
}
|
||||
return this.item;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
import { Item } from "@app/item";
|
||||
import { LegendaryItemBehavior } from "./legendary-item-behavior";
|
||||
|
||||
describe("Legendary Item Behavior", () => {
|
||||
it("should keep items of quality 80 the same", () => {
|
||||
const behavior = new LegendaryItemBehavior(
|
||||
new Item("Sulfuras, Hand of Ragnaros", 0, 80)
|
||||
);
|
||||
|
||||
const result = behavior.update();
|
||||
|
||||
expect(result).toMatchObject({
|
||||
name: "Sulfuras, Hand of Ragnaros",
|
||||
sellIn: 0,
|
||||
quality: 80,
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw an error when a legendary item doesn't have a quality of 80", () => {
|
||||
const behavior = new LegendaryItemBehavior(
|
||||
new Item("Sulfuras, Hand of Ragnaros", 0, 5)
|
||||
);
|
||||
|
||||
expect(() => behavior.update()).toThrow(
|
||||
"A Legendary Item cannot have a quality other than 80"
|
||||
);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user