Browse Source

add jest; add sketched factory-class

main
Oliver Stingl 2 years ago
parent
commit
81f0d8f068
  1. 3
      jest.config.js
  2. 11656
      package-lock.json
  3. 21
      package.json
  4. 144
      src/models/Factory.js
  5. 12
      tests/unit/example.spec.js

3
jest.config.js

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
module.exports = {
preset: '@vue/cli-plugin-unit-jest'
}

11656
package-lock.json generated

File diff suppressed because it is too large Load Diff

21
package.json

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint"
},
"dependencies": {
@ -19,10 +20,15 @@ @@ -19,10 +20,15 @@
"@vue/cli-plugin-babel": "~5.0.0",
"@vue/cli-plugin-eslint": "~5.0.0",
"@vue/cli-plugin-router": "~5.0.0",
"@vue/cli-plugin-unit-jest": "~5.0.0",
"@vue/cli-plugin-vuex": "~5.0.0",
"@vue/cli-service": "~5.0.0",
"@vue/test-utils": "^2.0.0-0",
"@vue/vue3-jest": "^27.0.0-alpha.1",
"babel-jest": "^27.0.6",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3"
"eslint-plugin-vue": "^8.0.3",
"jest": "^27.0.5"
},
"eslintConfig": {
"root": true,
@ -36,7 +42,18 @@ @@ -36,7 +42,18 @@
"parserOptions": {
"parser": "@babel/eslint-parser"
},
"rules": {}
"rules": {},
"overrides": [
{
"files": [
"**/__tests__/*.{j,t}s?(x)",
"**/tests/unit/**/*.spec.{j,t}s?(x)"
],
"env": {
"jest": true
}
}
]
},
"browserslist": [
"> 1%",

144
src/models/Factory.js

@ -0,0 +1,144 @@ @@ -0,0 +1,144 @@
const ITEM_IRON_ORE = 1;
const ITEM_IRON_BAR = 2;
const ITEM_COAL = 3;
class Factory {
static get price() {
return 400;
}
constructor() {
this.running = false;
this.input = [
{
id: ITEM_IRON_ORE,
amount: 30
},
{
id: ITEM_COAL,
amount: 10
}
];
this.inventory = [];
this.inventoryLimit = 24;
this.name = '';
this.rate = 1;
this.interval = null;
this.itemToGenerate = {
id: ITEM_IRON_BAR,
name: 'Iron bar',
recipe: {
requires: [
{
id: ITEM_IRON_ORE,
name: 'Iron ore',
amount: 3
},
{
id: ITEM_COAL,
name: 'Coal',
amount: 1
}
],
output: 1,
secondsToProduce: 5
}
}
}
start() {
if (!this.itemToGenerate) return false;
if (this.isInventoryFull()) return false;
this.interval = setInterval(() => {
this.produce();
}, this.itemToGenerate.recipe.secondsToProduce * (1000 / this.rate));
this.running = true;
console.info('▶ Production has started');
}
stop() {
clearInterval(this.interval);
this.interval = null;
this.running = false;
console.info('🛑 Production has stopped');
}
produce() {
if (this.isInventoryFull()) {
this.stop();
console.warn('⚠ Inventory is full - Production stopped');
return false;
}
let productionIssueOccured = false;
this.itemToGenerate.recipe.requires.forEach((requiredItem) => {
let inputItem = this.input.find(item => item.id === requiredItem.id);
if (!inputItem || inputItem.amount <= 0 || requiredItem.amount > inputItem.amount) {
console.warn('⚠ Not enough '+requiredItem.name+' to produce '+this.itemToGenerate.name+' - requires '+requiredItem.amount+', got '+inputItem.amount);
productionIssueOccured = true;
return false;
}
this.input.find(item => item.id === requiredItem.id).amount -= requiredItem.amount;
});
if (productionIssueOccured) {
this.stop();
return false;
}
this.inventory.push(this.itemToGenerate);
console.info('Produced 1 '+this.itemToGenerate.name+' ('+this.getItemCountInInventory(this.itemToGenerate.id)+')');
}
getItemCountInInventory(id) {
return this.inventory.filter(item => item.id === id).length;
}
isInventoryFull() {
return this.inventory.length >= this.inventoryLimit;
}
}
class Cluster {
constructor() {
this.inventory = [];
this.factories = [];
}
startAll() {
this.factories.forEach((factory) => {
factory.start();
})
}
stopAll() {
this.factories.forEach((factory) => {
factory.stop();
})
}
}
class Game {
constructor() {
this.money = 1_000;
this.cluster = new Cluster();
this.inventory = [];
}
buyFactory() {
if (this.money >= Factory.price) {
this.cluster.factories.push(new Factory());
this.money -= Factory.price;
} else {
console.warn('💰 Not enough money to buy a factory');
}
}
}
let game = new Game();
game.buyFactory();
game.cluster.startAll();

12
tests/unit/example.spec.js

@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
import { shallowMount } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'
describe('HelloWorld.vue', () => {
it('renders props.msg when passed', () => {
const msg = 'new message'
const wrapper = shallowMount(HelloWorld, {
props: { msg }
})
expect(wrapper.text()).toMatch(msg)
})
})
Loading…
Cancel
Save