2022-03-13 20:10:25 +08:00
|
|
|
import path from 'path'
|
|
|
|
import { createApp } from 'vue'
|
|
|
|
import { renderToString } from '@vue/server-renderer'
|
2022-05-02 15:15:22 +08:00
|
|
|
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
|
2022-03-13 20:10:25 +08:00
|
|
|
import puppeteer from 'puppeteer'
|
|
|
|
import glob from 'fast-glob'
|
2022-03-15 19:21:54 +08:00
|
|
|
import ElementPlus, { ID_INJECTION_KEY } from '../dist/element-plus'
|
2022-03-13 20:10:25 +08:00
|
|
|
|
|
|
|
import type { Browser } from 'puppeteer'
|
|
|
|
|
|
|
|
const projectRoot = process.cwd()
|
2022-03-18 16:01:41 +08:00
|
|
|
const testRoot = path.resolve(projectRoot, 'ssr-testing')
|
|
|
|
const demoRoot = path.resolve(testRoot, 'cases')
|
2022-03-13 20:10:25 +08:00
|
|
|
describe('Cypress Button', () => {
|
|
|
|
let browser: Browser
|
|
|
|
beforeAll(async () => {
|
|
|
|
browser = await puppeteer.launch()
|
|
|
|
})
|
|
|
|
|
2022-05-02 15:15:22 +08:00
|
|
|
afterAll(() => {
|
|
|
|
browser.close()
|
|
|
|
})
|
|
|
|
|
2022-03-13 20:10:25 +08:00
|
|
|
describe('when initialized', () => {
|
2022-03-15 19:21:54 +08:00
|
|
|
const demoPaths = glob
|
2022-03-15 20:18:06 +08:00
|
|
|
.sync(`${demoRoot}/*.vue`)
|
2022-03-15 19:21:54 +08:00
|
|
|
.map((demo) => demo.slice(demoRoot.length + 1))
|
|
|
|
|
|
|
|
it.each(demoPaths)(`render %s correctly`, async (demoPath) => {
|
2022-03-13 20:10:25 +08:00
|
|
|
const page = await browser.newPage()
|
|
|
|
await page.goto(`file://${projectRoot}/ssr-testing/index.html`)
|
|
|
|
await page.addStyleTag({
|
|
|
|
path: path.join(
|
|
|
|
projectRoot,
|
|
|
|
'dist',
|
|
|
|
'element-plus',
|
|
|
|
'dist',
|
|
|
|
'index.css'
|
|
|
|
),
|
|
|
|
})
|
2022-03-18 15:52:39 +08:00
|
|
|
|
2022-03-15 19:21:54 +08:00
|
|
|
const { default: Demo } = await import(path.join(demoRoot, demoPath))
|
|
|
|
const app = createApp(<Demo />)
|
|
|
|
.use(ElementPlus)
|
|
|
|
.provide(ID_INJECTION_KEY, {
|
|
|
|
prefix: 100,
|
|
|
|
current: 0,
|
|
|
|
})
|
2022-03-18 15:52:39 +08:00
|
|
|
|
2022-03-13 20:10:25 +08:00
|
|
|
const html = await renderToString(app)
|
|
|
|
|
|
|
|
await page.evaluate((innerHTML) => {
|
|
|
|
document.querySelector('#root')!.innerHTML = innerHTML
|
|
|
|
}, html)
|
|
|
|
|
2022-03-18 15:52:39 +08:00
|
|
|
// SSR testing don't need screenshots.
|
|
|
|
// const screenshotPath = demoPath
|
|
|
|
// .split('/')
|
|
|
|
// .join('-')
|
|
|
|
// .replace(/\.vue$/, '.png')
|
|
|
|
// await page.screenshot({
|
|
|
|
// path: path.join(testRoot, 'screenshots', screenshotPath),
|
|
|
|
// fullPage: true,
|
|
|
|
// })
|
2022-05-02 15:15:22 +08:00
|
|
|
|
2022-03-13 20:10:25 +08:00
|
|
|
await page.close()
|
|
|
|
expect(true).toBe(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|