|
| 1 | +require('dotenv').config() |
| 2 | +const assert = require('assert') |
| 3 | +const { suite, test } = require('mocha') |
| 4 | +const getDependents = require('../lib/getDependents') |
| 5 | +const pkg = require('../package.json') |
| 6 | +const config = require('./fixtures/config') |
| 7 | + |
| 8 | +assert(process.env.GITHUB_TOKEN, ` |
| 9 | +Tests require you to have a GitHub personal token called GITHUB_TOKEN |
| 10 | +For more information about GitHub tokens |
| 11 | +https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line |
| 12 | +`) |
| 13 | + |
| 14 | +suite(pkg.name, async function () { |
| 15 | + this.timeout(0) |
| 16 | + test('Dependents object created', async function () { |
| 17 | + const deps = await getDependents(config.args) |
| 18 | + for (const i in deps) { |
| 19 | + for (const key in deps[i]) { |
| 20 | + const subObj = deps[i][key] |
| 21 | + assert.strictEqual(Object.prototype.hasOwnProperty.call(subObj, 'downloads'), true) |
| 22 | + assert.strictEqual(Object.prototype.hasOwnProperty.call(subObj, 'forks'), true) |
| 23 | + assert.strictEqual(Object.prototype.hasOwnProperty.call(subObj, 'stars'), true) |
| 24 | + assert.strictEqual(Object.prototype.hasOwnProperty.call(subObj, 'watchers'), true) |
| 25 | + assert.strictEqual(Object.prototype.hasOwnProperty.call(subObj, 'url'), true) |
| 26 | + } |
| 27 | + } |
| 28 | + }) |
| 29 | + test('GitHub attributes zero when URL is undefined', async function () { |
| 30 | + const deps = await getDependents(config.args) |
| 31 | + for (const i in deps) { |
| 32 | + for (const key in deps[i]) { |
| 33 | + const subObj = deps[i][key] |
| 34 | + if (subObj.url === 'undefined') { |
| 35 | + assert.strictEqual(subObj.forks, 0) |
| 36 | + assert.strictEqual(subObj.stars, 0) |
| 37 | + assert.strictEqual(subObj.watchers, 0) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + }) |
| 42 | + test('Dependents sorted correctly', async function () { |
| 43 | + const deps = await getDependents(config.args) |
| 44 | + let key, previousKey |
| 45 | + for (const i in deps) { |
| 46 | + for (const k in deps[i]) { |
| 47 | + previousKey = key |
| 48 | + key = k |
| 49 | + } |
| 50 | + if (i !== '0') { |
| 51 | + const j = String(parseInt(i) - 1) |
| 52 | + const previous = deps[j][previousKey][config.args.sort] |
| 53 | + const current = deps[i][key][config.args.sort] |
| 54 | + assert.strictEqual(previous >= current, true) |
| 55 | + } |
| 56 | + } |
| 57 | + }) |
| 58 | +}) |
0 commit comments