summaryrefslogtreecommitdiff
path: root/spec/frontend/pipelines/components/dag/dag_graph_spec.js
blob: 4619548d1bb0831317d66807eab9d29102f1fa3e (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { shallowMount } from '@vue/test-utils';
import { IS_HIGHLIGHTED, LINK_SELECTOR, NODE_SELECTOR } from '~/pipelines/components/dag/constants';
import DagGraph from '~/pipelines/components/dag/dag_graph.vue';
import { createSankey } from '~/pipelines/components/dag/drawing_utils';
import { highlightIn, highlightOut } from '~/pipelines/components/dag/interactions';
import { removeOrphanNodes } from '~/pipelines/components/parsing_utils';
import { parsedData } from './mock_data';

describe('The DAG graph', () => {
  let wrapper;

  const getGraph = () => wrapper.find('.dag-graph-container > svg');
  const getAllLinks = () => wrapper.findAll(`.${LINK_SELECTOR}`);
  const getAllNodes = () => wrapper.findAll(`.${NODE_SELECTOR}`);
  const getAllLabels = () => wrapper.findAll('foreignObject');

  const createComponent = (propsData = {}) => {
    if (wrapper?.destroy) {
      wrapper.destroy();
    }

    wrapper = shallowMount(DagGraph, {
      attachTo: document.body,
      propsData,
      data() {
        return {
          color: () => {},
          width: 0,
          height: 0,
        };
      },
    });
  };

  beforeEach(() => {
    createComponent({ graphData: parsedData });
  });

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  describe('in the basic case', () => {
    beforeEach(() => {
      /*
        The graph uses random to offset links. To keep the snapshot consistent,
        we mock Math.random. Wheeeee!
      */
      const randomNumber = jest.spyOn(global.Math, 'random');
      randomNumber.mockImplementation(() => 0.2);
      createComponent({ graphData: parsedData });
    });

    it('renders the graph svg', () => {
      expect(getGraph().exists()).toBe(true);
      expect(getGraph().html()).toMatchSnapshot();
    });
  });

  describe('links', () => {
    it('renders the expected number of links', () => {
      expect(getAllLinks()).toHaveLength(parsedData.links.length);
    });

    it('renders the expected number of gradients', () => {
      expect(wrapper.findAll('linearGradient')).toHaveLength(parsedData.links.length);
    });

    it('renders the expected number of clip paths', () => {
      expect(wrapper.findAll('clipPath')).toHaveLength(parsedData.links.length);
    });
  });

  describe('nodes and labels', () => {
    const sankeyNodes = createSankey()(parsedData).nodes;
    const processedNodes = removeOrphanNodes(sankeyNodes);

    describe('nodes', () => {
      it('renders the expected number of nodes', () => {
        expect(getAllNodes()).toHaveLength(processedNodes.length);
      });
    });

    describe('labels', () => {
      it('renders the expected number of labels as foreignObjects', () => {
        expect(getAllLabels()).toHaveLength(processedNodes.length);
      });

      it('renders the title as text', () => {
        expect(getAllLabels().at(0).text()).toBe(parsedData.nodes[0].name);
      });
    });
  });

  describe('interactions', () => {
    const strokeOpacity = (opacity) => `stroke-opacity: ${opacity};`;
    const baseOpacity = () => wrapper.vm.$options.viewOptions.baseOpacity;

    describe('links', () => {
      const liveLink = () => getAllLinks().at(4);
      const otherLink = () => getAllLinks().at(1);

      describe('on hover', () => {
        it('sets the link opacity to baseOpacity and background links to 0.2', () => {
          liveLink().trigger('mouseover');
          expect(liveLink().attributes('style')).toBe(strokeOpacity(highlightIn));
          expect(otherLink().attributes('style')).toBe(strokeOpacity(highlightOut));
        });

        it('reverts the styles on mouseout', () => {
          liveLink().trigger('mouseover');
          liveLink().trigger('mouseout');
          expect(liveLink().attributes('style')).toBe(strokeOpacity(baseOpacity()));
          expect(otherLink().attributes('style')).toBe(strokeOpacity(baseOpacity()));
        });
      });

      describe('on click', () => {
        describe('toggles link liveness', () => {
          it('turns link on', () => {
            liveLink().trigger('click');
            expect(liveLink().attributes('style')).toBe(strokeOpacity(highlightIn));
            expect(otherLink().attributes('style')).toBe(strokeOpacity(highlightOut));
          });

          it('turns link off on second click', () => {
            liveLink().trigger('click');
            liveLink().trigger('click');
            expect(liveLink().attributes('style')).toBe(strokeOpacity(baseOpacity()));
            expect(otherLink().attributes('style')).toBe(strokeOpacity(baseOpacity()));
          });
        });

        it('the link remains live even after mouseout', () => {
          liveLink().trigger('click');
          liveLink().trigger('mouseout');
          expect(liveLink().attributes('style')).toBe(strokeOpacity(highlightIn));
          expect(otherLink().attributes('style')).toBe(strokeOpacity(highlightOut));
        });

        it('preserves state when multiple links are toggled on and off', () => {
          const anotherLiveLink = () => getAllLinks().at(2);

          liveLink().trigger('click');
          anotherLiveLink().trigger('click');
          expect(liveLink().attributes('style')).toBe(strokeOpacity(highlightIn));
          expect(anotherLiveLink().attributes('style')).toBe(strokeOpacity(highlightIn));
          expect(otherLink().attributes('style')).toBe(strokeOpacity(highlightOut));

          anotherLiveLink().trigger('click');
          expect(liveLink().attributes('style')).toBe(strokeOpacity(highlightIn));
          expect(anotherLiveLink().attributes('style')).toBe(strokeOpacity(highlightOut));
          expect(otherLink().attributes('style')).toBe(strokeOpacity(highlightOut));

          liveLink().trigger('click');
          expect(liveLink().attributes('style')).toBe(strokeOpacity(baseOpacity()));
          expect(anotherLiveLink().attributes('style')).toBe(strokeOpacity(baseOpacity()));
          expect(otherLink().attributes('style')).toBe(strokeOpacity(baseOpacity()));
        });
      });
    });

    describe('nodes', () => {
      const liveNode = () => getAllNodes().at(10);
      const anotherLiveNode = () => getAllNodes().at(5);
      const nodesNotHighlighted = () => getAllNodes().filter((n) => !n.classes(IS_HIGHLIGHTED));
      const linksNotHighlighted = () => getAllLinks().filter((n) => !n.classes(IS_HIGHLIGHTED));
      const nodesHighlighted = () => getAllNodes().filter((n) => n.classes(IS_HIGHLIGHTED));
      const linksHighlighted = () => getAllLinks().filter((n) => n.classes(IS_HIGHLIGHTED));

      describe('on click', () => {
        it('highlights the clicked node and predecessors', () => {
          liveNode().trigger('click');

          expect(nodesNotHighlighted().length < getAllNodes().length).toBe(true);
          expect(linksNotHighlighted().length < getAllLinks().length).toBe(true);

          linksHighlighted().wrappers.forEach((link) => {
            expect(link.attributes('style')).toBe(strokeOpacity(highlightIn));
          });

          nodesHighlighted().wrappers.forEach((node) => {
            expect(node.attributes('stroke')).not.toBe('#f2f2f2');
          });

          linksNotHighlighted().wrappers.forEach((link) => {
            expect(link.attributes('style')).toBe(strokeOpacity(highlightOut));
          });

          nodesNotHighlighted().wrappers.forEach((node) => {
            expect(node.attributes('stroke')).toBe('#f2f2f2');
          });
        });

        it('toggles path off on second click', () => {
          liveNode().trigger('click');
          liveNode().trigger('click');

          expect(nodesNotHighlighted().length).toBe(getAllNodes().length);
          expect(linksNotHighlighted().length).toBe(getAllLinks().length);
        });

        it('preserves state when multiple nodes are toggled on and off', () => {
          anotherLiveNode().trigger('click');
          liveNode().trigger('click');
          anotherLiveNode().trigger('click');
          expect(nodesNotHighlighted().length < getAllNodes().length).toBe(true);
          expect(linksNotHighlighted().length < getAllLinks().length).toBe(true);
        });
      });
    });
  });
});