2018-02-21 14:41:05 -08:00
|
|
|
import { module, test } from 'qunit';
|
2020-08-26 15:44:50 -07:00
|
|
|
import { throwIfApiError, ApiError } from '@datahub/utils/api/error';
|
|
|
|
import { ApiResponseStatus } from '@datahub/utils/api/shared';
|
2018-02-21 14:41:05 -08:00
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
module('Unit | Utility | api/errors/errors', function(): void {
|
|
|
|
test('throwIfApiError exists', function(assert): void {
|
2018-08-09 23:25:55 -07:00
|
|
|
assert.ok(typeof throwIfApiError === 'function', 'throwIfApiError exists as a function');
|
|
|
|
});
|
2018-02-21 14:41:05 -08:00
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
test('throwIfApiError returns a Promise / thennable', function(assert): void {
|
2018-08-09 23:25:55 -07:00
|
|
|
assert.ok(
|
2020-08-26 15:44:50 -07:00
|
|
|
typeof throwIfApiError(
|
|
|
|
({ status: 200, ok: true, json: (): Promise<void> => Promise.resolve() } as unknown) as Response,
|
|
|
|
(): Promise<void> => Promise.resolve()
|
|
|
|
).then === 'function',
|
2018-08-09 23:25:55 -07:00
|
|
|
'invocation returns a Promise object / thennable'
|
|
|
|
);
|
|
|
|
});
|
2018-02-22 15:59:56 -08:00
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
test('ApiError subclasses built-in Error and has attributes', function(assert): void {
|
2018-08-09 23:25:55 -07:00
|
|
|
const status = ApiResponseStatus.NotFound;
|
2020-08-26 15:44:50 -07:00
|
|
|
const apiError = new ApiError(status, '');
|
2018-02-22 15:59:56 -08:00
|
|
|
|
2018-08-09 23:25:55 -07:00
|
|
|
assert.ok(apiError instanceof Error, 'is an instanceof Error');
|
|
|
|
assert.ok(apiError.timestamp instanceof Date, 'has a valid timestamp');
|
|
|
|
assert.ok(apiError.status === status, 'has a status attribute');
|
|
|
|
});
|
2018-02-22 15:59:56 -08:00
|
|
|
});
|