Skip to content

Commit

Permalink
refactor(@esri/hub-downloads): use enums and handle empty layers.
Browse files Browse the repository at this point in the history
affects: @esri/hub-downloads
  • Loading branch information
rgwozdz committed Nov 16, 2020
1 parent fd0ac9a commit 6c8d0a1
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 8 deletions.
21 changes: 14 additions & 7 deletions packages/downloads/src/portal/portal-request-download-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { getItem, IItem, searchItems } from "@esri/arcgis-rest-portal";
import { UserSession } from "@esri/arcgis-rest-auth";
import {
getLayer,
getService,
IFeatureServiceDefinition,
ILayerDefinition
} from "@esri/arcgis-rest-feature-layer";
import { DownloadFormat } from "../download-format";
import { urlBuilder, composeDownloadId } from "../utils";
enum CollectionTypes {
CSV = "CSV",
KML = "KML"
}
enum ItemTypes {
FeatureService = "Feature Service",
MapService = "Map Service"
}

/**
* @private
Expand Down Expand Up @@ -84,19 +91,19 @@ export function portalRequestDownloadMetadata(
function fetchCacheSearchMetadata(params: any): Promise<ICacheSearchMetadata> {
const { format, layerId, url, type, modified, authentication } = params;

if (type !== "Feature Service" && type !== "Map Service") {
if (type !== ItemTypes.FeatureService && type !== ItemTypes.MapService) {
return Promise.resolve({
modified,
format: getSearchFormat(format, false)
format
});
}

return getService({ url, authentication }).then(
(response: IFeatureServiceDefinition) => {
const layers: ILayerDefinition[] = response.layers;
const layers: ILayerDefinition[] = response.layers || [];
const multilayer = isMultilayerRequest(layerId, layers);
return {
format: getSearchFormat(format, multilayer),
format: multilayer ? getMultilayerSearchFormat(format) : format,
modified: extractLastEditDate(layers)
};
}
Expand All @@ -122,8 +129,8 @@ function extractLastEditDate(layers: ILayerDefinition[]) {
return result[0];
}

function getSearchFormat(format: DownloadFormat, multilayer: boolean) {
if (multilayer && (format === "CSV" || format === "KML")) {
function getMultilayerSearchFormat(format: DownloadFormat) {
if (format === CollectionTypes.CSV || format === CollectionTypes.KML) {
return `${format} Collection`;
}
return format;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,75 @@ describe("portalRequestDownloadMetadata", () => {
}
});

it("no layer id, no layers, not cached", async done => {
try {
const getItemSpy = spyOn(portal, "getItem").and.returnValue(
new Promise(resolve => {
resolve({
type: "Feature Service",
modified: new Date(1593450876).getTime(),
url: "http://feature-service.com/FeatureServer"
});
})
);

const getServiceSpy = spyOn(featureLayer, "getService").and.returnValue(
new Promise(resolve => {
resolve({});
})
);

const searchItemsSpy = spyOn(portal, "searchItems").and.returnValue(
new Promise(resolve => {
resolve({ results: [] });
})
);

const result = await portalRequestDownloadMetadata({
datasetId: "abcdef0123456789abcdef0123456789",
format: "CSV",
authentication
});

expect(result).toEqual({
downloadId:
"abcdef0123456789abcdef0123456789:CSV:undefined:undefined:undefined",
lastEditDate: undefined,
status: "not_ready"
});

expect(getItemSpy.calls.count()).toEqual(1);
expect(getItemSpy.calls.argsFor(0)).toEqual([
"abcdef0123456789abcdef0123456789",
{
authentication
}
]);
expect(getServiceSpy.calls.count()).toEqual(1);
expect(getServiceSpy.calls.argsFor(0)).toEqual([
{
url: "http://feature-service.com/FeatureServer",
authentication
}
]);
expect(searchItemsSpy.calls.count()).toEqual(1);
expect(searchItemsSpy.calls.argsFor(0)).toEqual([
{
authentication,
num: 1,
q:
'type:"CSV" AND typekeywords:"export:abcdef0123456789abcdef0123456789,spatialRefId:undefined"',
sortField: "modified",
sortOrder: "DESC"
}
]);
} catch (err) {
expect(err).toEqual(undefined);
} finally {
done();
}
});

it("no layer id, multi-layer, no lastEditDate, not cached", async done => {
try {
const getItemSpy = spyOn(portal, "getItem").and.returnValue(
Expand Down Expand Up @@ -820,7 +889,7 @@ describe("portalRequestDownloadMetadata", () => {
});
});

describe("feature-service items, format KML", () => {
describe("feature-service items, format Shapefile", () => {
it("no layer id, multi-layer, no lastEditDate, not cached", async done => {
try {
const getItemSpy = spyOn(portal, "getItem").and.returnValue(
Expand Down Expand Up @@ -893,6 +962,79 @@ describe("portalRequestDownloadMetadata", () => {
});
});

describe("feature-service items, format KML", () => {
it("no layer id, multi-layer, no lastEditDate, not cached", async done => {
try {
const getItemSpy = spyOn(portal, "getItem").and.returnValue(
new Promise(resolve => {
resolve({
type: "Feature Service",
modified: new Date(1593450876).getTime(),
url: "http://feature-service.com/FeatureServer"
});
})
);

const getServiceSpy = spyOn(featureLayer, "getService").and.returnValue(
new Promise(resolve => {
resolve({
layers: [{ id: 0 }, { id: 1 }]
});
})
);

const searchItemsSpy = spyOn(portal, "searchItems").and.returnValue(
new Promise(resolve => {
resolve({ results: [] });
})
);

const result = await portalRequestDownloadMetadata({
datasetId: "abcdef0123456789abcdef0123456789",
format: "Shapefile",
authentication
});

expect(result).toEqual({
downloadId:
"abcdef0123456789abcdef0123456789:Shapefile:undefined:undefined:undefined",
lastEditDate: undefined,
status: "not_ready"
});

expect(getItemSpy.calls.count()).toEqual(1);
expect(getItemSpy.calls.argsFor(0)).toEqual([
"abcdef0123456789abcdef0123456789",
{
authentication
}
]);
expect(getServiceSpy.calls.count()).toEqual(1);
expect(getServiceSpy.calls.argsFor(0)).toEqual([
{
url: "http://feature-service.com/FeatureServer",
authentication
}
]);
expect(searchItemsSpy.calls.count()).toEqual(1);
expect(searchItemsSpy.calls.argsFor(0)).toEqual([
{
authentication,
num: 1,
q:
'type:"Shapefile" AND typekeywords:"export:abcdef0123456789abcdef0123456789,spatialRefId:undefined"',
sortField: "modified",
sortOrder: "DESC"
}
]);
} catch (err) {
expect(err).toEqual(undefined);
} finally {
done();
}
});
});

describe("CSV items", () => {
it("with spatialRefId", async done => {
try {
Expand Down

0 comments on commit 6c8d0a1

Please sign in to comment.