forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphEdge.test.js
More file actions
65 lines (50 loc) · 2.07 KB
/
GraphEdge.test.js
File metadata and controls
65 lines (50 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import GraphEdge from '../GraphEdge';
import GraphVertex from '../GraphVertex';
describe('GraphEdge', () => {
it('should create graph edge with default weight', () => {
const startVertex = new GraphVertex('A');
const endVertex = new GraphVertex('B');
const edge = new GraphEdge(startVertex, endVertex);
expect(edge.startVertex).toEqual(startVertex);
expect(edge.endVertex).toEqual(endVertex);
expect(edge.weight).toEqual(0);
});
it('should create graph edge with predefined weight', () => {
const startVertex = new GraphVertex('A');
const endVertex = new GraphVertex('B');
const edge = new GraphEdge(startVertex, endVertex, 10);
expect(edge.startVertex).toEqual(startVertex);
expect(edge.endVertex).toEqual(endVertex);
expect(edge.weight).toEqual(10);
});
it('should be possible to do edge reverse', () => {
const vertexA = new GraphVertex('A');
const vertexB = new GraphVertex('B');
const edge = new GraphEdge(vertexA, vertexB, 10);
expect(edge.startVertex).toEqual(vertexA);
expect(edge.endVertex).toEqual(vertexB);
expect(edge.weight).toEqual(10);
edge.reverse();
expect(edge.startVertex).toEqual(vertexB);
expect(edge.endVertex).toEqual(vertexA);
expect(edge.weight).toEqual(10);
});
it('should return edges names as key', () => {
const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0);
expect(edge.getKey()).toBe('A_B');
expect(edge.toString()).toBe('A_B');
});
it('should return custom key if defined', () => {
const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0, 'custom_key');
expect(edge.getKey()).toEqual('custom_key');
expect(edge.toString()).toEqual('custom_key');
});
it('should execute toString on key when calling toString on edge', () => {
const customKey = {
toString() { return 'custom_key'; },
};
const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0, customKey);
expect(edge.getKey()).toEqual(customKey);
expect(edge.toString()).toEqual('custom_key');
});
});