forked from vuejs/vuepress
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
476 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/@vuepress/shared-utils/__tests__/fixtures/plugin-a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
name: 'a' | ||
} |
1 change: 1 addition & 0 deletions
1
packages/@vuepress/shared-utils/__tests__/fixtures/theme-a/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {} |
162 changes: 162 additions & 0 deletions
162
packages/@vuepress/shared-utils/__tests__/moduleResolver.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
jest.mock('vuepress-plugin-a') | ||
jest.mock('@org/vuepress-plugin-a') | ||
jest.mock('@vuepress/plugin-a') | ||
|
||
jest.mock('vuepress-theme-a') | ||
jest.mock('@org/vuepress-theme-a') | ||
jest.mock('@vuepress/theme-a') | ||
|
||
import path from 'path' | ||
import { | ||
getThemeResolver, | ||
getPluginResolver, | ||
resolveScopePackage | ||
} from '../lib/moduleResolver' | ||
import hash from 'hash-sum' | ||
|
||
const MOCK_RELATIVE = '../../../../__mocks__' | ||
|
||
function loadMockModule (name) { | ||
return require(`${MOCK_RELATIVE}/${name}`) | ||
} | ||
|
||
function resolveMockModule (name) { | ||
return path.resolve(__dirname, `${MOCK_RELATIVE}/${name}`) | ||
} | ||
|
||
const fixturesDir = path.resolve(__dirname, 'fixtures') | ||
const themeResolver = getThemeResolver(fixturesDir) | ||
const pluginResolver = getPluginResolver(fixturesDir) | ||
|
||
const prevCwd = process.cwd() | ||
beforeAll(() => { | ||
process.chdir(path.resolve(__dirname, 'fixtures')) | ||
}) | ||
|
||
afterAll(() => { | ||
process.chdir(prevCwd) | ||
}) | ||
|
||
describe('resolveScopePackage', () => { | ||
test('corrent format', () => { | ||
const pkg = resolveScopePackage('@vuepress/plugin-a') | ||
expect(pkg.org).toBe('vuepress') | ||
expect(pkg.name).toBe('plugin-a') | ||
}) | ||
|
||
test('incorrect format', () => { | ||
const pkg2 = resolveScopePackage('vuepress/plugin-a') | ||
expect(pkg2).toBe(null) | ||
|
||
const pkg3 = resolveScopePackage('vuepress-plugin-a') | ||
expect(pkg3).toBe(null) | ||
}) | ||
}) | ||
|
||
const getBaseAsserts = (type) => [ | ||
{ input: 'a', output: ['a', `vuepress-${type}-a`] }, | ||
{ input: `vuepress-${type}-a`, output: ['a', `vuepress-${type}-a`] }, | ||
{ input: '@vuepress/a', output: ['@vuepress/a', `@vuepress/${type}-a`] }, | ||
{ input: '@org/a', output: ['@org/a', `@org/vuepress-${type}-a`] } | ||
] | ||
|
||
describe('normalizeRequest', () => { | ||
const normalizeRequest = pluginResolver.normalizeRequest.bind(pluginResolver) | ||
|
||
test('base', () => { | ||
const asserts = [ | ||
{ input: null, output: [null, null] }, | ||
{ input: undefined, output: [null, null] }, | ||
...getBaseAsserts('plugin') | ||
] | ||
|
||
for (const { input, output } of asserts) { | ||
const { shortcut, name } = normalizeRequest(input) | ||
expect([shortcut, name]).toEqual(output) | ||
} | ||
}) | ||
|
||
test('object', () => { | ||
const req = {} | ||
const { name, shortcut } = normalizeRequest(req) | ||
expect(shortcut).toBe(`anonymous-${hash(req)}`) | ||
expect(name).toBe(`vuepress-plugin-anonymous-${hash(req)}`) | ||
}) | ||
|
||
test('object - should respect name', () => { | ||
const req = { name: 'a' } | ||
const { name, shortcut } = normalizeRequest(req) | ||
expect(shortcut).toBe('a') | ||
expect(name).toBe(`vuepress-plugin-a`) | ||
}) | ||
}) | ||
|
||
describe('resolvePlugin', () => { | ||
const resolvePlugin = pluginResolver.resolve.bind(pluginResolver) | ||
|
||
test('function', () => { | ||
const plugin = () => { | ||
/* noop */ | ||
} | ||
const resolved = resolvePlugin(plugin) | ||
expect(resolved.entry).toBe(plugin) | ||
expect(resolved.fromDep).toBe(false) | ||
}) | ||
|
||
test('object', () => { | ||
const plugin = {} | ||
const resolved = resolvePlugin(plugin) | ||
expect(resolved.entry).toBe(plugin) | ||
expect(resolved.fromDep).toBe(false) | ||
}) | ||
|
||
test('from dep', () => { | ||
const asserts = getBaseAsserts('plugin') | ||
for (const { input, output } of asserts) { | ||
const [, name] = output | ||
const resolved = resolvePlugin(input) | ||
expect(resolved.entry).toBe(loadMockModule(name)) | ||
} | ||
}) | ||
|
||
test('relative path', () => { | ||
const resolved = resolvePlugin('./plugin-a') | ||
expect(resolved.entry).toBe(require('./fixtures/plugin-a')) | ||
}) | ||
|
||
test('aosolute path', () => { | ||
const resolved = resolvePlugin(path.resolve(__dirname, 'fixtures/plugin-a')) | ||
expect(resolved.entry).toBe(require('./fixtures/plugin-a')) | ||
}) | ||
|
||
test('plugin that cannot be resolved', () => { | ||
expect(resolvePlugin('c').entry).toBe(null) | ||
}) | ||
}) | ||
|
||
describe('resolveTheme', () => { | ||
const resolveTheme = themeResolver.resolve.bind(themeResolver) | ||
|
||
test('from dep', () => { | ||
const asserts = getBaseAsserts('theme') | ||
for (const { input, output } of asserts) { | ||
const [, name] = output | ||
const resolved = resolveTheme(input) | ||
expect(resolved.entry).toBe(resolveMockModule(name)) | ||
} | ||
}) | ||
|
||
test('relative path', () => { | ||
const resolved = resolveTheme('./theme-a') | ||
expect(resolved.entry).toBe(path.resolve(__dirname, './fixtures/theme-a')) | ||
}) | ||
|
||
test('absolute path', () => { | ||
const resolved = resolveTheme(path.resolve(__dirname, 'fixtures/theme-a')) | ||
expect(resolved.entry).toBe(path.resolve(__dirname, './fixtures/theme-a')) | ||
}) | ||
|
||
test('theme that cannot be resolved', () => { | ||
expect(resolveTheme('c').entry).toBe(null) | ||
}) | ||
}) |
129 changes: 0 additions & 129 deletions
129
packages/@vuepress/shared-utils/__tests__/shortcutPackageResolver.spec.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.