Commit df467747 authored by Kyle Keating's avatar Kyle Keating

Remove promise-util

Replace assertPromiseIsRejected and toResult with assert.rejects
Signed-off-by: 's avatarKyle Keating <kkeating@hypothes.is>
parent ec3d81f3
import { toResult } from '../../../shared/test/promise-util';
import * as html from '../html';
import fixture from './html-anchoring-fixture.html';
......@@ -379,17 +378,15 @@ describe('HTML anchoring', function () {
exact: 'Lorem ipsum',
};
it('throws an error if anchoring using a quote fails', function () {
it('throws an error if anchoring using a quote fails', async function () {
const quoteSelector = {
type: 'TextQuoteSelector',
exact: 'This text does not appear in the web page',
};
return toResult(html.anchor(container, [quoteSelector])).then(function (
result
) {
assert.equal(result.error.message, 'Quote not found');
});
await assert.rejects(
html.anchor(container, [quoteSelector]),
'Quote not found'
);
});
it('does not throw an error if anchoring using a position fails', function () {
......
/**
* Helper to assert a promise is rejected.
*
* IMPORTANT NOTE: If you use this you must _return_ the result of this function
* from your test, otherwise the test runner will not know when your test is
* finished.
*
* @param {Promise} promise
* @param {string} expectedErr - Expected `message` property of error
*/
export function assertPromiseIsRejected(promise, expectedErr) {
const rejectFlag = {};
return promise
.catch(err => {
assert.equal(err.message, expectedErr);
return rejectFlag;
})
.then(result => {
assert.equal(
result,
rejectFlag,
'expected promise to be rejected but it was fulfilled'
);
});
}
/**
* Takes a Promise<T> and returns a Promise<Result>
* where Result = { result: T } | { error: any }.
*
* This is useful for testing that promises are rejected
* as expected in tests.
*
* Consider using `assertPromiseIsRejected` instead.
*/
export function toResult(promise) {
return promise
.then(function (result) {
return { result: result };
})
.catch(function (err) {
return { error: err };
});
}
......@@ -2,7 +2,7 @@
sinon.assert.expose(assert, { prefix: null });
// Patch extra assert helper methods
import patch from '../../test-util/assert-methods';
import { patch } from '../../test-util/assert-methods';
patch(assert);
import 'angular';
......
import EventEmitter from 'tiny-emitter';
import { assertPromiseIsRejected } from '../../../shared/test/promise-util';
import { call } from '../postmessage-json-rpc';
class FakeWindow {
......@@ -48,12 +47,9 @@ describe('sidebar.util.postmessage-json-rpc', () => {
});
});
it('rejects if `postMessage` fails', () => {
it('rejects if `postMessage` fails', async () => {
frame.postMessage.throws(new Error('Nope!'));
const result = doCall();
assertPromiseIsRejected(result, 'Nope!');
await assert.rejects(doCall(), 'Nope!');
});
[
......@@ -103,7 +99,7 @@ describe('sidebar.util.postmessage-json-rpc', () => {
});
});
it('rejects with an error if the `error` field is set in the response', () => {
it('rejects with an error if the `error` field is set in the response', async () => {
const result = doCall();
fakeWindow.emitter.emit('message', {
origin,
......@@ -115,21 +111,16 @@ describe('sidebar.util.postmessage-json-rpc', () => {
},
},
});
return assertPromiseIsRejected(result, 'Something went wrong');
await assert.rejects(result, 'Something went wrong');
});
it('rejects if no `error` or `result` field is set in the response', () => {
it('rejects if no `error` or `result` field is set in the response', async () => {
const result = doCall();
fakeWindow.emitter.emit('message', {
origin,
data: { jsonrpc: '2.0', id: messageId },
});
return assertPromiseIsRejected(
result,
'RPC reply had no result or error'
);
await assert.rejects(result, 'RPC reply had no result or error');
});
it('resolves with the result if the `result` field is set in the response', () => {
......@@ -149,10 +140,9 @@ describe('sidebar.util.postmessage-json-rpc', () => {
});
});
it('rejects with an error if the timeout is exceeded', () => {
const result = doCall();
return assertPromiseIsRejected(
result,
it('rejects with an error if the timeout is exceeded', async () => {
await assert.rejects(
doCall(),
'Request to https://embedder.com timed out'
);
});
......
import { toResult } from '../../../shared/test/promise-util';
import * as retryUtil from '../retry';
describe('sidebar.util.retry', function () {
......@@ -29,7 +28,7 @@ describe('sidebar.util.retry', function () {
});
});
it('should return the error if it repeatedly fails', function () {
it('should return the error if it repeatedly fails', async function () {
const error = new Error('error');
const operation = sinon.spy(function () {
return Promise.reject(error);
......@@ -38,9 +37,7 @@ describe('sidebar.util.retry', function () {
minTimeout: 3,
retries: 2,
});
return toResult(wrappedOperation).then(function (result) {
assert.equal(result.error, error);
});
await assert.rejects(wrappedOperation, 'error');
});
});
});
......@@ -10,7 +10,7 @@
* e.g. await rejects(someAsyncFunction(), /expected error/g);
*
* @param {Promise} promiseResult - The returned promise a function to test
* @param errorMessage {RegEx|String} - A string or regex that matches the error
* @param {RegEx|String} errorMessage - A string or regex that matches the error
* which is expected to be thrown.
*/
......@@ -40,6 +40,6 @@ const rejects = async (promiseResult, errorMessage) => {
*
* @param {Object} assert - global assertion object.
*/
export default function patch(assert) {
export function patch(assert) {
assert.rejects = rejects;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment