1 | 'use strict';
|
---|
2 |
|
---|
3 | /* globals window */
|
---|
4 |
|
---|
5 | var test = require('tape');
|
---|
6 | var isAsyncFunction = require('../index');
|
---|
7 | var generatorFuncs = require('make-generator-function')();
|
---|
8 | var asyncFuncs = require('make-async-function').list();
|
---|
9 | var hasToStringTag = require('has-tostringtag/shams')();
|
---|
10 |
|
---|
11 | var forEach = function (arr, func) {
|
---|
12 | var i;
|
---|
13 | for (i = 0; i < arr.length; ++i) {
|
---|
14 | func(arr[i], i, arr);
|
---|
15 | }
|
---|
16 | };
|
---|
17 |
|
---|
18 | test('returns false for non-functions', function (t) {
|
---|
19 | var nonFuncs = [
|
---|
20 | true,
|
---|
21 | false,
|
---|
22 | null,
|
---|
23 | undefined,
|
---|
24 | {},
|
---|
25 | [],
|
---|
26 | /a/g,
|
---|
27 | 'string',
|
---|
28 | 42,
|
---|
29 | new Date()
|
---|
30 | ];
|
---|
31 | t.plan(nonFuncs.length);
|
---|
32 | forEach(nonFuncs, function (nonFunc) {
|
---|
33 | t.notOk(isAsyncFunction(nonFunc), nonFunc + ' is not a function');
|
---|
34 | });
|
---|
35 | t.end();
|
---|
36 | });
|
---|
37 |
|
---|
38 | test('returns false for non-async functions', function (t) {
|
---|
39 | var func = function () {};
|
---|
40 | t.notOk(isAsyncFunction(func), 'anonymous function is not an async function');
|
---|
41 |
|
---|
42 | var namedFunc = function foo() {};
|
---|
43 | t.notOk(isAsyncFunction(namedFunc), 'named function is not an async function');
|
---|
44 |
|
---|
45 | if (typeof window === 'undefined') {
|
---|
46 | t.skip('window.alert is not an async function');
|
---|
47 | } else {
|
---|
48 | t.notOk(isAsyncFunction(window.alert), 'window.alert is not an async function');
|
---|
49 | }
|
---|
50 | t.end();
|
---|
51 | });
|
---|
52 |
|
---|
53 | var fakeToString = function () { return 'async function () { return "TOTALLY REAL I SWEAR!"; }'; };
|
---|
54 |
|
---|
55 | test('returns false for non-async function with faked toString', function (t) {
|
---|
56 | var func = function () {};
|
---|
57 | func.toString = fakeToString;
|
---|
58 |
|
---|
59 | t.notEqual(String(func), Function.prototype.toString.apply(func), 'faked toString is not real toString');
|
---|
60 | t.notOk(isAsyncFunction(func), 'anonymous function with faked toString is not an async function');
|
---|
61 | t.end();
|
---|
62 | });
|
---|
63 |
|
---|
64 | test('returns false for generator functions', function (t) {
|
---|
65 | if (generatorFuncs.length > 0) {
|
---|
66 | forEach(generatorFuncs, function (generatorFunc) {
|
---|
67 | t.notOk(isAsyncFunction(generatorFunc), generatorFunc + ' is not async function');
|
---|
68 | });
|
---|
69 | } else {
|
---|
70 | t.skip('generator function is not async function - this environment does not support ES6 generator functions. Please use an engine that supports them.');
|
---|
71 | }
|
---|
72 | t.end();
|
---|
73 | });
|
---|
74 |
|
---|
75 | test('returns false for non-async function with faked @@toStringTag', { skip: !hasToStringTag || asyncFuncs.length === 0 }, function (t) {
|
---|
76 | var asyncFunc = asyncFuncs[0];
|
---|
77 | var fakeAsyncFunction = {
|
---|
78 | toString: function () { return String(asyncFunc); },
|
---|
79 | valueOf: function () { return asyncFunc; }
|
---|
80 | };
|
---|
81 | fakeAsyncFunction[Symbol.toStringTag] = 'AsyncFunction';
|
---|
82 | t.notOk(isAsyncFunction(fakeAsyncFunction), 'fake AsyncFunction with @@toStringTag "AsyncFunction" is not an async function');
|
---|
83 | t.end();
|
---|
84 | });
|
---|
85 |
|
---|
86 | test('returns true for async functions', function (t) {
|
---|
87 | if (asyncFuncs.length > 0) {
|
---|
88 | forEach(asyncFuncs, function (asyncFunc) {
|
---|
89 | t.ok(isAsyncFunction(asyncFunc), asyncFunc + ' is async function');
|
---|
90 | });
|
---|
91 | } else {
|
---|
92 | t.skip('async function is async function - this environment does not support ES2018 async functions. Please use an engine that supports them.');
|
---|
93 | }
|
---|
94 | t.end();
|
---|
95 | });
|
---|