test(e2e): use better mocking strategy for commits example

This commit is contained in:
Evan You
2019-12-11 10:15:52 -05:00
parent f48a4f71a7
commit c202bd6ac0
5 changed files with 37 additions and 27 deletions

View File

@@ -1,5 +1,6 @@
import path from 'path'
import { setupPuppeteer } from './e2eUtils'
import mocks from './commits.mock'
describe('e2e: commits', () => {
const { page, click, count, text, isChecked } = setupPuppeteer()
@@ -7,9 +8,25 @@ describe('e2e: commits', () => {
async function testCommits(apiType: 'classic' | 'composition') {
const baseUrl = `file://${path.resolve(
__dirname,
`../${apiType}/commits.html#test`
`../${apiType}/commits.html`
)}`
// intercept and mock the response to avoid hitting the actual API
await page().setRequestInterception(true)
page().on('request', req => {
const match = req.url().match(/&sha=(.*)$/)
if (!match) {
req.continue()
} else {
req.respond({
status: 200,
contentType: 'application/json',
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify(mocks[match[1] as 'master' | 'sync'])
})
}
})
await page().goto(baseUrl)
await page().waitFor('li')
expect(await count('input')).toBe(2)