Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 15, 2020
1 parent 77e648e commit 1f289f5
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@
],
}],
},

"overrides": [
{
"files": "**/*.mjs",
"rules": {
"no-restricted-exports": 0,
},
},
],
}
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
"prepublish": "safe-publish-latest",
"pretest": "npm run lint",
"lint": "eslint --ext=js,mjs .",
"test": "echo \"Error: no test specified\" && exit 1"
"tests-only": "tape 'test/**/*.js'",
"tests-esm": "node test/index.mjs",
"test": "npm run tests-only && npm run tests-esm",
"posttest": "aud --production"
},
"repository": {
"type": "git",
Expand All @@ -70,8 +73,12 @@
"homepage": "https://github.com/es-shims/Array.prototype.item#readme",
"devDependencies": {
"@ljharb/eslint-config": "^17.1.0",
"aud": "^1.1.2",
"eslint": "^7.7.0",
"safe-publish-latest": "^1.1.4"
"functions-have-names": "^1.2.1",
"has-strict-mode": "^1.0.0",
"safe-publish-latest": "^1.1.4",
"tape": "^5.0.1"
},
"dependencies": {
"define-properties": "^1.1.3",
Expand Down
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var item = require('..');
var test = require('tape');
var runTests = require('./tests');

test('as a function', function (t) {
t.test('bad array/this value', function (st) {
st['throws'](item.bind(null, undefined, function () {}), TypeError, 'undefined is not an object');
st['throws'](item.bind(null, null, function () {}), TypeError, 'null is not an object');
st.end();
});

runTests(item, t);

t.end();
});
31 changes: 31 additions & 0 deletions test/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import item from 'array.prototype.item';
import * as itemModule from 'array.prototype.item';
import test from 'tape';
import runTests from './tests.js';

test('as a function', (t) => {
t.test('bad array/this value', (st) => {
st.throws(() => item(undefined), TypeError, 'undefined is not an object');
st.throws(() => item(null), TypeError, 'null is not an object');
st.end();
});

runTests(item, t);

t.end();
});

test('named exports', async (t) => {
t.deepEqual(
Object.keys(itemModule).sort(),
['default', 'shim', 'getPolyfill', 'implementation'].sort(),
'has expected named exports'
);

const { shim, getPolyfill, implementation } = itemModule;
t.equal(await import('array.prototype.item/shim'), shim, 'shim named export matches deep export');
t.equal(await import('array.prototype.item/implementation'), implementation, 'implementation named export matches deep export');
t.equal(await import('array.prototype.item/polyfill'), getPolyfill, 'getPolyfill named export matches deep export');

t.end();
});
35 changes: 35 additions & 0 deletions test/shimmed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

require('../shim')();

var test = require('tape');
var defineProperties = require('define-properties');
var bind = require('function-bind');
var isEnumerable = Object.prototype.propertyIsEnumerable;
var functionsHaveNames = require('functions-have-names')();
var hasStrictMode = require('has-strict-mode')();

var runTests = require('./tests');

test('shimmed', function (t) {
t.equal(Array.prototype.item.length, 1, 'Array#item has a length of 1');
t.test('Function name', { skip: !functionsHaveNames }, function (st) {
st.equal(Array.prototype.item.name, 'item', 'Array#item has name "item"');
st.end();
});

t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (et) {
et.equal(false, isEnumerable.call(Array.prototype, 'item'), 'Array#item is not enumerable');
et.end();
});

t.test('bad array/this value', { skip: !hasStrictMode }, function (st) {
st['throws'](function () { return Array.prototype.item.call(undefined, 'a'); }, TypeError, 'undefined is not an object');
st['throws'](function () { return Array.prototype.item.call(null, 'a'); }, TypeError, 'null is not an object');
st.end();
});

runTests(bind.call(Function.call, Array.prototype.item), t);

t.end();
});
36 changes: 36 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

module.exports = function (item, t) {
t.test('item', function (st) {
var arr = [1, [2], [3, 4]];

st.equal(item(arr, 0), arr[0]);
st.equal(item(arr, -3), arr[0]);

st.deepEqual(item(arr, 1), arr[1]);
st.deepEqual(item(arr, -2), arr[1]);

st.deepEqual(item(arr, 2), arr[2]);
st.deepEqual(item(arr, -1), arr[2]);

st.equal(item(arr, 3), undefined);
st.equal(item(arr, -4), undefined);

st.equal(item([], 0), undefined);
st.equal(item([], -1), undefined);

st.end();
});

t.test('sparse arrays', function (st) {
// eslint-disable-next-line no-sparse-arrays
var arr = [, [1]];
st.equal(item(arr, 0), undefined);
st.equal(item(arr, -2), undefined);

st.deepEqual(item(arr, 1), [1]);
st.deepEqual(item(arr, -1), [1]);

st.end();
});
};

0 comments on commit 1f289f5

Please sign in to comment.