Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
test: update
Browse files Browse the repository at this point in the history
  • Loading branch information
haxxmaxx committed Mar 4, 2020
1 parent 8d70660 commit 59d5fd6
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 77 deletions.
83 changes: 83 additions & 0 deletions src/__tests__/snapshot.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { hierarchy } from 'd3';
import { createSnapshotData } from '../snapshot';

describe('snapshot', () => {
describe('createSnapshotData', () => {
const nodes = {
id: '1',
elemNo: 1,
rowNo: 0,
children: [
{
id: '2',
elemNo: 2,
rowNo: 1,
children: [
{
id: '3',
elemNo: 3,
rowNo: 2,
parent: {
data: {
id: '2',
},
},
children: [
{
id: '5',
elemNo: 798,
rowNo: 3,
parent: {
id: '3',
},
},
],
},
],
},
{
id: '4',
elemNo: 88,
rowNo: 4,
children: [],
},
],
};

let topId;
let isExpanded;
let expandedChildren;
let nodeTree;
let layout;

beforeEach(() => {
topId = '1';
isExpanded = true;
expandedChildren = [];
nodeTree = hierarchy(nodes);
layout = {
snapshotData: {},
qHyperCube: {
qDataPages: [{ qMatrix: [0, 1, 2, 3, 4] }],
},
};
});

it('should return matrix from snapshotData', () => {
layout.snapshotData.dataMatrix = 'someMatrix';
const result = createSnapshotData({ topId, isExpanded, expandedChildren }, nodeTree, layout);
expect(result).to.equal('someMatrix');
});

it('should return usedMatrix', () => {
const result = createSnapshotData({ topId, isExpanded, expandedChildren }, nodeTree, layout);
expect(result).to.eql([0, 1, 2, 4]);
});

it('should return usedMatrix except node with missing rowNo', () => {
nodeTree.children[1].data.rowNo = undefined;
const result = createSnapshotData({ topId, isExpanded, expandedChildren }, nodeTree, layout);
expect(result).to.eql([0, 1, 2]);
});
});
});
8 changes: 4 additions & 4 deletions src/card/__tests__/card.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ describe('card', () => {
it('should return html for selected node in active state', () => {
data.measure = 'measure';
data.selected = true;
selections = { api: { isActive: () => true } };
const result = card(data, cardStyling, selections, [data.elemNo]);
selections = { api: { isActive: () => true }, state: [data.elemNo] };
const result = card(data, cardStyling, selections);
expect(result).to.equal(
getHtml(
`<div class="sn-org-card-title">${data.id}</div><div class="sn-org-card-label">${data.measure}</div>`,
Expand All @@ -162,8 +162,8 @@ describe('card', () => {
it('should return html for not selected node in active state', () => {
data.measure = 'measure';
data.selected = false;
selections = { api: { isActive: () => true } };
const result = card(data, cardStyling, selections, [7]);
selections = { api: { isActive: () => true }, state: [7] };
const result = card(data, cardStyling, selections);
expect(result).to.equal(
getHtml(
`<div class="sn-org-card-title">${data.id}</div><div class="sn-org-card-label">${data.measure}</div>`,
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default function supernova(env) {
}
}, [element, dataTree, constraints]);

// Updates snapshot??
// Updates snapshot when resizing
useEffect(() => {
if (preRenderData && layout && layout.snapshotData) {
const snapshotZoom = getSnapshotZoom(rect, layout.snapshotData.viewState);
Expand Down
25 changes: 12 additions & 13 deletions src/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,26 @@ export const createSnapshotData = (expandedState, allNodes, layout) => {
return usedMatrix;
};

export default function snapshot(expandedState, preRenderData, layout, transform, initialZoom) {
export const createViewState = (expandedState, transform, initialZoom) => {
const element = useElement();

const createViewState = () => {
const size = { w: element.clientWidth, h: element.clientHeight };
const vs = {
expandedState,
transform,
size,
initialZoom,
};
vs.expandedState.useTransitions = false;
return vs;
const size = { w: element.clientWidth, h: element.clientHeight };
const vs = {
expandedState,
transform,
size,
initialZoom,
};
vs.expandedState.useTransitions = false;
return vs;
};

export default function snapshot(expandedState, preRenderData, layout, transform, initialZoom) {
onTakeSnapshot(snapshotLayout => {
if (!snapshotLayout.snapshotData) {
snapshotLayout.snapshotData = {};
}
if (!layout.snapshotData || !layout.snapshotData.viewState) {
snapshotLayout.snapshotData.viewState = createViewState();
snapshotLayout.snapshotData.viewState = createViewState(expandedState, transform, initialZoom);
}
snapshotLayout.snapshotData.dataMatrix = createSnapshotData(expandedState, preRenderData.allNodes, layout);
});
Expand Down
40 changes: 1 addition & 39 deletions src/tree/__tests__/render.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hierarchy } from 'd3';
import { filterTree, createSnapshotData } from '../render';
import { filterTree } from '../render';

const nodes = {
id: '1',
Expand Down Expand Up @@ -102,42 +102,4 @@ describe('render', () => {
expect(result.map(node => node.data.id)).to.deep.equal(['1', '2', '4', '3', '5']);
});
});

describe('createSnapshotData', () => {
let topId;
let isExpanded;
let expandedChildren;
let nodeTree;
let layout;

beforeEach(() => {
topId = '1';
isExpanded = true;
expandedChildren = [];
nodeTree = hierarchy(nodes);
layout = {
snapshotData: {},
qHyperCube: {
qDataPages: [{ qMatrix: [0, 1, 2, 3, 4] }],
},
};
});

it('should return matrix from snapshotData', () => {
layout.snapshotData.dataMatrix = 'someMatrix';
const result = createSnapshotData({ topId, isExpanded, expandedChildren }, nodeTree, layout);
expect(result).to.equal('someMatrix');
});

it('should return usedMatrix', () => {
const result = createSnapshotData({ topId, isExpanded, expandedChildren }, nodeTree, layout);
expect(result).to.eql([0, 1, 2, 4]);
});

it('should return usedMatrix except node with missing rowNo', () => {
nodeTree.children[1].data.rowNo = undefined;
const result = createSnapshotData({ topId, isExpanded, expandedChildren }, nodeTree, layout);
expect(result).to.eql([0, 1, 2]);
});
});
});
12 changes: 5 additions & 7 deletions src/tree/__tests__/tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ describe('tooltip', () => {
const constainerHeight = 1000;
const x = () => d.xActual;
const y = () => d.yActual;
const sel = {
transform: {
zoom: 1,
y: 1,
x: 1,
},
const transform = {
zoom: 1,
y: 1,
x: 1,
};

it('Should return string containing style', () => {
const style = getTooltipStyle(d, constainerHeight, x, y, sel);
const style = getTooltipStyle(d, constainerHeight, x, y, transform);
expect(style).to.equal('bottom:1014px;left:77px;');
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/tree/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default function box(
})
.html(d => card(d.data, cardStyling, selectionObj))
.on('mouseenter', d => {
if (!touchmode && !storageState.constraints.active && event.buttons === 0) {
if (!touchmode && !storageState.constraints.passive && event.buttons === 0) {
openTooltip(tooltip, d, element.clientHeight, cardStyling, x, y, storageState.transform);
}
})
Expand Down
23 changes: 11 additions & 12 deletions src/utils/__tests__/selections.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe('selections', () => {
const { select } = selections;
let node;
let selectionObj;
let selectionState;
let isActive;
beforeEach(() => {
node = JSON.parse(JSON.stringify(defaultValues.nodes));
Expand All @@ -20,51 +19,51 @@ describe('selections', () => {
},
linked: false,
setState: sinon.spy(),
state: [],
};
selectionState = [];
});
it('should early return if elemNo is negative', () => {
node.data.elemNo = -2;
select(node, selectionObj, selectionState);
select(node, selectionObj);
expect(selectionObj.setState).to.not.have.been.called;
});
it('should early return if elemNo isLocked is true', () => {
node.data.isLocked = true;
select(node, selectionObj, selectionState);
select(node, selectionObj);
expect(selectionObj.setState).to.not.have.been.called;
});
it('should call begin and setState to node id', () => {
select(node, selectionObj, selectionState);
select(node, selectionObj);
expect(selectionObj.api.begin).to.have.been.calledOnce;
expect(selectionObj.api.select).to.have.been.calledOnce;
expect(selectionObj.setState).to.have.been.calledWith([1]);
});
it('should not call begin and push node id to selectionState', () => {
isActive = true;
selectionState.push(2);
select(node, selectionObj, selectionState);
selectionObj.state.push(2);
select(node, selectionObj);
expect(selectionObj.api.begin).to.not.have.been.called;
expect(selectionObj.api.select).to.have.been.calledOnce;
expect(selectionObj.setState).to.have.been.calledWith([2, 1]);
});
it('should get all children when linked is true', () => {
selectionObj.linked = true;
select(node, selectionObj, selectionState);
select(node, selectionObj);
expect(selectionObj.api.select).to.have.been.calledOnce;
expect(selectionObj.setState).to.have.been.calledWith([1, 2, 3, 798, 88]);
});

it('should deselect node', () => {
isActive = true;
selectionState = [1, 2];
select(node, selectionObj, selectionState);
selectionObj.state = [1, 2];
select(node, selectionObj);
expect(selectionObj.setState).to.have.been.calledWith([2]);
});
it('should deselect node and children', () => {
isActive = true;
selectionObj.linked = true;
selectionState = [1, 2];
select(node, selectionObj, selectionState);
selectionObj.state = [1, 2];
select(node, selectionObj);
expect(selectionObj.api.clear).to.have.been.calledOnce;
expect(selectionObj.setState).to.have.been.calledWith([]);
});
Expand Down

0 comments on commit 59d5fd6

Please sign in to comment.