1const t = require('tap') 2const mockNpm = require('../../fixtures/mock-npm.js') 3const { sep } = require('path') 4 5const fixtures = { 6 pkg: { 7 'package.json': JSON.stringify({ 8 name: 'thispkg', 9 version: '1.2.3', 10 homepage: 'https://example.com', 11 }), 12 nodocs: { 13 'package.json': JSON.stringify({ 14 name: 'nodocs', 15 version: '1.2.3', 16 }), 17 }, 18 docsurl: { 19 'package.json': JSON.stringify({ 20 name: 'docsurl', 21 version: '1.2.3', 22 homepage: 'https://bugzilla.localhost/docsurl', 23 }), 24 }, 25 repourl: { 26 'package.json': JSON.stringify({ 27 name: 'repourl', 28 version: '1.2.3', 29 repository: 'https://github.com/foo/repourl', 30 }), 31 }, 32 repoobj: { 33 'package.json': JSON.stringify({ 34 name: 'repoobj', 35 version: '1.2.3', 36 repository: { url: 'https://github.com/foo/repoobj' }, 37 }), 38 }, 39 repourlobj: { 40 'package.json': JSON.stringify({ 41 name: 'repourlobj', 42 version: '1.2.3', 43 repository: { url: { works: false } }, 44 }), 45 }, 46 }, 47 workspaces: { 48 'package.json': JSON.stringify({ 49 name: 'workspaces-test', 50 version: '1.2.3-test', 51 workspaces: ['workspace-a', 'workspace-b', 'workspace-c'], 52 }), 53 'workspace-a': { 54 'package.json': JSON.stringify({ 55 name: 'workspace-a', 56 version: '1.2.3-a', 57 homepage: 'http://docs.workspace-a/', 58 }), 59 }, 60 'workspace-b': { 61 'package.json': JSON.stringify({ 62 name: 'workspace-b', 63 version: '1.2.3-n', 64 repository: 'https://github.com/npm/workspace-b', 65 }), 66 }, 67 'workspace-c': JSON.stringify({ 68 'package.json': { 69 name: 'workspace-n', 70 version: '1.2.3-n', 71 }, 72 }), 73 }, 74} 75 76const setup = async (t, { prefixDir = fixtures.pkg, config } = {}) => { 77 // keep a tally of which urls got opened 78 const opened = {} 79 const openUrl = async (_, url) => { 80 opened[url] = opened[url] || 0 81 opened[url]++ 82 } 83 84 const res = await mockNpm(t, { 85 prefixDir, 86 mocks: { 87 '{LIB}/utils/open-url.js': openUrl, 88 }, 89 config, 90 }) 91 92 return { 93 ...res, 94 opened, 95 } 96} 97 98t.test('open docs urls', async t => { 99 const expect = { 100 nodocs: 'https://www.npmjs.com/package/nodocs', 101 docsurl: 'https://bugzilla.localhost/docsurl', 102 repourl: 'https://github.com/foo/repourl#readme', 103 repoobj: 'https://github.com/foo/repoobj#readme', 104 repourlobj: 'https://www.npmjs.com/package/repourlobj', 105 '.': 'https://example.com', 106 } 107 108 for (const [key, url] of Object.entries(expect)) { 109 await t.test(`open ${key} url`, async t => { 110 const { npm, opened } = await setup(t) 111 await npm.exec('docs', [['.', key].join(sep)]) 112 t.strictSame({ [url]: 1 }, opened, `opened ${url}`) 113 }) 114 } 115}) 116 117t.test('open default package if none specified', async t => { 118 const { npm, opened } = await setup(t) 119 120 await npm.exec('docs', []) 121 t.strictSame({ 'https://example.com': 1 }, opened, 'opened expected url') 122}) 123 124t.test('workspaces', async (t) => { 125 await t.test('all workspaces', async t => { 126 const { npm, opened } = await setup(t, { 127 prefixDir: fixtures.workspaces, 128 config: { workspaces: true }, 129 }) 130 await npm.exec('docs', []) 131 t.strictSame({ 132 'http://docs.workspace-a/': 1, 133 'https://github.com/npm/workspace-b#readme': 1, 134 }, opened, 'opened two valid docs urls') 135 }) 136 137 await t.test('one workspace', async t => { 138 const { npm, opened } = await setup(t, { 139 prefixDir: fixtures.workspaces, 140 config: { workspace: 'workspace-a' }, 141 }) 142 await npm.exec('docs', []) 143 t.strictSame({ 144 'http://docs.workspace-a/': 1, 145 }, opened, 'opened one requested docs urls') 146 }) 147 148 await t.test('invalid workspace', async t => { 149 const { npm, opened } = await setup(t, { 150 prefixDir: fixtures.workspaces, 151 config: { workspace: 'workspace-x' }, 152 }) 153 await t.rejects( 154 npm.exec('docs', []), 155 /No workspaces found/ 156 ) 157 await t.rejects( 158 npm.exec('docs', []), 159 /workspace-x/ 160 ) 161 t.match({}, opened, 'opened no docs urls') 162 }) 163}) 164