Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore(test): Actor test suite #356

Merged
merged 11 commits into from
Mar 28, 2023
350 changes: 201 additions & 149 deletions src/e2e/index.ts

Large diffs are not rendered by default.

82 changes: 53 additions & 29 deletions src/e2e/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@

const inputDelay = 120;

const delay = (ms) =>
export const delay = (ms: number) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});

/**
* If there are messages, purge them.
*
* @returns {Promise} The promise from deleting messages
*/
export const trashChat = () =>
game.messages.size > 0
? game.messages.documentClass.deleteDocuments([], { deleteAll: true })
: null;
export const trashChat = (): any => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if this passes linting for the current ESLint config. That said, we may have to revisit discussion of the FVTT Types project anyway...

if (game.messages?.size)
game.messages?.documentClass.deleteDocuments([], { deleteAll: true });
};

/**
* Delays execution so the UI can catch up.
Expand All @@ -32,14 +30,19 @@ export const openWindows = (className: string) =>
);

export const openDialogs = () =>
Object.values(ui.windows).filter((o) => o.options.classes.includes("dialog"))
Object.values(ui.windows).filter((o) => o.options.classes.includes("dialog"));

export const closeDialogs = async () => {
openDialogs()?.forEach(async (o) => {
await o.close();
});
};

export const closeSheets = async () => {
openWindows("sheet").forEach(async (w) => w.close());
waitForInput();
};

/**
* MOCKING HELPERS
*/
Expand All @@ -55,50 +58,71 @@ export const createMockActorKey = async (
type,
});

export const createWorldTestItem = async (type: string) =>
export const createWorldTestItem = async (
type: string,
name: string = `New World Test ${type.capitalize()}`
) =>
Item.create({
type,
name: `New World Test ${type.capitalize()}`,
name,
});

export const createActorTestItem = async (
actor: StoredDocument<Actor>,
type: string
) =>
actor.createEmbeddedDocuments(
"Item",
[{ type, name: `New Actor Test ${type.capitalize()}` }],
{}
);
actor: StoredDocument<Actor> | undefined,
type: string,
name: string = `New Actor Test ${type.capitalize()}`,
data: object = {}
) => actor?.createEmbeddedDocuments("Item", [{ type, name, ...data }]);

export const createMockMacro = async () =>
Macro.create({
name: `Mock Macro ${foundry.utils.randomID()}`,
type: "script",
command: "console.log('Testing Macro');",
});

export const createMockScene = async () =>
Scene.create({ name: "Mock Scene", tokenVision: true });

export const getMockActorKey = async (key: string) =>
game.actors?.getName(`Test Actor ${key}`);

/**
* CLEANUP HELPERS
*/

export const cleanUpMacros = () => {
const mockMacros = game.macros?.filter((o) =>
o.name.includes("New Actor Test")
);
mockMacros?.forEach((o) => o.delete());
export const cleanUpMacros = async () => {
const mockMacros = game.macros?.filter((o) => o.name?.includes("Mock Macro"));
mockMacros?.forEach(async (o) => await o.delete());
return true;
};

export const cleanUpActorsKey = (key) => {
export const cleanUpActorsByKey = async (key: string) => {
game.actors
?.filter((a) => a.name === `Test Actor ${key}`)
.forEach((a) => a.delete());
.forEach(async (a) => await a.delete());
};

export const cleanUpWorldItems = () => {
export const cleanUpWorldItems = async () => {
game.items
?.filter((a) => a?.name?.includes("New World Test"))
.forEach((a) => a.delete());
.forEach(async (a) => await a.delete());
};

export const cleanUpScenes = () => {
export const cleanUpScenes = async () => {
game.scenes
?.filter((s) => s.name === "Mock Scene")
.forEach((s) => s.delete());
.forEach(async (s) => await s.delete());
};

/**
* CONSTS
*/
export const itemTypes = new Set([
"spell",
"ability",
"armor",
"weapon",
"item",
"container",
]);
15 changes: 14 additions & 1 deletion src/module/__tests__/helpers-behaviour.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import skipRollDialogCheck from "../helpers-behaviour";

export const key = "ose.helpers.behaviour";
export const options = {
displayName: "Helpers: Behaviour",
displayName: "OSE: Helpers: Behaviour",
};

export default ({ describe, it, before, after, assert }: QuenchMethods) => {
Expand All @@ -16,62 +16,75 @@ export default ({ describe, it, before, after, assert }: QuenchMethods) => {
game.system.id,
"invertedCtrlBehavior"
);

describe("invertedCtrlBehavior is set to false", () => {
before(async () => {
await game.settings.set(game.system.id, "invertedCtrlBehavior", false);
});

it("Setting is false", async () => {
const setting = await game.settings.get(
game.system.id,
"invertedCtrlBehavior"
);
assert(!setting);
});

it("Not holding ctrl should not skip dialog", () => {
const event = new KeyboardEvent("keydown", { ctrlKey: false });
assert(!skipRollDialogCheck(event));
});

it("Not holding meta should not skip dialog", () => {
const event = new KeyboardEvent("keydown", { metaKey: false });
assert(!skipRollDialogCheck(event));
});

it("Holding ctrl should not skip dialog", () => {
const event = new KeyboardEvent("keydown", { ctrlKey: true });
assert(skipRollDialogCheck(event));
});

it("Holding meta should not skip dialog", () => {
const event = new KeyboardEvent("keydown", { metaKey: true });
assert(skipRollDialogCheck(event));
});
});

describe("invertedCtrlBehavior is set to true", () => {
before(async () => {
await game.settings.set(game.system.id, "invertedCtrlBehavior", true);
});

it("Setting is false", async () => {
const setting = await game.settings.get(
game.system.id,
"invertedCtrlBehavior"
);
assert(setting);
});

it("Not holding ctrl should skip dialog", () => {
const event = new KeyboardEvent("keydown", { ctrlKey: false });
assert(skipRollDialogCheck(event));
});

it("Not holding meta should skip dialog", () => {
const event = new KeyboardEvent("keydown", { ctrlKey: false });
assert(skipRollDialogCheck(event));
});

it("Holding ctrl should not skip dialog", () => {
const event = new KeyboardEvent("keydown", { ctrlKey: true });
assert(!skipRollDialogCheck(event));
});

it("Holding meta should not skip dialog", () => {
const event = new KeyboardEvent("keydown", { metaKey: true });
assert(!skipRollDialogCheck(event));
});
});

after(async () => {
await game.settings.set(
game.system.id,
Expand Down
2 changes: 1 addition & 1 deletion src/module/__tests__/helpers-chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { applyChatCardDamage } = functionsForTesting;

export const key = "ose.helpers.chat";
export const options = {
displayName: "Helpers: Chat",
displayName: "OSE: Helpers: Chat",
};

export default ({ describe, it, before, after, expect }: QuenchMethods) => {
Expand Down
Loading