Skip to content

Commit 35daa54

Browse files
committed
Added more tests, refactored the code and added the CI script
Signed-off-by: Carpinisan George <[email protected]>
1 parent a2a2aff commit 35daa54

File tree

3 files changed

+92
-42
lines changed

3 files changed

+92
-42
lines changed

.github/workflows/tests.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
name: Run Tests
22

33
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
branches:
10+
- main
411

512
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Setup
18+
uses: actions/setup-node@v2
19+
with:
20+
node-version: '18'
21+
22+
- name: Install dependencies
23+
run: |
24+
sudo apt install npm
25+
npm install
26+
- name: Run the tests
27+
run: npm run test:jest

app/gilded-rose.ts

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,46 @@ export class GildedRose {
1717
this.items = items;
1818
}
1919

20+
updateConcertTicket(item: Item) {
21+
item.quality += 1
22+
if (item.sellIn < 11)
23+
item.quality += 1
24+
25+
if (item.sellIn < 6)
26+
item.quality += 1
27+
28+
if (item.quality >= 50)
29+
item.quality = 50
30+
}
31+
2032
updateQuality() {
2133
for (let i = 0; i < this.items.length; i++) {
22-
if (this.items[i].name != 'Aged Brie' && this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
23-
if (this.items[i].quality > 0) {
24-
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
25-
this.items[i].quality = this.items[i].quality - 1
26-
}
27-
}
34+
if (this.items[i].name == 'Sulfuras, Hand of Ragnaros')
35+
break
36+
37+
this.items[i].sellIn -= 1
38+
39+
if (this.items[i].quality == 50 || this.items[i].quality == 0)
40+
break
41+
42+
if (this.items[i].name == 'Aged Brie') {
43+
this.items[i].quality += 1
44+
break
45+
} else if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
46+
this.updateConcertTicket(this.items[i])
47+
} else if (this.items[i].name == 'Conjured') {
48+
this.items[i].quality -= 2
2849
} else {
29-
if (this.items[i].quality < 50) {
30-
this.items[i].quality = this.items[i].quality + 1
31-
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert') {
32-
if (this.items[i].sellIn < 11) {
33-
if (this.items[i].quality < 50) {
34-
this.items[i].quality = this.items[i].quality + 1
35-
}
36-
}
37-
if (this.items[i].sellIn < 6) {
38-
if (this.items[i].quality < 50) {
39-
this.items[i].quality = this.items[i].quality + 1
40-
}
41-
}
42-
}
43-
}
44-
}
45-
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
46-
this.items[i].sellIn = this.items[i].sellIn - 1;
50+
this.items[i].quality -= 1
4751
}
52+
4853
if (this.items[i].sellIn < 0) {
49-
if (this.items[i].name != 'Aged Brie') {
50-
if (this.items[i].name != 'Backstage passes to a TAFKAL80ETC concert') {
51-
if (this.items[i].quality > 0) {
52-
if (this.items[i].name != 'Sulfuras, Hand of Ragnaros') {
53-
this.items[i].quality = this.items[i].quality - 1
54-
}
55-
}
56-
} else {
57-
this.items[i].quality = this.items[i].quality - this.items[i].quality
58-
}
59-
} else {
60-
if (this.items[i].quality < 50) {
61-
this.items[i].quality = this.items[i].quality + 1
62-
}
63-
}
54+
if (this.items[i].name == 'Backstage passes to a TAFKAL80ETC concert')
55+
this.items[i].quality = 0
6456
}
57+
6558
}
6659

6760
return this.items;
6861
}
69-
}
62+
}

test/jest/gilded-rose.spec.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('Gilded Rose', () => {
99
const items = gildedRose.updateQuality();
1010

1111
// Assert
12-
expect(items[0].name).toBe('bar');
12+
expect(items[0].name).toBe('foo');
1313
});
1414

1515
it('sword quality drops by 1', () => {
@@ -20,6 +20,41 @@ describe('Gilded Rose', () => {
2020
const items = gildedRose.updateQuality();
2121

2222
// Assert
23-
expect(items[0].quality).toBe(1);
23+
expect(items[0].quality).toBe(0);
24+
})
25+
26+
it('Sulfuras quality doesn\'t drop', () => {
27+
const gildedRose = new GildedRose([new Item('Sulfuras, Hand of Ragnaros', 0, 80)]);
28+
29+
const items = gildedRose.updateQuality();
30+
31+
expect(items[0].quality).toBe(80);
32+
})
33+
34+
it('Aged brie increases in Quality', () => {
35+
const gildedRose = new GildedRose([new Item('Aged Brie', 10, 30)]);
36+
37+
const items = gildedRose.updateQuality();
38+
39+
expect(items[0].quality).toBe(31);
40+
})
41+
42+
it('Item quality doesn\'t exceed 50/drop below 0', () => {
43+
const gildedRose1 = new GildedRose([new Item('Shield', 10, 0)]);
44+
const gildedRose2 = new GildedRose([new Item('Backstage passes to a TAFKAL80ETC concert', 5, 48)]);
45+
46+
const item1 = gildedRose1.updateQuality();
47+
const item2 = gildedRose2.updateQuality();
48+
49+
expect(item1[0].quality).toBe(0);
50+
expect(item2[0].quality).toBe(50);
51+
})
52+
53+
it('Conjured items degrade twice as fast', () => {
54+
const gildedRose = new GildedRose([new Item('Conjured', 5, 20)]);
55+
56+
const items = gildedRose.updateQuality();
57+
58+
expect(items[0].quality).toBe(18);
2459
})
2560
});

0 commit comments

Comments
 (0)