Skip to content

Commit

Permalink
Extraer a una función el setup para crear el World en los tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierGelatti committed Nov 28, 2024
1 parent 10f074c commit 0f7f8f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
41 changes: 25 additions & 16 deletions tests/outliners_in_dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@ import {asClientLocation, positionOfDomElement} from "../src/dom.ts";
import {OutlinerFromDomElement} from "./outlinerFromDomElement.ts";

describe("The outliners in the world", () => {
setupPointerCaptureSimulation();

let world: World;
let worldDomElement: HTMLElement;

beforeEach(() => {
document.body.innerHTML = "";

world = new World();
worldDomElement = world.domElement();
});
const { world, worldDomElement } = createWorldInDomBeforeEach();

test("are opened once per object", () => {
const anObject = {};
Expand Down Expand Up @@ -185,7 +175,7 @@ describe("The outliners in the world", () => {

describe("movements", () => {
beforeEach(() => {
document.body.append(worldDomElement);
document.body.append(worldDomElement());
});

test("the starting position of the outliner can be specified", () => {
Expand Down Expand Up @@ -255,7 +245,7 @@ describe("The outliners in the world", () => {
test("prevents the default action (of making a selection) from happening when dragging starts", () => {
const outlinerElement = openOutlinerFor({}, point(10, 20));
let event: PointerEvent;
worldDomElement.addEventListener("pointerdown", e => event = e)
worldDomElement().addEventListener("pointerdown", e => event = e)

fireMousePointerEventOver(outlinerElement.header(), "pointerDown", point(5, 3));

Expand Down Expand Up @@ -450,7 +440,7 @@ describe("The outliners in the world", () => {
});

function openOutliners() {
return Array.from(worldDomElement.querySelectorAll<HTMLElement>(".outliner"))
return Array.from(worldDomElement().querySelectorAll<HTMLElement>(".outliner"))
.map(domElement => new OutlinerFromDomElement(domElement));
}

Expand All @@ -459,12 +449,12 @@ describe("The outliners in the world", () => {
}

function openOutlinerFor(anObject: unknown, position?: Position) {
world.openOutliner(anObject, position);
world().openOutliner(anObject, position);
return lastOutliner();
}

function updateOutliners() {
world.updateOutliners();
world().updateOutliners();
}

function lastOutliner() {
Expand All @@ -474,4 +464,23 @@ describe("The outliners in the world", () => {
function numberOfOpenOutliners() {
return openOutliners().length;
}

function createWorldInDomBeforeEach() {
setupPointerCaptureSimulation();

let currentWorld: World;
let currentWorldDomElement: HTMLElement;

beforeEach(() => {
document.body.innerHTML = "";

currentWorld = new World();
currentWorldDomElement = currentWorld.domElement();
});

return {
world: () => currentWorld,
worldDomElement: () => currentWorldDomElement,
};
}
});
1 change: 1 addition & 0 deletions tests/pointer_capture_simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class PointerCaptureSimulator {

export function setupPointerCaptureSimulation() {
const pointerCaptureSimulator = new PointerCaptureSimulator();

beforeEach(() => {
Element.prototype.setPointerCapture = vi.fn(function (this: Element, pointerId: number) {
pointerCaptureSimulator.capturePointerOn(this, pointerId);
Expand Down

0 comments on commit 0f7f8f2

Please sign in to comment.