Skip to content

Commit

Permalink
New and updated tests for default values in modifiers functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
parnas committed Nov 27, 2023
1 parent b9d9df7 commit ac9b0b6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 21 deletions.
33 changes: 30 additions & 3 deletions tests/modifiers/ComparisonModifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,40 @@ describe('ComparisonModifier', () => {
});

describe('Run', () => {
const results = new RollResults();
const die = new StandardDice(6, 2);
let mod;

beforeEach(() => {
mod = new ComparisonModifier();
});

test('returns RollResults object', () => {
const results = new RollResults();
const die = new StandardDice(6, 2);
const mod = new ComparisonModifier(new ComparePoint('=', 4));
mod.comparePoint = new ComparePoint('=', 4);

expect(mod.run(results, die)).toBe(results);
});

describe('Defaults', () => {
const testOperator = '=';
const testValue = 6;

beforeEach(() => {
mod.defaultComparePoint = () => [testOperator, testValue];
});

test('uses default comparison modifier', () => {
mod.run(results, die);
expect(mod.comparePoint.operator).toEqual(testOperator);
expect(mod.comparePoint.value).toEqual(testValue);
});

test('does not set default comparison modifier if value was provided', () => {
mod.comparePoint = new ComparePoint('=', 4);

expect(mod.run(results, die)).toBe(results);
});
});
});

describe('Readonly properties', () => {
Expand Down
14 changes: 11 additions & 3 deletions tests/modifiers/ExplodeModifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ describe('ExplodeModifier', () => {
expect(mod.run(results, die)).toBe(results);
});

test('does not explode without compare point', () => {
test('can explode with default compare point', () => {
const modifiedResults = mod.run(results, die).rolls;

// assert that all the rolls exist
expect(modifiedResults).toBeInstanceOf(Array);
expect(modifiedResults).toHaveLength(6);
expect(modifiedResults).toHaveLength(8);

expect(modifiedResults[0].initialValue).toBe(8);
expect(modifiedResults[0].value).toBe(8);
Expand All @@ -253,7 +253,15 @@ describe('ExplodeModifier', () => {

expect(modifiedResults[5].initialValue).toBe(10);
expect(modifiedResults[5].value).toBe(10);
expect(modifiedResults[5].modifiers).toEqual(new Set());
expect(modifiedResults[5].modifiers).toEqual(new Set(['explode']));

expect(modifiedResults[6].initialValue).toBe(10);
expect(modifiedResults[6].value).toBe(10);
expect(modifiedResults[6].modifiers).toEqual(new Set(['explode']));

expect(modifiedResults[7].initialValue).toBe(2);
expect(modifiedResults[7].value).toBe(2);
expect(modifiedResults[7].modifiers).toEqual(new Set());
});

test('can explode with compare point `>=8`', () => {
Expand Down
22 changes: 22 additions & 0 deletions tests/modifiers/Modifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ describe('Modifier', () => {
test('returns RollResults object', () => {
expect(mod.run(results, die)).toBe(results);
});

describe('Defaults', () => {
const testKey = 'testKey';
const defaultValue = 'defaultValue';

beforeEach(() => {
mod.defaults = () => ({ testKey: defaultValue });
});

test('uses default if value wasn\'t provided', () => {
mod.run(results, die);
expect(mod[testKey]).toEqual(defaultValue);
});

test('does not set default if value was provided', () => {
const providedValue = 'providedValue';
mod[testKey] = providedValue;

mod.run(results, die);
expect(mod[testKey]).toEqual(providedValue);
});
});
});
});
});
6 changes: 3 additions & 3 deletions tests/modifiers/ReRollModifier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ describe('ReRollModifier', () => {
expect(mod.run(results, die)).toBe(results);
});

test('does not re-roll without compare point', () => {
test('does re-roll with default compare point', () => {
const modifiedResults = mod.run(results, die).rolls;

expect(modifiedResults).toBeInstanceOf(Array);
Expand All @@ -201,8 +201,8 @@ describe('ReRollModifier', () => {
expect(modifiedResults[2].modifiers).toEqual(new Set());

expect(modifiedResults[3].initialValue).toBe(1);
expect(modifiedResults[3].value).toBe(1);
expect(modifiedResults[3].modifiers).toEqual(new Set());
expect(modifiedResults[3].value).toBe(10);
expect(modifiedResults[3].modifiers).toEqual(new Set(new Set(['re-roll'])));

expect(modifiedResults[4].initialValue).toBe(6);
expect(modifiedResults[4].value).toBe(6);
Expand Down
21 changes: 9 additions & 12 deletions tests/parser/Parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,6 @@ describe('Parser', () => {
}),
}));
});

test('throws error if no compare point', () => {
expect(() => {
Parser.parse('d6cf');
}).toThrow(parser.SyntaxError);
});
});

describe('Critical success', () => {
Expand Down Expand Up @@ -305,12 +299,6 @@ describe('Parser', () => {
}),
}));
});

test('throws error if no compare point', () => {
expect(() => {
Parser.parse('d6cs');
}).toThrow(parser.SyntaxError);
});
});

describe('Drop', () => {
Expand Down Expand Up @@ -420,6 +408,7 @@ describe('Parser', () => {

const mod = parsed[0].modifiers.get('explode');
expect(mod).toBeInstanceOf(ExplodeModifier);
mod.useDefaultsIfNeeded(parsed[0]);
expect(mod.toJSON()).toEqual(expect.objectContaining({
comparePoint: expect.objectContaining({
operator: '=',
Expand All @@ -444,6 +433,7 @@ describe('Parser', () => {

const mod = parsed[0].modifiers.get('explode');
expect(mod).toBeInstanceOf(ExplodeModifier);
mod.useDefaultsIfNeeded(parsed[0]);
expect(mod.toJSON()).toEqual(expect.objectContaining({
comparePoint: expect.objectContaining({
operator: '=',
Expand All @@ -468,6 +458,7 @@ describe('Parser', () => {

const mod = parsed[0].modifiers.get('explode');
expect(mod).toBeInstanceOf(ExplodeModifier);
mod.useDefaultsIfNeeded(parsed[0]);
expect(mod.toJSON()).toEqual(expect.objectContaining({
comparePoint: expect.objectContaining({
operator: '=',
Expand All @@ -492,6 +483,7 @@ describe('Parser', () => {

const mod = parsed[0].modifiers.get('explode');
expect(mod).toBeInstanceOf(ExplodeModifier);
mod.useDefaultsIfNeeded(parsed[0]);
expect(mod.toJSON()).toEqual(expect.objectContaining({
comparePoint: expect.objectContaining({
operator: '=',
Expand Down Expand Up @@ -826,6 +818,7 @@ describe('Parser', () => {

const mod = parsed[0].modifiers.get('re-roll');
expect(mod).toBeInstanceOf(ReRollModifier);
mod.useDefaultsIfNeeded(parsed[0]);
expect(mod.toJSON()).toEqual(expect.objectContaining({
once: false,
comparePoint: expect.objectContaining({
Expand All @@ -849,6 +842,7 @@ describe('Parser', () => {

const mod = parsed[0].modifiers.get('re-roll');
expect(mod).toBeInstanceOf(ReRollModifier);
mod.useDefaultsIfNeeded(parsed[0]);
expect(mod.toJSON()).toEqual(expect.objectContaining({
once: false,
comparePoint: expect.objectContaining({
Expand Down Expand Up @@ -895,6 +889,7 @@ describe('Parser', () => {

const mod = parsed[0].modifiers.get('re-roll');
expect(mod).toBeInstanceOf(ReRollModifier);
mod.useDefaultsIfNeeded(parsed[0]);
expect(mod.toJSON()).toEqual(expect.objectContaining({
once: true,
comparePoint: expect.objectContaining({
Expand Down Expand Up @@ -1669,6 +1664,7 @@ describe('Parser', () => {

let mod = parsed[0].modifiers.get('explode');
expect(mod).toBeInstanceOf(ExplodeModifier);
mod.useDefaultsIfNeeded(parsed[0]);
expect(mod.toJSON()).toEqual(expect.objectContaining({
comparePoint: expect.objectContaining({
operator: '=',
Expand All @@ -1691,6 +1687,7 @@ describe('Parser', () => {

mod = parsed[5].modifiers.get('re-roll');
expect(mod).toBeInstanceOf(ReRollModifier);
mod.useDefaultsIfNeeded(parsed[0]);
expect(mod.toJSON()).toEqual(expect.objectContaining({
once: false,
comparePoint: expect.objectContaining({
Expand Down

0 comments on commit ac9b0b6

Please sign in to comment.