test(e2e): use better mocking strategy for commits example
This commit is contained in:
parent
f48a4f71a7
commit
c202bd6ac0
@ -1,4 +1,4 @@
|
|||||||
window.MOCKS = {
|
export default {
|
||||||
master: [
|
master: [
|
||||||
{
|
{
|
||||||
sha: 'd1527fbee422c7170e56845e55b49c4fd6de72a7',
|
sha: 'd1527fbee422c7170e56845e55b49c4fd6de72a7',
|
@ -1,5 +1,6 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { setupPuppeteer } from './e2eUtils'
|
import { setupPuppeteer } from './e2eUtils'
|
||||||
|
import mocks from './commits.mock'
|
||||||
|
|
||||||
describe('e2e: commits', () => {
|
describe('e2e: commits', () => {
|
||||||
const { page, click, count, text, isChecked } = setupPuppeteer()
|
const { page, click, count, text, isChecked } = setupPuppeteer()
|
||||||
@ -7,9 +8,25 @@ describe('e2e: commits', () => {
|
|||||||
async function testCommits(apiType: 'classic' | 'composition') {
|
async function testCommits(apiType: 'classic' | 'composition') {
|
||||||
const baseUrl = `file://${path.resolve(
|
const baseUrl = `file://${path.resolve(
|
||||||
__dirname,
|
__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().goto(baseUrl)
|
||||||
await page().waitFor('li')
|
await page().waitFor('li')
|
||||||
expect(await count('input')).toBe(2)
|
expect(await count('input')).toBe(2)
|
||||||
|
@ -11,6 +11,12 @@ export function setupPuppeteer() {
|
|||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
browser = await puppeteer.launch(puppeteerOptions)
|
browser = await puppeteer.launch(puppeteerOptions)
|
||||||
page = await browser.newPage()
|
page = await browser.newPage()
|
||||||
|
|
||||||
|
page.on('console', e => {
|
||||||
|
if (e.type() === 'error') {
|
||||||
|
console.error(`Error from Puppeteer-loaded page:`, e)
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
<script src="../../dist/vue.global.js"></script>
|
<script src="../../dist/vue.global.js"></script>
|
||||||
<script src="../mocks/commits.js"></script>
|
|
||||||
|
|
||||||
<div id="demo">
|
<div id="demo">
|
||||||
<h1>Latest Vue.js Commits</h1>
|
<h1>Latest Vue.js Commits</h1>
|
||||||
@ -42,18 +41,11 @@ const App = {
|
|||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
fetchData() {
|
fetchData() {
|
||||||
if (window.location.hash === '#test') {
|
|
||||||
// use mocks in e2e to avoid dependency on network / authentication
|
|
||||||
setTimeout(() => {
|
|
||||||
this.commits = window.MOCKS[this.currentBranch]
|
|
||||||
}, 0)
|
|
||||||
} else {
|
|
||||||
fetch(`${API_URL}${this.currentBranch}`)
|
fetch(`${API_URL}${this.currentBranch}`)
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
this.commits = data
|
this.commits = data
|
||||||
})
|
})
|
||||||
}
|
|
||||||
},
|
},
|
||||||
truncate(v) {
|
truncate(v) {
|
||||||
const newline = v.indexOf('\n')
|
const newline = v.indexOf('\n')
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
<script src="../../dist/vue.global.js"></script>
|
<script src="../../dist/vue.global.js"></script>
|
||||||
<script src="../mocks/commits.js"></script>
|
|
||||||
|
|
||||||
<div id="demo">
|
<div id="demo">
|
||||||
<h1>Latest Vue.js Commits</h1>
|
<h1>Latest Vue.js Commits</h1>
|
||||||
@ -38,17 +37,13 @@ const App = {
|
|||||||
const currentBranch = ref('master')
|
const currentBranch = ref('master')
|
||||||
const commits = ref(null)
|
const commits = ref(null)
|
||||||
|
|
||||||
watch([currentBranch, commits], () => {
|
watch(() => {
|
||||||
if (window.location.hash === '#test') {
|
|
||||||
// use mocks in e2e to avoid dependency on network / authentication
|
|
||||||
setTimeout(() => {
|
|
||||||
commits.value = window.MOCKS[currentBranch.value]
|
|
||||||
}, 0)
|
|
||||||
} else {
|
|
||||||
fetch(`${API_URL}${currentBranch.value}`)
|
fetch(`${API_URL}${currentBranch.value}`)
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(data => { commits.value = data })
|
.then(data => {
|
||||||
}
|
console.log(data)
|
||||||
|
commits.value = data
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user