Skip to content

Commit 13ea22f

Browse files
Slashgearkefranabg
authored andcommitted
add test and move to async/await
1 parent 27cf758 commit 13ea22f

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
@@ -282,14 +282,14 @@ module.exports = class Page {
282282
* @api private
283283
*/
284284

285-
enhance (enhancers) {
286-
return enhancers.reduce(async (accumulator, { pluginName, value }) => {
287-
return accumulator
288-
.then(() => value(this))
289-
.catch(error => {
290-
console.log(error)
291-
throw new Error(`[${pluginName}] execute extendPageData failed.`)
292-
})
293-
}, Promise.resolve())
285+
async enhance (enhancers) {
286+
for (const { name: pluginName, value: enhancer } of enhancers) {
287+
try {
288+
await enhancer(this)
289+
} catch (error) {
290+
console.log(error)
291+
throw new Error(`[${pluginName}] execute extendPageData failed.`)
292+
}
293+
}
294294
}
295295
}

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)