Update some of the exercises
This commit is contained in:
9
examples/basic-math/src/arithmetic.test.js
Normal file
9
examples/basic-math/src/arithmetic.test.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
describe.todo('add', () => {});
|
||||
|
||||
describe.todo('subtract', () => {});
|
||||
|
||||
describe.todo('multiply', () => {});
|
||||
|
||||
describe.todo('divide', () => {});
|
||||
@@ -1 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
16
examples/basic-math/src/counter.js
Normal file
16
examples/basic-math/src/counter.js
Normal file
@@ -0,0 +1,16 @@
|
||||
let value = 0;
|
||||
|
||||
export const counter = {
|
||||
get value() {
|
||||
return value;
|
||||
},
|
||||
increment() {
|
||||
value++;
|
||||
},
|
||||
decrement() {
|
||||
value--;
|
||||
},
|
||||
reset() {
|
||||
value = 0;
|
||||
},
|
||||
};
|
||||
21
examples/basic-math/src/counter.test.js
Normal file
21
examples/basic-math/src/counter.test.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { expect, it, describe } from 'vitest';
|
||||
import { counter } from './counter';
|
||||
|
||||
// For added fun, we can try `describe.shuffle`.
|
||||
describe.todo('Counter', () => {
|
||||
it('starts at zero', () => {
|
||||
expect(counter.value).toBe(0);
|
||||
});
|
||||
|
||||
it('can increment', () => {
|
||||
counter.increment();
|
||||
expect(counter.value).toBe(1);
|
||||
});
|
||||
|
||||
// Let's get this test to *not* fail.
|
||||
it('can decrement', () => {
|
||||
counter.increment();
|
||||
counter.decrement();
|
||||
expect(counter.value).toBe(0);
|
||||
});
|
||||
});
|
||||
1
examples/characters/README.md
Normal file
1
examples/characters/README.md
Normal file
@@ -0,0 +1 @@
|
||||
# Cool Kids Club
|
||||
1420
examples/characters/package-lock.json
generated
Normal file
1420
examples/characters/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
29
examples/characters/package.json
Normal file
29
examples/characters/package.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "basic-math",
|
||||
"version": "1.0.0",
|
||||
"description": "Let's talk about adding and subtracting numbers.",
|
||||
"main": "src/index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "vitest --ui",
|
||||
"test": "vitest"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/stevekinney/testing-javascript.git"
|
||||
},
|
||||
"author": "Steve Kinney <hello@stevekinney.net>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/stevekinney/testing-javascript/issues"
|
||||
},
|
||||
"homepage": "https://github.com/stevekinney/testing-javascript#readme",
|
||||
"devDependencies": {
|
||||
"@vitest/ui": "^2.1.1",
|
||||
"vite": "^5.4.5",
|
||||
"vitest": "^2.1.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"uuid": "^10.0.0"
|
||||
}
|
||||
}
|
||||
14
examples/characters/src/characrer.test.js
Normal file
14
examples/characters/src/characrer.test.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { Character } from './character.js';
|
||||
import { Person } from './person.js';
|
||||
|
||||
describe('Character', () => {
|
||||
it.todo(
|
||||
'should create a character with a first name, last name, and role',
|
||||
() => {},
|
||||
);
|
||||
|
||||
it.todo('should allow you to increase the level', () => {});
|
||||
|
||||
it.todo('should update the last modified date when leveling up', () => {});
|
||||
});
|
||||
26
examples/characters/src/character.js
Normal file
26
examples/characters/src/character.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Person } from './person.js';
|
||||
import { rollDice } from './roll-dice.js';
|
||||
|
||||
export class Character extends Person {
|
||||
constructor(firstName, lastName, role) {
|
||||
super(firstName, lastName);
|
||||
|
||||
this.role = role;
|
||||
this.level = 1;
|
||||
|
||||
this.createdAt = new Date();
|
||||
this.lastModified = this.createdAt;
|
||||
|
||||
this.strength = rollDice(4, 6);
|
||||
this.dexterity = rollDice(4, 6);
|
||||
this.intelligence = rollDice(4, 6);
|
||||
this.wisdom = rollDice(4, 6);
|
||||
this.charisma = rollDice(4, 6);
|
||||
this.constitution = rollDice(4, 6);
|
||||
}
|
||||
|
||||
levelUp() {
|
||||
this.level++;
|
||||
this.lastModified = new Date();
|
||||
}
|
||||
}
|
||||
17
examples/characters/src/person.js
Normal file
17
examples/characters/src/person.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { v4 as id } from 'uuid';
|
||||
|
||||
export class Person {
|
||||
constructor(firstName, lastName) {
|
||||
if (!firstName || !lastName) {
|
||||
throw new Error('First name and last name are required');
|
||||
}
|
||||
|
||||
this.id = 'person-' + id();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
get fullName() {
|
||||
return `${this.firstName} ${this.lastName}`;
|
||||
}
|
||||
}
|
||||
27
examples/characters/src/person.test.js
Normal file
27
examples/characters/src/person.test.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { Person } from './person.js';
|
||||
|
||||
// Remove the `todo` from the `describe` to run the tests.
|
||||
describe.todo('Person', () => {
|
||||
// This test will fail. Why?
|
||||
it('should create a person with a first name and last name', () => {
|
||||
const person = new Person('Grace', 'Hopper');
|
||||
expect(person).toEqual({
|
||||
firstName: 'Grace',
|
||||
lastName: 'Hopper',
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error if first name or last name is missing', () => {
|
||||
expect(() => new Person('Grace')).toThrow(
|
||||
'First name and last name are required',
|
||||
);
|
||||
|
||||
expect(() => new Person()).toThrow('First name and last name are required');
|
||||
});
|
||||
|
||||
it('should return the full name', () => {
|
||||
const person = new Person('Grace', 'Hopper');
|
||||
expect(person.fullName).toBe('Grace Hopper');
|
||||
});
|
||||
});
|
||||
16
examples/characters/src/roll-dice.js
Normal file
16
examples/characters/src/roll-dice.js
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Rolls a specified number of dice with a specified number of sides,
|
||||
* drops the lowest roll, and returns the sum of the remaining rolls.
|
||||
* @param {number} diceCount - The number of dice to roll.
|
||||
* @param {number} diceSides - The number of sides on each die.
|
||||
*/
|
||||
export const rollDice = (diceCount = 4, diceSides = 6) => {
|
||||
const rolls = [];
|
||||
|
||||
for (let i = 0; i < diceCount; i++) {
|
||||
rolls.push(Math.floor(Math.random() * diceSides) + 1);
|
||||
}
|
||||
|
||||
rolls.sort((a, b) => a - b); // Sort rolls to drop the lowest one
|
||||
return rolls.slice(1).reduce((acc, curr) => acc + curr, 0); // Sum the top 3 rolls
|
||||
};
|
||||
5
examples/characters/vitest.config.js
Normal file
5
examples/characters/vitest.config.js
Normal file
@@ -0,0 +1,5 @@
|
||||
export default {
|
||||
test: {
|
||||
environment: 'node',
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user