Explorations with mocking
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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', () => {});
|
||||
beforeEach(() => {
|
||||
character = new Character(firstName, lastName, role, 1);
|
||||
});
|
||||
|
||||
it.todo('should update the last modified date when leveling up', () => {});
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user