Skip to content

Commit

Permalink
[FIX] update test based on new filtering logic (new id's for links)
Browse files Browse the repository at this point in the history
  • Loading branch information
brauliorivas committed Jun 2, 2024
1 parent e67e6eb commit 489bf69
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 48 deletions.
12 changes: 6 additions & 6 deletions js/menu/filter/reconnect.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { Link } from "../../objects.js";

export function reconnect(criteriaFunction, particlesHandler) {
const { parentLinks, childrenLinks, infoBoxes: particles } = particlesHandler;
const { parentLinks, childrenLinks, infoBoxes } = particlesHandler;

const newParentLinks = [];
const newChildrenLinks = [];
const filteredParticles = [];

for (const particle of particles) {
for (const particle of infoBoxes) {
if (!criteriaFunction(particle)) {
filteredParticles.push(null);

const parentParticles = [];
const childrenParticles = [];

for (const parent of particle.parents) {
if (criteriaFunction(particles[parent])) {
if (criteriaFunction(infoBoxes[parent])) {
parentParticles.push(parent);
}
}

for (const child of particle.children) {
if (criteriaFunction(particles[child])) {
if (criteriaFunction(infoBoxes[child])) {
childrenParticles.push(child);
}
}
Expand All @@ -43,7 +43,7 @@ export function reconnect(criteriaFunction, particlesHandler) {

for (const parentLinkId of particle.parentLinks) {
const parentLink = parentLinks[parentLinkId];
const parent = particles[parentLink.from];
const parent = infoBoxes[parentLink.from];
if (criteriaFunction(parent)) {
const parentLinkCopy = new Link(
newParentLinks.length,
Expand All @@ -57,7 +57,7 @@ export function reconnect(criteriaFunction, particlesHandler) {

for (const childrenLinkId of particle.childrenLinks) {
const childrenLink = childrenLinks[childrenLinkId];
const child = particles[childrenLink.to];
const child = infoBoxes[childrenLink.to];
if (criteriaFunction(child)) {
const childrenLinkCopy = new Link(
newChildrenLinks.length,
Expand Down
78 changes: 36 additions & 42 deletions test/filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

const parentLinks = [];
const childrenLinks = [];
const particles = [];
const infoBoxes = [];

beforeAll(() => {
for (let i = 0; i < 5; i++) {
Expand All @@ -17,7 +17,7 @@ beforeAll(() => {
particle.charge = i;
particle.mass = i * 10;
particle.simStatus = i + 23;
particles.push(particle);
infoBoxes.push(particle);
}

parentLinks.push(new Link(0, 0, 1));
Expand All @@ -32,28 +32,34 @@ beforeAll(() => {
childrenLinks.push(new Link(3, 2, 4));
childrenLinks.push(new Link(4, 3, 4));

particles[0].children = [1, 2];
particles[0].childrenLinks = [0, 1];
infoBoxes[0].children = [1, 2];
infoBoxes[0].childrenLinks = [0, 1];

particles[1].parents = [0];
particles[1].children = [3];
particles[1].parentLinks = [0];
particles[1].childrenLinks = [2];
infoBoxes[1].parents = [0];
infoBoxes[1].children = [3];
infoBoxes[1].parentLinks = [0];
infoBoxes[1].childrenLinks = [2];

particles[2].parents = [0];
particles[2].children = [4];
particles[2].parentLinks = [1];
particles[2].childrenLinks = [3];
infoBoxes[2].parents = [0];
infoBoxes[2].children = [4];
infoBoxes[2].parentLinks = [1];
infoBoxes[2].childrenLinks = [3];

particles[3].parents = [1];
particles[3].children = [4];
particles[3].parentLinks = [3];
particles[3].childrenLinks = [4];
infoBoxes[3].parents = [1];
infoBoxes[3].children = [4];
infoBoxes[3].parentLinks = [3];
infoBoxes[3].childrenLinks = [4];

particles[4].parents = [2, 3];
particles[4].parentLinks = [2, 4];
infoBoxes[4].parents = [2, 3];
infoBoxes[4].parentLinks = [2, 4];
});

const particlesHandler = {
parentLinks,
childrenLinks,
infoBoxes,
};

describe("filter by ranges", () => {
it("filter by a single range parameter", () => {
const momentum = new Range("momentum");
Expand All @@ -64,13 +70,11 @@ describe("filter by ranges", () => {

const [newParentLinks, newChildrenLinks, filteredParticles] = reconnect(
criteriaFunction,
parentLinks,
childrenLinks,
particles
particlesHandler
);

expect(newChildrenLinks.map((link) => link.id)).toEqual([4]);
expect(newParentLinks.map((link) => link.id)).toEqual([4]);
expect(newChildrenLinks.map((link) => link.id)).toEqual([0]);
expect(newParentLinks.map((link) => link.id)).toEqual([0]);
expect(
filteredParticles.filter((p) => p).map((particle) => particle.id)
).toEqual([3, 4]);
Expand All @@ -87,13 +91,11 @@ describe("filter by ranges", () => {

const [newParentLinks, newChildrenLinks, filteredParticles] = reconnect(
criteriaFunction,
parentLinks,
childrenLinks,
particles
particlesHandler
);

expect(newChildrenLinks.map((link) => link.id)).toEqual([4]);
expect(newParentLinks.map((link) => link.id)).toEqual([4]);
expect(newChildrenLinks.map((link) => link.id)).toEqual([0]);
expect(newParentLinks.map((link) => link.id)).toEqual([0]);
expect(
filteredParticles.filter((p) => p).map((particle) => particle.id)
).toEqual([3, 4]);
Expand All @@ -109,9 +111,7 @@ describe("filter by checkboxes", () => {

const [newParentLinks, newChildrenLinks, filteredParticles] = reconnect(
criteriaFunction,
parentLinks,
childrenLinks,
particles
particlesHandler
);

expect(newChildrenLinks.map((link) => link.id)).toEqual([]);
Expand All @@ -137,13 +137,11 @@ describe("filter by checkboxes", () => {

const [newParentLinks, newChildrenLinks, filteredParticles] = reconnect(
criteriaFunction,
parentLinks,
childrenLinks,
particles
particlesHandler
);

expect(newChildrenLinks.map((link) => link.id)).toEqual([0, 1, 4]);
expect(newParentLinks.map((link) => link.id)).toEqual([0, 1, 4]);
expect(newChildrenLinks.map((link) => link.id)).toEqual([0, 1, 2]);
expect(newParentLinks.map((link) => link.id)).toEqual([0, 1, 2]);
expect(
filteredParticles.filter((p) => p).map((particle) => particle.id)
).toEqual([0, 3, 4]);
Expand All @@ -163,9 +161,7 @@ describe("filter by ranges and checkboxes", () => {

const [newParentLinks, newChildrenLinks, filteredParticles] = reconnect(
criteriaFunction,
parentLinks,
childrenLinks,
particles
particlesHandler
);

expect(newParentLinks.map((link) => link.id).sort()).toEqual([
Expand Down Expand Up @@ -193,9 +189,7 @@ describe("filter by ranges and checkboxes", () => {

const [newParentLinks, newChildrenLinks, filteredParticles] = reconnect(
criteriaFunction,
parentLinks,
childrenLinks,
particles
particlesHandler
);

expect(newChildrenLinks.map((link) => link.id)).toEqual([]);
Expand Down

0 comments on commit 489bf69

Please sign in to comment.