Skip to content

Commit

Permalink
fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dpwoert committed Dec 26, 2023
1 parent 254f6e1 commit 4841193
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
27 changes: 13 additions & 14 deletions core/client/src/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type Child = Layer | Control<any>;

export default class Layer {
name: string;
children: Set<Child>;
children: Child[];
parent?: Layer;
magicInstance?: MagicCircle;
folder: boolean;
Expand All @@ -15,7 +15,7 @@ export default class Layer {

constructor(name: string, magicInstance?: MagicCircle) {
this.name = name;
this.children = new Set<Child>();
this.children = [];
this.folder = false;
this.collapsed = false;
this.isBaseLayer = !!magicInstance;
Expand All @@ -36,7 +36,7 @@ export default class Layer {
traverse(fn: (child: Child, path: string) => void) {
const path = new Paths();

const recursive = (children: Set<Child>, basePath: string) => {
const recursive = (children: Child[], basePath: string) => {
children.forEach((child) => {
const currentPath = child.getPath(basePath, path);
fn(child, currentPath);
Expand Down Expand Up @@ -83,12 +83,13 @@ export default class Layer {
child.forEach((c) => {
this.add(c);
});
} else {
this.children.add(child);
}
} else if (this.children.indexOf(child) === -1) {
// Make sure we're not duplicating
this.children.push(child);

// ensure we're syncing magic instance after
this.getMagicInstance()?.sync();
// ensure we're syncing magic instance after
this.getMagicInstance()?.sync();
}

return this;
}
Expand All @@ -105,11 +106,9 @@ export default class Layer {
}

remove(layer: Child | Child[]) {
if (Array.isArray(layer)) {
layer.forEach((l) => this.children.delete(l));
} else {
this.children.delete(layer);
}
this.children = this.children.filter((c) =>
Array.isArray(layer) ? !layer.includes(c) : c !== layer
);

return this;
}
Expand All @@ -133,7 +132,7 @@ export default class Layer {
});
}

this.children.clear();
this.children = [];
this.magicInstance = undefined;
}

Expand Down
15 changes: 6 additions & 9 deletions core/client/test/layer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,58 @@ import Layer from '../src/layer';
describe('core/client:layer', () => {
test('Layer should be able to get created', () => {
const layer = new Layer('layer1');

expect(layer.name).toBe('layer1');
});

test('Layer should be able to get added to a parent via addTo()', () => {
const parent = new Layer('parent');
const child = new Layer('child').addTo(parent);

expect(parent.children[0]).toBe(child);
expect(child.parent).toBe(parent);
});

test('Layer should be able to get added to a parent via add()', () => {
const parent = new Layer('parent');
const child = new Layer('child');
parent.add(child);

expect(parent.children[0]).toBe(child);
expect(child.parent).toBe(parent);
});

test('Multiple layers should be able to get added via add()', () => {
const parent = new Layer('parent');
const child1 = new Layer('child');
const child2 = new Layer('child');
parent.add([child1, child2]);

expect(parent.children.length).toBe(2);
expect(child1.parent).toBe(parent);
expect(child2.parent).toBe(parent);
});

test('Should not add duplicate layers via add()', () => {
const parent = new Layer('parent');
const child = new Layer('child');
parent.add([child, child, child]);

expect(parent.children.length).toBe(1);
});

test('Should be able to add and remove layer', () => {
const parent = new Layer('parent');
const child = new Layer('child');

parent.add(child);
expect(parent.children.length).toBe(1);

expect(child.parent).toBe(parent);
parent.remove(child);
expect(parent.children.length).toBe(0);
expect(child.parent).toBeNull();
});

test('Should be able to add and remove multiple layers', () => {
const parent = new Layer('parent');
const child1 = new Layer('child 1');
const child2 = new Layer('child 2');

parent.add([child1, child2]);
expect(parent.children.length).toBe(2);

parent.remove([child1, child2]);
expect(parent.children.length).toBe(0);
});
Expand Down

0 comments on commit 4841193

Please sign in to comment.