1import { PanelModel } from './PanelModel';
2import { deleteScopeVars, isOnTheSameGridRow } from './utils';
3import { REPEAT_DIR_HORIZONTAL } from '../../../core/constants';
4
5describe('isOnTheSameGridRow', () => {
6  describe('when source panel is next to a panel', () => {
7    it('then it should return true', () => {
8      const sourcePanel: PanelModel = ({ gridPos: { x: 0, y: 1, w: 4, h: 4 } } as unknown) as PanelModel;
9      const otherPanel: PanelModel = ({ gridPos: { x: 4, y: 1, w: 4, h: 4 } } as unknown) as PanelModel;
10
11      expect(isOnTheSameGridRow(sourcePanel, otherPanel)).toBe(true);
12    });
13  });
14
15  describe('when source panel is not next to a panel', () => {
16    it('then it should return false', () => {
17      const sourcePanel: PanelModel = ({ gridPos: { x: 0, y: 1, w: 4, h: 4 } } as unknown) as PanelModel;
18      const otherPanel: PanelModel = ({ gridPos: { x: 4, y: 5, w: 4, h: 4 } } as unknown) as PanelModel;
19
20      expect(isOnTheSameGridRow(sourcePanel, otherPanel)).toBe(false);
21    });
22  });
23
24  describe('when source panel is repeated horizontally', () => {
25    it('then it should return false', () => {
26      const sourcePanel: PanelModel = ({
27        gridPos: { x: 0, y: 1, w: 4, h: 4 },
28        repeatDirection: REPEAT_DIR_HORIZONTAL,
29      } as unknown) as PanelModel;
30      const otherPanel: PanelModel = ({ gridPos: { x: 4, y: 1, w: 4, h: 4 } } as unknown) as PanelModel;
31
32      expect(isOnTheSameGridRow(sourcePanel, otherPanel)).toBe(false);
33    });
34  });
35});
36
37describe('deleteScopeVars', () => {
38  describe('when called with a collapsed row with panels', () => {
39    it('then scopedVars should be deleted on the row and all collapsed panels', () => {
40      const panel1 = new PanelModel({
41        id: 1,
42        type: 'row',
43        collapsed: true,
44        scopedVars: { job: { value: 'myjob', text: 'myjob' } },
45        panels: [
46          { id: 2, type: 'graph', title: 'Graph', scopedVars: { job: { value: 'myjob', text: 'myjob' } } },
47          { id: 3, type: 'graph2', title: 'Graph2', scopedVars: { job: { value: 'myjob', text: 'myjob' } } },
48        ],
49      });
50
51      expect(panel1.scopedVars).toBeDefined();
52      expect(panel1.panels[0].scopedVars).toBeDefined();
53      expect(panel1.panels[1].scopedVars).toBeDefined();
54
55      deleteScopeVars([panel1]);
56
57      expect(panel1.scopedVars).toBeUndefined();
58      expect(panel1.panels[0].scopedVars).toBeUndefined();
59      expect(panel1.panels[1].scopedVars).toBeUndefined();
60    });
61  });
62});
63