Skip to content

Commit

Permalink
chore: refactor fund tests to test/lib
Browse files Browse the repository at this point in the history
Refactored `npm fund` tests to use new `test/lib/` unit tests structure.

ref: npm/statusboard#151

PR-URL: npm#1582
Credit: @ruyadorno
Close: npm#1582
Reviewed-by: @isaacs
  • Loading branch information
ruyadorno authored and isaacs committed Aug 4, 2020
1 parent 5473bbd commit 68a16d6
Show file tree
Hide file tree
Showing 4 changed files with 758 additions and 591 deletions.
21 changes: 3 additions & 18 deletions lib/fund.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
} = require('libnpmfund')

const npm = require('./npm.js')
const completion = require('./utils/completion/installed-deep.js')
const output = require('./utils/output.js')
const openUrl = require('./utils/open-url.js')
const usageUtil = require('./utils/usage.js')
Expand All @@ -23,16 +24,6 @@ const usage = usageUtil(
'npm fund [--json] [--browser] [--unicode] [[<@scope>/]<pkg> [--which=<fundingSourceNumber>]'
)

const completion = (opts, cb) => {
const argv = opts.conf.argv.remain
switch (argv[2]) {
case 'fund':
return cb(null, [])
default:
return cb(new Error(argv[2] + ' not recognized'))
}
}

const cmd = (args, cb) => fund(args).then(() => cb()).catch(cb)

function printJSON (fundingInfo) {
Expand Down Expand Up @@ -126,14 +117,8 @@ async function openFundingUrl ({ path, tree, spec, fundingSourceNumber }) {
}
}

let { funding } = retrievePackageMetadata() || {}

if (!funding) {
// if still has not funding info, let's try
// fetching metadata from the registry then
const manifest = await pacote.manifest(arg, npm.flatOptions)
funding = manifest.funding
}
const { funding } = retrievePackageMetadata() ||
await pacote.manifest(arg, npm.flatOptions).catch(() => ({}))

const validSources = []
.concat(normalizeFunding(funding))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/tap/fund.js TAP fund containing multi-level nested deps with no funding > should omit dependencies with no funding declared 1`] = `
exports[`test/lib/fund.js TAP fund a package with type and multiple sources > should print prompt select message 1`] = `
1: Foo funding available at the following URL: http://example.com/foo
2: Lorem funding available at the following URL: http://example.com/foo-lorem
Run \`npm fund [<@scope>/]<pkg> --which=1\`, for example, to open the first funding URL listed in that package
`

exports[`test/lib/fund.js TAP fund containing multi-level nested deps with no funding > should omit dependencies with no funding declared 1`] = `
[email protected]
+-- https://example.com/lorem
| \`-- [email protected]
Expand All @@ -15,49 +22,48 @@ [email protected]
`

exports[`test/tap/fund.js TAP fund in which same maintainer owns all its deps > should print stack packages together 1`] = `
exports[`test/lib/fund.js TAP fund in which same maintainer owns all its deps > should print stack packages together 1`] = `
http://example.com/donate
\`-- [email protected], [email protected], [email protected], [email protected]
`

exports[`test/tap/fund.js TAP fund using nested packages with multiple sources > should prompt with all available URLs 1`] = `
exports[`test/lib/fund.js TAP fund pkg missing version number > should print name only 1`] = `
http://example.com/foo
\`-- foo
`

exports[`test/lib/fund.js TAP fund using nested packages with multiple sources > should prompt with all available URLs 1`] = `
1: Funding available at the following URL: https://one.example.com
2: Funding available at the following URL: https://two.example.com
Run \`npm fund [<@scope>/]<pkg> --which=1\`, for example, to open the first funding URL listed in that package
`

exports[`test/tap/fund.js TAP fund using nested packages with multiple sources, with a source number > should open the numbered URL 1`] = `
exports[`test/lib/fund.js TAP fund using nested packages with multiple sources, with a source number > should open the numbered URL 1`] = `
Funding available at the following URL:
https://one.example.com
https://one.example.com
`

exports[`test/tap/fund.js TAP fund using package argument with no browser > should open funding url 1`] = `
exports[`test/lib/fund.js TAP fund using package argument > should open funding url 1`] = `
individual funding available at the following URL:
http://example.com/donate
http://example.com/donate
`

exports[`test/tap/fund.js TAP fund using pkg name while having conflicting versions > should open greatest version 1`] = `
exports[`test/lib/fund.js TAP fund using pkg name while having conflicting versions > should open greatest version 1`] = `
Funding available at the following URL:
http://example.com/2
http://example.com/2
`

exports[`test/tap/fund.js TAP fund using string shorthand > should open string-only url 1`] = `
exports[`test/lib/fund.js TAP fund using string shorthand > should open string-only url 1`] = `
Funding available at the following URL:
https://example.com/sponsor
https://example.com/sponsor
`

exports[`test/tap/fund.js TAP fund with no package containing funding > should print empty funding info 1`] = `
exports[`test/lib/fund.js TAP fund with no package containing funding > should print empty funding info 1`] = `
[email protected]
Expand Down
Loading

0 comments on commit 68a16d6

Please sign in to comment.