mirror of
https://github.com/mapillary/mapillary-js.git
synced 2026-01-25 14:07:28 +00:00
Migrate to Rollup. Emit umd, minified umd, module, type declarations and source maps. Work around problems with Falcor by using virtual module. Migrate to Jest. Run tests in with CommonJS modules becasue ES6 is not supported. Work around dependency problems by bootstrap to ensure they are not loaded during test. Remove all unused dependencies. Create state transition matrix to avoid circular dependencies. chore: tsc and rollup watch build chore: incremental watch build Remove unused dependencies. chore: use existing type packages chore: mock favicon chore: improve watch build chore: fix styles build chore: specific commonjs build for tests chore: reset spec files refactor: explicit state transition matrix chore: make src commands work chore: working unit tests and lib chore: add serve docs script chore: remove unused bundle config chore: refactor rollup config to avoid duplication fix: falcor imports fix: transition from source to target Fixes #87 Fixes #100 Fixes #159 Fixes #208
205 lines
7.0 KiB
TypeScript
205 lines
7.0 KiB
TypeScript
import { BufferFetcher } from "../../src/api/BufferFetcher";
|
|
import { DataProviderBase } from "../../src/api/DataProviderBase";
|
|
import { MapillaryError } from "../../src/error/MapillaryError";
|
|
import { GeometryProvider } from "../helper/ProviderHelper";
|
|
|
|
class XMLHTTPRequestMock {
|
|
public response: {};
|
|
public responseType: string;
|
|
public status: number;
|
|
public timeout: number;
|
|
|
|
public onload: (e: Event) => any;
|
|
public onerror: (e: Event) => any;
|
|
public ontimeout: (e: Event) => any;
|
|
public onabort: (e: Event) => any;
|
|
|
|
public abort(): void { this.onabort(new Event("abort")); }
|
|
public open(...args: any[]): void { return; }
|
|
public send(...args: any[]): void { return; }
|
|
};
|
|
|
|
export class DataProvider extends DataProviderBase {
|
|
constructor() { super(new GeometryProvider()); }
|
|
public getArrayBuffer(abort?: Promise<void>): Promise<ArrayBuffer> {
|
|
return BufferFetcher.getArrayBuffer("", abort);
|
|
}
|
|
}
|
|
|
|
describe("DataProviderBase.ctor", () => {
|
|
test("should create a data provider base", () => {
|
|
const provider = new DataProvider();
|
|
|
|
expect(provider).toBeDefined();
|
|
expect(provider).toBeInstanceOf(DataProvider);
|
|
expect(provider).toBeInstanceOf(DataProviderBase);
|
|
});
|
|
});
|
|
|
|
describe("DataProviderBase.getArrayBuffer", () => {
|
|
test(
|
|
"should resolve array buffer with undefined abort parameter",
|
|
(done: Function) => {
|
|
|
|
const requestMock: XMLHTTPRequestMock = new XMLHTTPRequestMock();
|
|
spyOn(window, <keyof Window>"XMLHttpRequest").and.returnValue(requestMock);
|
|
|
|
const provider: DataProvider = new DataProvider();
|
|
|
|
const response: ArrayBuffer = new ArrayBuffer(1024);
|
|
|
|
provider.getArrayBuffer(undefined)
|
|
.then(
|
|
(buffer: ArrayBuffer): void => {
|
|
expect(buffer instanceof ArrayBuffer).toBe(true);
|
|
expect(buffer).toEqual(response);
|
|
done();
|
|
});
|
|
|
|
requestMock.status = 200;
|
|
requestMock.response = response;
|
|
requestMock.onload(undefined);
|
|
});
|
|
|
|
test(
|
|
"should resolve array buffer with defined abort parameter",
|
|
(done: Function) => {
|
|
|
|
const requestMock: XMLHTTPRequestMock = new XMLHTTPRequestMock();
|
|
spyOn(window, <keyof Window>"XMLHttpRequest").and.returnValue(requestMock);
|
|
|
|
const abort: Promise<void> = new Promise((_, __): void => { /*noop*/ });
|
|
const provider: DataProvider = new DataProvider();
|
|
const response: ArrayBuffer = new ArrayBuffer(1024);
|
|
|
|
provider.getArrayBuffer(abort)
|
|
.then(
|
|
(buffer: ArrayBuffer): void => {
|
|
expect(buffer instanceof ArrayBuffer).toBe(true);
|
|
expect(buffer).toEqual(response);
|
|
done();
|
|
});
|
|
|
|
requestMock.status = 200;
|
|
requestMock.response = response;
|
|
requestMock.onload(undefined);
|
|
});
|
|
|
|
test("should reject with abort", (done: Function) => {
|
|
const requestMock: XMLHTTPRequestMock = new XMLHTTPRequestMock();
|
|
spyOn(window, <keyof Window>"XMLHttpRequest").and.returnValue(requestMock);
|
|
|
|
let aborter: Function;
|
|
const abort: Promise<void> = new Promise(
|
|
(_, reject): void => {
|
|
aborter = reject;
|
|
});
|
|
|
|
const provider: DataProvider = new DataProvider();
|
|
|
|
provider.getArrayBuffer(abort)
|
|
.then(
|
|
undefined,
|
|
(reason: Error): void => {
|
|
expect(reason instanceof MapillaryError).toBe(true);
|
|
expect(reason.message).toContain("abort");
|
|
|
|
done();
|
|
});
|
|
|
|
aborter();
|
|
});
|
|
|
|
test("should reject with status", (done: Function) => {
|
|
const requestMock: XMLHTTPRequestMock = new XMLHTTPRequestMock();
|
|
spyOn(window, <keyof Window>"XMLHttpRequest").and.returnValue(requestMock);
|
|
|
|
const abort: Promise<void> = new Promise((_, __): void => { /*noop*/ });
|
|
const provider: DataProvider = new DataProvider();
|
|
|
|
const response: ArrayBuffer = new ArrayBuffer(1024);
|
|
|
|
provider.getArrayBuffer(abort)
|
|
.then(
|
|
undefined,
|
|
(reason: Error): void => {
|
|
expect(reason instanceof MapillaryError).toBe(true);
|
|
expect(reason.message).toContain("status");
|
|
|
|
done();
|
|
});
|
|
|
|
requestMock.status = 404;
|
|
requestMock.response = response;
|
|
requestMock.onload(undefined);
|
|
});
|
|
|
|
test("should reject with empty", (done: Function) => {
|
|
const requestMock: XMLHTTPRequestMock = new XMLHTTPRequestMock();
|
|
spyOn(window, <keyof Window>"XMLHttpRequest").and.returnValue(requestMock);
|
|
|
|
const abort: Promise<void> = new Promise((_, __): void => { /*noop*/ });
|
|
const provider: DataProvider = new DataProvider();
|
|
|
|
const response: ArrayBuffer = new ArrayBuffer(1024);
|
|
|
|
provider.getArrayBuffer(abort)
|
|
.then(
|
|
undefined,
|
|
(reason: Error): void => {
|
|
expect(reason instanceof MapillaryError).toBe(true);
|
|
expect(reason.message).toContain("empty");
|
|
|
|
done();
|
|
});
|
|
|
|
requestMock.status = 200;
|
|
requestMock.response = undefined;
|
|
requestMock.onload(undefined);
|
|
});
|
|
|
|
test("should reject with error", (done: Function) => {
|
|
const requestMock: XMLHTTPRequestMock = new XMLHTTPRequestMock();
|
|
spyOn(window, <keyof Window>"XMLHttpRequest").and.returnValue(requestMock);
|
|
|
|
const abort: Promise<void> = new Promise((_, __): void => { /*noop*/ });
|
|
const provider: DataProvider = new DataProvider();
|
|
|
|
const response: ArrayBuffer = new ArrayBuffer(1024);
|
|
|
|
provider.getArrayBuffer(abort)
|
|
.then(
|
|
undefined,
|
|
(reason: Error): void => {
|
|
expect(reason instanceof MapillaryError).toBe(true);
|
|
expect(reason.message).toContain("error");
|
|
|
|
done();
|
|
});
|
|
|
|
requestMock.onerror(undefined);
|
|
});
|
|
|
|
test("should reject with timeout", (done: Function) => {
|
|
const requestMock: XMLHTTPRequestMock = new XMLHTTPRequestMock();
|
|
spyOn(window, <keyof Window>"XMLHttpRequest").and.returnValue(requestMock);
|
|
|
|
const abort: Promise<void> = new Promise((_, __): void => { /*noop*/ });
|
|
const provider: DataProvider = new DataProvider();
|
|
|
|
const response: ArrayBuffer = new ArrayBuffer(1024);
|
|
|
|
provider.getArrayBuffer(abort)
|
|
.then(
|
|
undefined,
|
|
(reason: Error): void => {
|
|
expect(reason instanceof MapillaryError).toBe(true);
|
|
expect(reason.message).toContain("timeout");
|
|
|
|
done();
|
|
});
|
|
|
|
requestMock.ontimeout(undefined);
|
|
});
|
|
});
|