Skip to content

Commit 8e1238a

Browse files
committed
mock correctly readTransaction and fix sizeCypher test
1 parent a74c596 commit 8e1238a

File tree

3 files changed

+52
-104
lines changed

3 files changed

+52
-104
lines changed

__mocks__/neo4j-driver.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ export const mockSessionRun = jest.fn(() => {
88

99
export const mockSessionClose = jest.fn().mockImplementation(() => {});
1010

11+
export const mockReadTransaction = jest.fn(function (callback) {
12+
return callback(this);
13+
});
14+
1115
export const mockSession = jest.fn().mockImplementation(() => ({
1216
run: mockSessionRun,
13-
close: mockSessionClose
17+
close: mockSessionClose,
18+
readTransaction: mockReadTransaction
1419
}));
1520

1621
export const mockDriver = jest.spyOn(Neo4j, 'driver').mockImplementation(() => ({

__tests__/neovis.tests.js

Lines changed: 44 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,11 @@ describe('Neovis', () => {
203203
container_id,
204204
labels: {
205205
[label1]: {
206-
sizeCypher: sizeCypher
206+
[NEOVIS_ADVANCED_CONFIG]: {
207+
cypher: {
208+
value: sizeCypher
209+
}
210+
}
207211
}
208212
}
209213
};
@@ -213,14 +217,14 @@ describe('Neovis', () => {
213217

214218
// TODO: session.readTransaction needs to be properly mocked
215219
// skipping this test until mock is added
216-
it.skip('should call sizeCypher and save return value to data set value', async () => {
220+
it('should call sizeCypher and save return value to data set value', async () => {
217221
const node = testUtils.makeNode([label1]);
218222
testUtils.mockFullRunSubscribe({
219223
[initial_cypher]: {
220224
default: [testUtils.makeRecord([node])]
221225
},
222226
[sizeCypher]: {
223-
[node.identity.toInt()]: [testUtils.makeRecord([Neo4j.int(1)])]
227+
[node.identity]: [testUtils.makeRecord([Neo4j.int(1)])]
224228
}
225229
});
226230

@@ -278,105 +282,41 @@ describe('Neovis', () => {
278282
});
279283
});
280284

281-
describe('neovis config test', () => {
282-
const imageUrl = 'https://visjs.org/images/visjs_logo.png';
283-
const fontSize = 28;
284-
const fontColor = '#00FF00';
285-
let config = {
286-
container_id: container_id,
287-
labels: {
288-
[label1]: {
289-
[NEOVIS_ADVANCED_CONFIG]: {
290-
'static': {
291-
image: imageUrl,
292-
font: {
293-
'size': fontSize,
294-
'color': fontColor,
295-
}
296-
}
297-
}
298-
}
299-
},
300-
initial_cypher: initial_cypher
301-
};
302-
beforeEach(() => {
303-
neovis = new Neovis(config);
304-
});
305-
306-
it('image field in config should reflect in node data', async () => {
307-
const node1 = testUtils.makeNode([label1]);
308-
testUtils.mockFullRunSubscribe({
309-
[initial_cypher]: {
310-
default: [testUtils.makeRecord([node1])]
311-
}
312-
});
313-
314-
neovis.render();
315-
await testUtils.neovisRenderDonePromise(neovis);
316-
expect(neovis._data.nodes.get(1)).toHaveProperty('image', imageUrl);
317-
});
318-
319-
it('image field for type not specified in config should not reflect in node data', async () => {
320-
const node1 = testUtils.makeNode([label2]);
321-
testUtils.mockFullRunSubscribe({
322-
[initial_cypher]: {
323-
default: [testUtils.makeRecord([node1])]
324-
}
325-
});
326-
327-
neovis.render();
328-
await testUtils.neovisRenderDonePromise(neovis);
329-
expect(neovis._data.nodes.get(1)).toHaveProperty('image', undefined);
330-
});
331-
332-
it('font field in config should reflect in node data', async () => {
333-
const node1 = testUtils.makeNode([label1]);
334-
testUtils.mockFullRunSubscribe({
335-
[initial_cypher]: {
336-
default: [testUtils.makeRecord([node1])]
337-
}
338-
});
339-
340-
neovis.render();
341-
await testUtils.neovisRenderDonePromise(neovis);
342-
expect(neovis._data.nodes.get(1).font).toBeDefined();
343-
expect(neovis._data.nodes.get(1).font.size).toBe(fontSize);
344-
expect(neovis._data.nodes.get(1).font.color).toBe(fontColor);
345-
});
346-
347-
it('font field for type not specified in config should not reflect in node data', async () => {
348-
const node1 = testUtils.makeNode([label2]);
349-
testUtils.mockFullRunSubscribe({
350-
[initial_cypher]: {
351-
default: [testUtils.makeRecord([node1])]
352-
}
353-
});
354-
355-
neovis.render();
356-
await testUtils.neovisRenderDonePromise(neovis);
357-
expect(neovis._data.nodes.get(1)).toHaveProperty('font', undefined);
358-
});
359-
});
360-
describe('neovis non flat config test', () => {
361-
const imageUrl = 'https://visjs.org/images/visjs_logo.png';
362-
const fontSize = 28;
363-
const fontColor = '#00FF00';
364-
let config = {
365-
container_id: container_id,
366-
non_flat: true,
367-
labels: {
368-
[label1]: {
369-
'static': {
285+
const imageUrl = 'https://visjs.org/images/visjs_logo.png';
286+
const fontSize = 28;
287+
const fontColor = '#00FF00';
288+
describe.each([['config', {
289+
container_id,
290+
labels: {
291+
[label1]: {
292+
[NEOVIS_ADVANCED_CONFIG]: {
293+
static: {
370294
image: imageUrl,
371295
font: {
372-
'size': fontSize,
373-
'color': fontColor,
296+
size: fontSize,
297+
color: fontColor,
374298
}
375299
}
376300
}
377-
},
378-
initial_cypher: initial_cypher
379-
};
301+
}
302+
},
303+
initial_cypher: initial_cypher
304+
}], ['non flat config', {
305+
container_id,
306+
non_flat: true,
307+
labels: {
308+
[label1]: {
309+
static: {
310+
image: imageUrl,
311+
font: {
312+
size: fontSize,
313+
color: fontColor,
314+
}
315+
}
316+
}
317+
},
318+
initial_cypher: initial_cypher
319+
}]])('neovis advance %s test', (configName, config) => {
380320
beforeEach(() => {
381321
neovis = new Neovis(config);
382322
});
@@ -572,15 +512,15 @@ describe('Neovis', () => {
572512

573513
// TODO: session.readTransaction needs to be properly mocked
574514
// skipping this test until mock is added
575-
it.skip('should merge cypher type to vis.js config properly', async () => {
515+
it('should merge cypher type to vis.js config properly', async () => {
576516
const sizeCypher = 'sizeCypher';
577517
let config = {
578518
container_id: container_id,
579519
labels: {
580520
[label1]: {
581521
[NEOVIS_ADVANCED_CONFIG]: {
582-
'cypher': {
583-
'label': sizeCypher
522+
cypher: {
523+
label: sizeCypher
584524
}
585525
}
586526
}
@@ -592,11 +532,14 @@ describe('Neovis', () => {
592532
testUtils.mockFullRunSubscribe({
593533
[initial_cypher]: {
594534
default: [testUtils.makeRecord([node1])]
535+
},
536+
[sizeCypher]: {
537+
[node1.identity]: [testUtils.makeRecord([intProperityValue])]
595538
}
596539
});
597540
neovis.render();
598541
await testUtils.neovisRenderDonePromise(neovis);
599-
expect(Neo4jMock.mockSessionRun).toHaveBeenCalledTimes(1);
542+
expect(Neo4jMock.mockSessionRun).toHaveBeenCalledTimes(2);
600543
expect(neovis._data.nodes.get(1)).toHaveProperty('label', intProperityValue);
601544
});
602545

src/neovis.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class NeoVis {
104104
let results = [];
105105

106106
try {
107-
const result = await session.readTransaction(tx => tx.run(cypher, { id: id }));
107+
const result = await session.readTransaction(tx => tx.run(cypher, { id }));
108108
for (let record of result.records) {
109109
record.forEach((v) => {
110110
results.push(v);
@@ -225,7 +225,7 @@ export class NeoVis {
225225
}
226226
this._buildPropertyNameObject(propertyConfig, baseObejct, neo4jObject);
227227
this._buildStaticObject(staticConfig, baseObejct);
228-
await this._buildCypherObject(cypherConfig, baseObejct, neo4jObject);
228+
await this._buildCypherObject(cypherConfig, baseObejct, baseObejct.id);
229229
await this._buildFunctionObject(functionConfig, baseObejct, neo4jObject);
230230
}
231231

0 commit comments

Comments
 (0)