diff --git a/module/__tests__/README.md b/module/__tests__/README.md new file mode 100644 index 0000000..e90289f --- /dev/null +++ b/module/__tests__/README.md @@ -0,0 +1,5 @@ +## Testing + +The stat-tracker module utilizes [quench](https://foundryvtt.com/packages/quench) +for it's end-to-end tests and unit tests, enabling us to be sure that the module +is as stable as possible and detect when there are breaking changes. diff --git a/module/__tests__/registration.mjs b/module/__tests__/registration.mjs new file mode 100644 index 0000000..787c4f1 --- /dev/null +++ b/module/__tests__/registration.mjs @@ -0,0 +1,9 @@ +import { barGraphTests } from "./schemas/barGraph.mjs"; +import { numberBucketTests } from "./schemas/numberBucket.mjs"; +import { stringBucketTests } from "./schemas/stringBucket.mjs"; + +Hooks.on(`quenchReady`, (quench) => { + numberBucketTests(quench); + stringBucketTests(quench); + barGraphTests(quench); +}); diff --git a/module/__tests__/schemas/barGraph.mjs b/module/__tests__/schemas/barGraph.mjs new file mode 100644 index 0000000..a2b9fea --- /dev/null +++ b/module/__tests__/schemas/barGraph.mjs @@ -0,0 +1,51 @@ +import { api } from "../../api.mjs"; + +export function barGraphTests(quench) { + quench.registerBatch( + `${__ID__}.barGraphSchema`, + (ctx) => { + const { describe, it, expect } = ctx; + + describe(`the bar graph schema`, () => { + it(`should default any additional properties left out`, () => { + const { value, error } = api.schemas.graphs.bar.validate( + { type: `bar` }, + ); + expect(value).to.have.keys(`type`, `stacked`, `showEmptyBuckets`); + expect(error).to.be.undefined; + }); + + it(`should allow stacked to be provided specifically`, () => { + const { value, error } = api.schemas.graphs.bar.validate( + { type: `bar`, stacked: true }, + ); + expect(value).to.have.keys(`type`, `stacked`, `showEmptyBuckets`); + expect(error).to.be.undefined; + }); + + it(`should allow showEmptyBuckets to be provided specifically`, () => { + const { value, error } = api.schemas.graphs.bar.validate( + { type: `bar`, showEmptyBuckets: true }, + ); + expect(value).to.have.keys(`type`, `stacked`, `showEmptyBuckets`); + expect(error).to.be.undefined; + }); + + it(`should only allow showEmptyBuckets to be a boolean`, () => { + const { value, error } = api.schemas.graphs.bar.validate( + { type: `bar`, showEmptyBuckets: `a potato` }, + ); + expect(value).to.have.keys(`type`, `stacked`, `showEmptyBuckets`); + expect(error).not.to.be.undefined; + }); + + it(`should only allow stacked to be a boolean`, () => { + const { error } = api.schemas.graphs.bar.validate( + { type: `bar`, stacked: `a potato` }, + ); + expect(error).not.to.be.undefined; + }); + }); + }, + ); +}; diff --git a/module/__tests__/schemas/numberBucket.mjs b/module/__tests__/schemas/numberBucket.mjs new file mode 100644 index 0000000..2ebba9a --- /dev/null +++ b/module/__tests__/schemas/numberBucket.mjs @@ -0,0 +1,61 @@ +import { api } from "../../api.mjs"; + +export function numberBucketTests(quench) { + quench.registerBatch( + `${__ID__}.numberBucketSchema`, + (ctx) => { + const { describe, it, expect } = ctx; + + describe(`the number bucket schema`, () => { + it(`should allow all additional properties to be left out`, () => { + const { error } = api.schemas.buckets.number.validate( + { type: `number` }, + ); + expect(error).to.be.undefined; + }); + + it(`should allow the min additional property if only it is provided with the type`, () => { + const { error } = api.schemas.buckets.number.validate( + { type: `number`, min: 0 }, + ); + expect(error).to.be.undefined; + }); + + it(`should allow the max additional property if only it is provided with the type`, () => { + const { error } = api.schemas.buckets.number.validate( + { type: `number`, max: 10 }, + ); + expect(error).to.be.undefined; + }); + + it(`should not allow the step additional property if only it is provided with the type`, () => { + const { error } = api.schemas.buckets.number.validate( + { type: `number`, step: 1 }, + ); + expect(error).not.to.be.undefined; + }); + + it(`should not allow max to be less than min`, () => { + const { error } = api.schemas.buckets.number.validate( + { type: `number`, min: 10, max: 5 }, + ); + expect(error).not.to.be.undefined; + }); + + it(`should not allow max to be less than min`, () => { + const { error } = api.schemas.buckets.number.validate( + { type: `number`, min: 10, max: 15 }, + ); + expect(error).to.be.undefined; + }); + + it(`should allow step when min is also provided`, () => { + const { error } = api.schemas.buckets.number.validate( + { type: `number`, min: 10, step: 5 }, + ); + expect(error).to.be.undefined; + }); + }); + }, + ); +}; diff --git a/module/__tests__/schemas/stringBucket.mjs b/module/__tests__/schemas/stringBucket.mjs new file mode 100644 index 0000000..d992d63 --- /dev/null +++ b/module/__tests__/schemas/stringBucket.mjs @@ -0,0 +1,40 @@ +import { api } from "../../api.mjs"; + +export function stringBucketTests(quench) { + quench.registerBatch( + `${__ID__}.stringBucketSchema`, + (ctx) => { + const { describe, it, expect } = ctx; + + describe(`the string bucket schema`, () => { + it(`should allow all additional properties to be left out`, () => { + const { error } = api.schemas.buckets.string.validate( + { type: `string` }, + ); + expect(error).to.be.undefined; + }); + + it(`should allow specific choices to be provided`, () => { + const { error } = api.schemas.buckets.string.validate( + { type: `string`, choices: [`choice 1`, `choice 2`] }, + ); + expect(error).to.be.undefined; + }); + + it(`shouldn't allow specific choices to be empty`, () => { + const { error } = api.schemas.buckets.string.validate( + { type: `string`, choices: [] }, + ); + expect(error).not.to.be.undefined; + }); + + it(`should only allow specific choices to be strings`, () => { + const { error } = api.schemas.buckets.string.validate( + { type: `string`, choices: [`choice 1`, 5] }, + ); + expect(error).not.to.be.undefined; + }); + }); + }, + ); +}; diff --git a/module/main.mjs b/module/main.mjs index b3a76f4..11bdbcb 100644 --- a/module/main.mjs +++ b/module/main.mjs @@ -6,3 +6,9 @@ import "./hooks/ready.mjs"; // Document Hooks import "./hooks/preCreateChatMessage.mjs"; + +// Dev Only imports +import "./__tests__/registration.mjs"; +// if (import.meta.env.DEV) { +// import(`./__tests__/registration.mjs`); +// }