summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/selectors/test/visibleColumnBreakpoints.spec.js
blob: 873bf35ae2c0d6790f08228320c618159859f194 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

import { actions, createStore, makeSource } from "../../utils/test-head";
import { createLocation } from "../../utils/location";

import {
  getColumnBreakpoints,
  getFirstBreakpointPosition,
} from "../visibleColumnBreakpoints";
import {
  makeMockSource,
  makeMockSourceWithContent,
  makeMockBreakpoint,
} from "../../utils/test-mockup";

function pp(line, column) {
  return {
    location: { sourceId: "foo", line, column },
    generatedLocation: { sourceId: "foo", line, column },
  };
}

function defaultSource() {
  return makeMockSource(undefined, "foo");
}

function bp(line, column) {
  return makeMockBreakpoint(defaultSource(), line, column);
}

const source = makeMockSourceWithContent(
  undefined,
  "foo.js",
  undefined,
  `function foo() {
  console.log("hello");
}
console.log('bye');
`
);

describe("visible column breakpoints", () => {
  it("simple", () => {
    const viewport = {
      start: { line: 1, column: 0 },
      end: { line: 10, column: 10 },
    };
    const pausePoints = { 1: [pp(1, 1), pp(1, 5)], 3: [pp(3, 1)] };
    const breakpoints = [bp(1, 1), bp(4, 0), bp(4, 3)];

    const columnBps = getColumnBreakpoints(
      pausePoints,
      breakpoints,
      viewport,
      source,
      source.content
    );
    expect(columnBps).toMatchSnapshot();
  });

  it("ignores single breakpoints", () => {
    const viewport = {
      start: { line: 1, column: 0 },
      end: { line: 10, column: 10 },
    };
    const pausePoints = { 1: [pp(1, 1), pp(1, 3)], 2: [pp(2, 1)] };
    const breakpoints = [bp(1, 1)];
    const columnBps = getColumnBreakpoints(
      pausePoints,
      breakpoints,
      viewport,
      source,
      source.content
    );
    expect(columnBps).toMatchSnapshot();
  });

  it("only shows visible breakpoints", () => {
    const viewport = {
      start: { line: 1, column: 0 },
      end: { line: 10, column: 10 },
    };
    const pausePoints = { 1: [pp(1, 1), pp(1, 3)], 20: [pp(20, 1)] };
    const breakpoints = [bp(1, 1)];

    const columnBps = getColumnBreakpoints(
      pausePoints,
      breakpoints,
      viewport,
      source,
      source.content
    );
    expect(columnBps).toMatchSnapshot();
  });

  it("doesnt show breakpoints to the right", () => {
    const viewport = {
      start: { line: 1, column: 0 },
      end: { line: 10, column: 10 },
    };
    const pausePoints = { 1: [pp(1, 1), pp(1, 15)], 20: [pp(20, 1)] };
    const breakpoints = [bp(1, 1), bp(1, 15)];

    const columnBps = getColumnBreakpoints(
      pausePoints,
      breakpoints,
      viewport,
      source,
      source.content
    );
    expect(columnBps).toMatchSnapshot();
  });
});

describe("getFirstBreakpointPosition", () => {
  it("sorts the positions by column", async () => {
    const store = createStore();
    const { dispatch, getState } = store;

    const fooSource = await dispatch(
      actions.newGeneratedSource(makeSource("foo1"))
    );

    dispatch({
      type: "ADD_BREAKPOINT_POSITIONS",
      positions: [pp(1, 5), pp(1, 3)],
      source: fooSource,
    });

    const position = getFirstBreakpointPosition(
      getState(),
      createLocation({
        line: 1,
        source: fooSource,
      })
    );

    if (!position) {
      throw new Error("There should be a position");
    }
    expect(position.location.column).toEqual(3);
  });
});