Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: update fetch-mock #7857

Merged
merged 2 commits into from
Jul 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ui/app/components/stackedgraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ interface StackedAreaGraphProps extends MetricsDataComponentProps {
* on the same axis.
*/
export class StackedAreaGraph extends React.Component<StackedAreaGraphProps, {}> {

// The SVG Element in the DOM used to render the graph.
svgEl: SVGElement;

Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/banner/disconnectedBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let health = (state: AdminUIState): HealthState => state.cachedData.health;

// Connect the DisconnectedBanner class with our redux store.
let disconnectedBannerConnected = connect(
(state, ownProps) => {
(state: AdminUIState) => {
return {
health: health(state),
};
Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/banner/helpusBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ let helpusBannerDismissed = (state: AdminUIState): boolean => state.ui[HELPUS_BA

// Connect the HelpUsBanner class with our redux store.
let helpusBannerConnected = connect(
(state: AdminUIState, ownProps: any) => {
(state: AdminUIState) => {
return {
attributes: optinAttributes(state),
attributesLoaded: attributesLoaded(state),
Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/banner/outdatedBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ let buildtag = createSelector(

// Connect the DisconnectedBanner class with our redux store.
let outdatedBannerConnected = connect(
(state, ownProps) => {
(state: AdminUIState) => {
return {
clusterID: clusterID(state),
buildtag: buildtag(state),
Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/clusterOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ let clusterInfo = createSelector(
);

let clusterMainConnected = connect(
(state, ownProps) => {
(state: AdminUIState) => {
return {
clusterInfo: clusterInfo(state),
};
Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/databases/databaseDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ let sortedGrants = createSelector(

// Connect the DatabaseMain class with our redux store.
let databaseMainConnected = connect(
(state, ownProps) => {
(state: AdminUIState, ownProps: IInjectedProps) => {
return {
sortedTables: sortedTables(state, ownProps),
sortedGrants: sortedGrants(state, ownProps),
Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/databases/databaseList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ let sortedDatabases = createSelector(

// Connect the DatabasesMain class with our redux store.
let databasesMainConnected = connect(
(state, ownProps) => {
(state: AdminUIState) => {
return {
sortedDatabases: sortedDatabases(state),
sortSetting: sortSetting(state),
Expand Down
3 changes: 1 addition & 2 deletions ui/app/containers/databases/tableDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ interface TableMainData {
sortedColumns: Column[];
sortedIndexes: Index[];
sortedGrants: Grant[];

}

/**
Expand Down Expand Up @@ -433,7 +432,7 @@ let sortedGrants = createSelector(

// Connect the TableMain class with our redux store.
let tableMainConnected = connect(
(state, ownProps) => {
(state: AdminUIState, ownProps: IInjectedProps) => {
return {
sortedColumns: sortedColumns(state, ownProps),
sortedIndexes: sortedIndexes(state, ownProps),
Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ let events = (state: AdminUIState): Event[] => state.cachedData.events.data && s

// Connect the EventsList class with our redux store.
let eventsConnected = connect(
(state, ownProps) => {
(state: AdminUIState) => {
return {
events: events(state),
};
Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/helpus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let optinAttributes = (state: AdminUIState): OptInAttributes => state && state.u

// Connect the HelpUs class with our redux store.
let helpusConnected = connect(
(state, ownProps) => {
(state: AdminUIState) => {
return {
optInAttributes: optinAttributes(state),
};
Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/nodesOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ let rollupStatuses = createSelector(

// Connect the NodesMain class with our redux store.
let nodesMainConnected = connect(
(state, ownProps) => {
(state: AdminUIState) => {
return {
sortedStatuses: sortedStatuses(state),
statusRollups: rollupStatuses(state),
Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/raftRanges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ let rangeStatuses = (state: AdminUIState): cockroach.server.serverpb.RaftDebugRe

// Connect the RangesMain class with our redux store.
let rangesMainConnected = connect(
(state, ownProps) => {
(state: AdminUIState) => {
return {
rangeStatuses: rangeStatuses(state),
};
Expand Down
3 changes: 2 additions & 1 deletion ui/app/containers/timescale.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { connect } from "react-redux";
import classNames = require("classnames");
import _ = require("lodash");

import { AdminUIState } from "../redux/state";
import * as timewindow from "../redux/timewindow";

interface TimeScaleSelectorProps {
Expand Down Expand Up @@ -64,7 +65,7 @@ class TimeScaleSelector extends React.Component<TimeScaleSelectorProps, TimeScal
}

export default connect(
(state) => {
(state: AdminUIState) => {
return {
currentScale: (state.timewindow as timewindow.TimeWindowState).scale,
availableScales: timewindow.availableTimeScales,
Expand Down
3 changes: 2 additions & 1 deletion ui/app/containers/timewindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from "react";
import { connect } from "react-redux";
import moment = require("moment");

import { AdminUIState } from "../redux/state";
import * as timewindow from "../redux/timewindow";

interface TimeWindowManagerProps {
Expand Down Expand Up @@ -93,7 +94,7 @@ class TimeWindowManager extends React.Component<TimeWindowManagerProps, TimeWind
}

let timeWindowManagerConnected = connect(
(state) => {
(state: AdminUIState) => {
return {
timeWindow: state.timewindow,
};
Expand Down
3 changes: 3 additions & 0 deletions ui/app/js/object-assign.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface ObjectConstructor {
assign(target: any, ...sources: any[]): any;
}
25 changes: 25 additions & 0 deletions ui/app/js/object-assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill

if (typeof Object.assign != 'function') {
(function () {
Object.assign = function (target) {
'use strict';
if (target === undefined || target === null) {
throw new TypeError('Cannot convert undefined or null to object');
}

var output = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source !== undefined && source !== null) {
for (var nextKey in source) {
if (source.hasOwnProperty(nextKey)) {
output[nextKey] = source[nextKey];
}
}
}
}
return output;
};
})();
}
91 changes: 56 additions & 35 deletions ui/app/redux/databaseInfo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "chai";
import * as _ from "lodash";
import * as fetchMock from "fetch-mock";
import * as fetchMock from "../util/fetch-mock";

import * as databases from "./databaseInfo";
import * as protos from "../js/protos";
Expand Down Expand Up @@ -370,13 +370,16 @@ describe("databases reducers", function () {
afterEach(fetchMock.restore);

it("refreshes database list", function () {
fetchMock.mock("/_admin/v1/databases", "get", (url: string, requestObj: RequestInit) => {
assert.deepEqual(state.databaseInfo.databases, { inFlight: true, valid: false });
fetchMock.mock({
matcher: "/_admin/v1/databases",
method: "GET",
response: (url: string, requestObj: RequestInit) => {
assert.deepEqual(state.databaseInfo.databases, { inFlight: true, valid: false });

return {
sendAsJson: false,
body: response.toArrayBuffer(),
};
return {
body: response.toArrayBuffer(),
};
},
});

return databases.refreshDatabases()(dispatch, () => state).then(() => {
Expand All @@ -392,8 +395,12 @@ describe("databases reducers", function () {
it("handles database list errors", function () {
let error = new Error();

fetchMock.mock("/_admin/v1/databases", "get", (url: string, requestObj: RequestInit) => {
return { throws: error };
fetchMock.mock({
matcher: "/_admin/v1/databases",
method: "GET",
response: (url: string, requestObj: RequestInit) => {
return { throws: error };
},
});

return databases.refreshDatabases()(dispatch, () => state).then(() => {
Expand Down Expand Up @@ -436,17 +443,20 @@ describe("databases reducers", function () {
return Promise.all(_.map([DB1, DB2], (db: string) => {
let response: DatabaseDetailsResponseMessage;

fetchMock.reMock(re, "get", (url: string, requestObj: RequestInit) => {
let database = url.match(re)[1];
fetchMock.restore().mock({
matcher: re,
method: "GET",
response: (url: string, requestObj: RequestInit) => {
let database = url.match(re)[1];

assert.deepEqual(state.databaseInfo.databaseDetails[database], { inFlight: true, valid: false });
assert.deepEqual(state.databaseInfo.databaseDetails[database], { inFlight: true, valid: false });

response = new protos.cockroach.server.serverpb.DatabaseDetailsResponse(dbs[database]);
response = new protos.cockroach.server.serverpb.DatabaseDetailsResponse(dbs[database]);

return {
sendAsJson: false,
body: response.toArrayBuffer(),
};
return {
body: response.toArrayBuffer(),
};
},
});

return databases.refreshDatabaseDetails(db)(dispatch, () => state).then(() => {
Expand All @@ -467,12 +477,16 @@ describe("databases reducers", function () {
let re = new RegExp("/_admin/v1/databases/(.+)");

return Promise.all(_.map([DB1, DB2], (db: string) => {
fetchMock.reMock(re, "get", (url: string, requestObj: RequestInit) => {
let database = url.match(re)[1];
fetchMock.restore().mock({
matcher: re,
method: "GET",
response: (url: string, requestObj: RequestInit) => {
let database = url.match(re)[1];

assert.deepEqual(state.databaseInfo.databaseDetails[database], { inFlight: true, valid: false });
assert.deepEqual(state.databaseInfo.databaseDetails[database], { inFlight: true, valid: false });

return { throws: error };
return { throws: error };
},
});

return databases.refreshDatabaseDetails(db)(dispatch, () => state).then(() => {
Expand Down Expand Up @@ -538,19 +552,22 @@ describe("databases reducers", function () {
return Promise.all(_.map(tableList, (id: TableID) => {
let response: TableDetailsResponseMessage;

fetchMock.reMock(re, "get", (url: string, requestObj: RequestInit) => {
let result = url.match(re);
let database = result[1];
let table = result[2];
fetchMock.restore().mock({
matcher: re,
method: "GET",
response: (url: string, requestObj: RequestInit) => {
let result = url.match(re);
let database = result[1];
let table = result[2];

assert.deepEqual(state.databaseInfo.tableDetails[databases.generateTableID(id.db, id.table)], { inFlight: true, valid: false });
assert.deepEqual(state.databaseInfo.tableDetails[databases.generateTableID(id.db, id.table)], { inFlight: true, valid: false });

response = new protos.cockroach.server.serverpb.TableDetailsResponse(dbTables[databases.generateTableID(database, table)]);
response = new protos.cockroach.server.serverpb.TableDetailsResponse(dbTables[databases.generateTableID(database, table)]);

return {
sendAsJson: false,
body: response.toArrayBuffer(),
};
return {
body: response.toArrayBuffer(),
};
},
});

return databases.refreshTableDetails(id.db, id.table)(dispatch, () => state).then(() => {
Expand All @@ -570,10 +587,14 @@ describe("databases reducers", function () {
let error = new Error();

return Promise.all(_.map(tableList, (id: TableID) => {
fetchMock.reMock(new RegExp("/_admin/v1/databases/.+/tables/.+"), "get", (url: string, requestObj: RequestInit) => {
assert.deepEqual(state.databaseInfo.tableDetails[databases.generateTableID(id.db, id.table)], { inFlight: true, valid: false });

return { throws: error };
fetchMock.restore().mock({
matcher: new RegExp("/_admin/v1/databases/.+/tables/.+"),
method: "GET",
response: (url: string, requestObj: RequestInit) => {
assert.deepEqual(state.databaseInfo.tableDetails[databases.generateTableID(id.db, id.table)], { inFlight: true, valid: false });

return { throws: error };
},
});

return databases.refreshTableDetails(id.db, id.table)(dispatch, () => state).then(() => {
Expand Down
16 changes: 11 additions & 5 deletions ui/app/redux/metrics.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from "chai";
import _ = require("lodash");
import Long = require("long");
import * as fetchMock from "fetch-mock";
import * as fetchMock from "../util/fetch-mock";

import * as protos from "../js/protos";
import reducer, * as metrics from "./metrics";
Expand Down Expand Up @@ -156,15 +156,17 @@ describe("metrics reducer", function() {

// Mock out fetch server; we are only expecting requests to /ts/query,
// which we simply reflect with an empty set of datapoints.
fetchMock.mock("/ts/query", "post", (url: string, requestObj: RequestInit) => {
fetchMock.mock({
matcher: "/ts/query",
method: "POST",
response: (url: string, requestObj: RequestInit) => {
// Assert that metric store's "inFlight" is 1 or 2.
assert.isAtLeast(mockMetricsState.inFlight, 1);
assert.isAtMost(mockMetricsState.inFlight, 2);

let request = protos.cockroach.ts.tspb.TimeSeriesQueryRequest.decode(requestObj.body as ArrayBuffer);

return {
sendAsJson: false,
body: new protos.cockroach.ts.tspb.TimeSeriesQueryResponse({
results: _.map(request.queries, (q) => {
return {
Expand All @@ -174,6 +176,7 @@ describe("metrics reducer", function() {
}),
}).toArrayBuffer(),
};
},
});

// Dispatch several requests. Requests are divided among two timespans,
Expand Down Expand Up @@ -223,7 +226,10 @@ describe("metrics reducer", function() {
// Mock out fetch server; send a positive reply to the first request, and
// an error to the second request.
let successSent = false;
fetchMock.mock("/ts/query", "post", (url: string, requestObj: any) => {
fetchMock.mock({
matcher: "/ts/query",
method: "POST",
response: (url: string, requestObj: RequestInit) => {
// Assert that metric store's "inFlight" is 1.
assert.equal(mockMetricsState.inFlight, 1);

Expand All @@ -235,7 +241,6 @@ describe("metrics reducer", function() {
let request = protos.cockroach.ts.tspb.TimeSeriesQueryRequest.decode(requestObj.body as ArrayBuffer);

return {
sendAsJson: false,
body: new protos.cockroach.ts.tspb.TimeSeriesQueryResponse({
results: _.map(request.queries, (q) => {
return {
Expand All @@ -245,6 +250,7 @@ describe("metrics reducer", function() {
}),
}).toArrayBuffer(),
};
},
});

// Dispatch several requests. Requests are divided among two timespans,
Expand Down
Loading