Skip to content

Commit

Permalink
Moved the throwing input tests to their own file.
Browse files Browse the repository at this point in the history
They're not really "basics" in my opinion.
  • Loading branch information
Pimm committed Dec 1, 2022
1 parent 99cb057 commit 35b151c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
2 changes: 1 addition & 1 deletion source/takeThen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function takeThen(input, /* This is never provided, thus initiall
return /* undefined */;
}
// Return undefined if retrieving the then property causes an error to be thrown. Since the promise awareness is
// somewhat of a hidden feature of this library, it should be operate as unintrusive as possible.
// somewhat of a hidden feature of this library, it should operate as unintrusive as possible.
try {
({ then } = input);
} catch (error) {
Expand Down
21 changes: 1 addition & 20 deletions test/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function createCallback() {
}

test('basics', () => {
expect.assertions(21);
expect.assertions(18);
// run[If] with non-null-ish argument.
run(createCallback(), callback => {
expect(
Expand Down Expand Up @@ -70,23 +70,4 @@ test('basics', () => {
).toBe(undefined);
expect(callback).toBeCalledWith(undefined);
});
// apply with object, which has a then getter which throws.
run(createCallback(), callback => {
const throwingGetter = {
get then() { throw new Error() }
}
expect(
() => apply(throwingGetter, callback)
).not.toThrow();
expect(callback).toHaveBeenCalledWith(throwingGetter);
});
// apply with object, where any getter throws.
run(createCallback(), callback => {
const throwingGetter = new Proxy({}, {
get then() { throw new Error() }
});
expect(
() => apply(throwingGetter, callback)
).not.toThrow();
});
});
69 changes: 69 additions & 0 deletions test/throwing-inputs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @jest-environment node
*/

import { run, runIf, apply } from '..';

function createCallback() {
return jest.fn(() => 'result');
}

const hostileObject = {
get then() {
throw 'error';
}
};

const hostileProxy = new Proxy({}, {
get(_, name) {
if ('then' == name) {
throw 'error';
}
}
});

test('throwing-inputs', () => {
expect.assertions(12);
// run with hostile object.
run(createCallback(), callback => {
expect(
() => run(hostileObject, callback)
).not.toThrow();
expect(callback).toHaveBeenCalledWith(hostileObject);
});
// runIf with hostile object.
run(createCallback(), callback => {
expect(
() => runIf(hostileObject, callback)
).not.toThrow();
expect(callback).toHaveBeenCalledWith(hostileObject);
});
// apply with hostile object.
run(createCallback(), callback => {
expect(
() => apply(hostileObject, callback)
).not.toThrow();
expect(callback).toHaveBeenCalledWith(hostileObject);
});
// run with hostile proxy.
run(createCallback(), callback => {
expect(
() => run(hostileProxy, callback)
).not.toThrow();
expect(callback).toHaveBeenCalledWith(hostileProxy);
});
// runIf with hostile proxy.
run(createCallback(), callback => {
expect(
() => runIf(hostileProxy, callback)
).not.toThrow();
expect(callback).toHaveBeenCalledWith(hostileProxy);
});
// apply with hostile proxy.
run(createCallback(), callback => {
expect(
() => apply(hostileProxy, callback)
).not.toThrow();
expect(callback).toHaveBeenCalledWith(hostileProxy);
});
});

0 comments on commit 35b151c

Please sign in to comment.