Explorations with mocking

This commit is contained in:
Steve Kinney
2024-10-02 15:38:38 -05:00
parent 9cb1a0c739
commit 2552cb26d5
2 changed files with 61 additions and 15 deletions

View File

@@ -2,21 +2,21 @@ import { Person } from './person.js';
import { rollDice } from './roll-dice.js';
export class Character extends Person {
constructor(firstName, lastName, role) {
constructor(firstName, lastName, role, level = 1, roll = rollDice) {
super(firstName, lastName);
this.role = role;
this.level = 1;
this.level = level;
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);
this.strength = roll(4, 6);
this.dexterity = roll(4, 6);
this.intelligence = roll(4, 6);
this.wisdom = roll(4, 6);
this.charisma = roll(4, 6);
this.constitution = roll(4, 6);
}
levelUp() {

View File

@@ -1,14 +1,60 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect, vi } from 'vitest';
import { Character } from './character.js';
import { Person } from './person.js';
const firstName = 'Ada';
const lastName = 'Lovelace';
const role = 'Computer Scienst';
describe('Character', () => {
it.todo(
'should create a character with a first name, last name, and role',
() => {},
);
let character;
it.todo('should allow you to increase the level', () => {});
it.todo('should update the last modified date when leveling up', () => {});
beforeEach(() => {
character = new Character(firstName, lastName, role, 1);
});
it.skip('should create a character with a first name, last name, and role', () => {
expect(character).toEqual({
firstName,
lastName,
role,
strength: 12,
wisdom: 12,
dexterity: 12,
intelligence: 12,
constitution: 12,
charisma: 12,
level: 1,
lastModified: expect.any(Date),
createdAt: expect.any(Date),
id: expect.stringContaining('person-'),
});
});
it('should allow you to increase the level', () => {
const initialLevel = character.level;
character.levelUp();
expect(character.level).toBeGreaterThan(initialLevel);
});
it('should update the last modified date when leveling up', () => {
const initialLastModified = character.lastModified;
character.levelUp();
expect(character.lastModified).not.toBe(initialLastModified);
});
it.only('should roll four six-sided die', () => {
const rollDiceMock = vi.fn(() => 15);
const character = new Character(firstName, lastName, role, 1, rollDiceMock);
expect(character.strength).toBe(15);
expect(rollDiceMock).toHaveBeenCalledWith(4, 6);
expect(rollDiceMock).toHaveBeenCalledTimes(6);
console.log(rollDiceMock.mock.calls);
});
});