Skip to content

Commit a793312

Browse files
committed
add test and move to async/await
1 parent d117461 commit a793312

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

Diff for: packages/@vuepress/core/lib/node/Page.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,14 @@ module.exports = class Page {
244244
* @api private
245245
*/
246246

247-
enhance (enhancers) {
248-
return enhancers.reduce(async (accumulator, { pluginName, value }) => {
249-
return accumulator
250-
.then(() => value(this))
251-
.catch(error => {
252-
console.log(error)
253-
throw new Error(`[${pluginName}] execute extendPageData failed.`)
254-
})
255-
}, Promise.resolve())
247+
async enhance (enhancers) {
248+
for (const { name: pluginName, value: enhancer } of enhancers) {
249+
try {
250+
await enhancer(this)
251+
} catch (error) {
252+
console.log(error)
253+
throw new Error(`[${pluginName}] execute extendPageData failed.`)
254+
}
255+
}
256256
}
257257
}

Diff for: packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js

+49
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,54 @@ describe('Page', () => {
102102
expect(page._content.startsWith('---')).toBe(true)
103103
expect(page._strippedContent.startsWith('---')).toBe(false)
104104
})
105+
106+
describe('enhance - ', () => {
107+
let page
108+
let enhancers
109+
110+
beforeEach(() => {
111+
page = new Page({ path: '/' }, app)
112+
enhancers = [
113+
{
114+
pluginName: 'foo',
115+
value: jest.fn()
116+
},
117+
{
118+
pluginName: 'foo',
119+
value: jest.fn()
120+
}
121+
]
122+
global.console.log = jest.fn()
123+
})
124+
125+
test('should loop over sync enhancers', async () => {
126+
await page.enhance(enhancers)
127+
128+
return enhancers.map(enhancer => expect(enhancer.value).toBeCalled())
129+
})
130+
131+
test('should loop over sync and async enhancers', async () => {
132+
const mixedEnhancers = [...enhancers, {
133+
pluginName: 'blog',
134+
value: jest.fn().mockResolvedValue({})
135+
}]
136+
await page.enhance(mixedEnhancers)
137+
138+
return mixedEnhancers.map(enhancer => expect(enhancer.value).toBeCalled())
139+
})
140+
141+
test('should log when enhancing when failing', async () => {
142+
const error = { errorMessage: 'this is an error message' }
143+
expect.assertions(1)
144+
try {
145+
await page.enhance([{
146+
pluginName: 'error-plugin',
147+
value: jest.fn().mockRejectedValue(error)
148+
}])
149+
} catch (e) {
150+
expect(console.log).toBeCalledWith(error)
151+
}
152+
})
153+
})
105154
})
106155

0 commit comments

Comments
 (0)