From 2b0f0aed17f1e7314a8c164293290f67efa8a622 Mon Sep 17 00:00:00 2001 From: yulia Date: Thu, 27 Jul 2017 15:22:25 +0200 Subject: [PATCH] Bugfix: TypeError: client.sourceContents is not a function (#3472) --- src/actions/tests/breakpoints.js | 3 +- src/actions/tests/helpers/breakpoints.js | 62 ------------ src/actions/tests/helpers/threadClient.js | 116 ++++++++++++++++++++++ src/actions/tests/pending-breakpoints.js | 7 +- src/actions/tests/sources.js | 35 +------ src/actions/tests/tabs.js | 8 +- 6 files changed, 128 insertions(+), 103 deletions(-) create mode 100644 src/actions/tests/helpers/threadClient.js diff --git a/src/actions/tests/breakpoints.js b/src/actions/tests/breakpoints.js index 6c6d1c9541..676d402d1f 100644 --- a/src/actions/tests/breakpoints.js +++ b/src/actions/tests/breakpoints.js @@ -4,10 +4,11 @@ import { actions, makeSource } from "../../utils/test-head"; + import { simulateCorrectThreadClient, simpleMockThreadClient -} from "./helpers/breakpoints.js"; +} from "./helpers/threadClient.js"; describe("breakpoints", () => { it("should add a breakpoint", async () => { diff --git a/src/actions/tests/helpers/breakpoints.js b/src/actions/tests/helpers/breakpoints.js index c23ded8010..f0f91521b7 100644 --- a/src/actions/tests/helpers/breakpoints.js +++ b/src/actions/tests/helpers/breakpoints.js @@ -1,16 +1,3 @@ -import { makeLocationId } from "../../../utils/breakpoint"; - -const sourceFixtures = { - foo1: { - source: "function foo1() {\n return 5;\n}", - contentType: "text/javascript" - }, - foo2: { - source: "function foo2(x, y) {\n return x + y;\n}", - contentType: "text/javascript" - } -}; - export function mockPendingBreakpoint(overrides = {}) { const { sourceUrl, line, column, condition, disabled } = overrides; return { @@ -29,35 +16,6 @@ export function mockPendingBreakpoint(overrides = {}) { }; } -function generateCorrectingThreadClient(offset = 0) { - return { - getBreakpointByLocation: jest.fn(), - setBreakpoint: (location, condition) => { - const actualLocation = Object.assign({}, location, { - line: location.line + offset - }); - - return Promise.resolve({ - id: makeLocationId(location), - actualLocation, - condition - }); - } - }; -} - -/* in some cases, a breakpoint may be added, but the source will respond - * with a different breakpoint location. This is due to the breakpoint being - * added between functions, or somewhere that doesnt make sense. This function - * simulates that behavior. - * */ -export function simulateCorrectThreadClient(offset, location) { - const correctedThreadClient = generateCorrectingThreadClient(offset); - const offsetLine = { line: location.line + offset }; - const correctedLocation = Object.assign({}, location, offsetLine); - return { correctedThreadClient, correctedLocation }; -} - export function generateBreakpoint(filename) { return { location: { @@ -69,23 +27,3 @@ export function generateBreakpoint(filename) { disabled: false }; } - -export const simpleMockThreadClient = { - getBreakpointByLocation: jest.fn(), - setBreakpoint: (location, _condition) => - Promise.resolve({ id: "hi", actualLocation: location }), - - removeBreakpoint: _id => Promise.resolve(), - - setBreakpointCondition: (_id, _location, _condition, _noSliding) => - Promise.resolve({ sourceId: "a", line: 5 }), - - sourceContents: sourceId => - new Promise((resolve, reject) => { - if (sourceFixtures[sourceId]) { - resolve(sourceFixtures[sourceId]); - } - - reject(`unknown source: ${sourceId}`); - }) -}; diff --git a/src/actions/tests/helpers/threadClient.js b/src/actions/tests/helpers/threadClient.js new file mode 100644 index 0000000000..0472d9b1fb --- /dev/null +++ b/src/actions/tests/helpers/threadClient.js @@ -0,0 +1,116 @@ +import { makeLocationId } from "../../../utils/breakpoint"; + +const sourceFixtures = { + foo1: { + source: "function foo1() {\n return 5;\n}", + contentType: "text/javascript" + }, + foo2: { + source: "function foo2(x, y) {\n return x + y;\n}", + contentType: "text/javascript" + } +}; + +export const simpleMockThreadClient = { + getBreakpointByLocation: jest.fn(), + setBreakpoint: (location, _condition) => + Promise.resolve({ id: "hi", actualLocation: location }), + + removeBreakpoint: _id => Promise.resolve(), + + setBreakpointCondition: (_id, _location, _condition, _noSliding) => + Promise.resolve({ sourceId: "a", line: 5 }), + + sourceContents: sourceId => + new Promise((resolve, reject) => { + if (sourceFixtures[sourceId]) { + resolve(sourceFixtures[sourceId]); + } + + reject(`unknown source: ${sourceId}`); + }) +}; + +// Breakpoint Sliding +function generateCorrectingThreadClient(offset = 0) { + return { + getBreakpointByLocation: jest.fn(), + setBreakpoint: (location, condition) => { + const actualLocation = Object.assign({}, location, { + line: location.line + offset + }); + + return Promise.resolve({ + id: makeLocationId(location), + actualLocation, + condition + }); + } + }; +} + +/* in some cases, a breakpoint may be added, but the source will respond + * with a different breakpoint location. This is due to the breakpoint being + * added between functions, or somewhere that doesnt make sense. This function + * simulates that behavior. + * */ +export function simulateCorrectThreadClient(offset, location) { + const correctedThreadClient = generateCorrectingThreadClient(offset); + const offsetLine = { line: location.line + offset }; + const correctedLocation = Object.assign({}, location, offsetLine); + return { correctedThreadClient, correctedLocation }; +} + +// sources and tabs +export const sourceThreadClient = { + sourceContents: function(sourceId) { + return new Promise((resolve, reject) => { + switch (sourceId) { + case "foo1": + resolve({ + source: "function foo1() {\n return 5;\n}", + contentType: "text/javascript" + }); + break; + case "foo2": + resolve({ + source: "function foo2(x, y) {\n return x + y;\n}", + contentType: "text/javascript" + }); + break; + case "foobar.js": + resolve({ + source: "function foo() {\n return 2;\n}", + contentType: "text/javascript" + }); + break; + case "barfoo.js": + resolve({ + source: "function bar() {\n return 3;\n}", + contentType: "text/javascript" + }); + break; + case "foo.js": + resolve({ + source: "function bar() {\n return 3;\n}", + contentType: "text/javascript" + }); + break; + case "bar.js": + resolve({ + source: "function bar() {\n return 3;\n}", + contentType: "text/javascript" + }); + break; + case "bazz.js": + resolve({ + source: "function bar() {\n return 3;\n}", + contentType: "text/javascript" + }); + break; + } + + reject(`unknown source: ${sourceId}`); + }); + } +}; diff --git a/src/actions/tests/pending-breakpoints.js b/src/actions/tests/pending-breakpoints.js index 0f7d179511..160075948c 100644 --- a/src/actions/tests/pending-breakpoints.js +++ b/src/actions/tests/pending-breakpoints.js @@ -1,10 +1,13 @@ // TODO: we would like to mock this in the local tests import { generateBreakpoint, - mockPendingBreakpoint, + mockPendingBreakpoint +} from "./helpers/breakpoints.js"; + +import { simulateCorrectThreadClient, simpleMockThreadClient -} from "./helpers/breakpoints.js"; +} from "./helpers/threadClient.js"; import { prefs } from "../../utils/prefs"; diff --git a/src/actions/tests/sources.js b/src/actions/tests/sources.js index b49df70bae..585a4dd227 100644 --- a/src/actions/tests/sources.js +++ b/src/actions/tests/sources.js @@ -13,40 +13,7 @@ const { getSelectedLocation } = selectors; -const threadClient = { - sourceContents: function(sourceId) { - return new Promise((resolve, reject) => { - switch (sourceId) { - case "foo1": - resolve({ - source: "function foo1() {\n return 5;\n}", - contentType: "text/javascript" - }); - break; - case "foo2": - resolve({ - source: "function foo2(x, y) {\n return x + y;\n}", - contentType: "text/javascript" - }); - break; - case "foobar.js": - resolve({ - source: "function foo() {\n return 2;\n}", - contentType: "text/javascript" - }); - break; - case "barfoo.js": - resolve({ - source: "function bar() {\n return 3;\n}", - contentType: "text/javascript" - }); - break; - } - - reject(`unknown source: ${sourceId}`); - }); - } -}; +import { sourceThreadClient as threadClient } from "./helpers/threadClient.js"; process.on("unhandledRejection", (reason, p) => {}); diff --git a/src/actions/tests/tabs.js b/src/actions/tests/tabs.js index b5fc57a221..9092ad2d83 100644 --- a/src/actions/tests/tabs.js +++ b/src/actions/tests/tabs.js @@ -6,7 +6,7 @@ import { } from "../../utils/test-head"; const { getSelectedSource, getSourceTabs } = selectors; -const threadClient = {}; +import { sourceThreadClient as threadClient } from "./helpers/threadClient.js"; describe("closing tabs", () => { it("closing a tab", async () => { @@ -54,7 +54,7 @@ describe("closing tabs", () => { }); it("closing many inactive tabs", async () => { - const { dispatch, getState } = createStore({}); + const { dispatch, getState } = createStore(threadClient); await dispatch(actions.newSource(makeSource("foo.js"))); await dispatch(actions.newSource(makeSource("bar.js"))); await dispatch(actions.newSource(makeSource("bazz.js"))); @@ -73,7 +73,7 @@ describe("closing tabs", () => { }); it("closing many tabs including the active tab", async () => { - const { dispatch, getState } = createStore({}); + const { dispatch, getState } = createStore(threadClient); await dispatch(actions.newSource(makeSource("foo.js"))); await dispatch(actions.newSource(makeSource("bar.js"))); await dispatch(actions.newSource(makeSource("bazz.js"))); @@ -92,7 +92,7 @@ describe("closing tabs", () => { }); it("closing all the tabs", async () => { - const { dispatch, getState } = createStore({}); + const { dispatch, getState } = createStore(threadClient); await dispatch(actions.newSource(makeSource("foo.js"))); await dispatch(actions.newSource(makeSource("bar.js"))); dispatch(actions.selectSource("foo.js"));