test: add file list render test cases

This commit is contained in:
EnixCoda 2020-10-28 16:25:45 +08:00
parent 453661af6e
commit de5b67bb2f
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
4 changed files with 55 additions and 4 deletions

View file

@ -4,6 +4,6 @@ describe(`in GitHub homepage`, () => {
beforeAll(() => page.goto('https://github.com'))
it('should not render Gitako', async () => {
expectToNotFind('.gitako-side-bar .gitako-position-wrapper')
await expectToNotFind('.gitako-side-bar .gitako-position-wrapper')
})
})

View file

@ -1,9 +1,27 @@
import { expectToFind } from '../utils'
import { expectToFind, expectToNotFind, scroll } from '../utils'
describe(`in Gitako project page`, () => {
beforeAll(() => page.goto('https://github.com/EnixCoda/Gitako'))
it('should render Gitako', async () => {
expectToFind('.gitako-side-bar .gitako-position-wrapper')
await expectToFind('.gitako-side-bar .gitako-position-wrapper')
})
it('should render file list', async () => {
await expectToFind('.gitako-side-bar .files .node-item')
})
it('should render while scroll', async () => {
const filesEle = await page.waitForSelector('.gitako-side-bar .files')
// node of tsconfig.json should NOT be rendered before scroll down
await expectToNotFind('.gitako-side-bar .files a[title="tsconfig.json"]')
const box = await filesEle.boundingBox()
if (box) {
await page.mouse.move(box.x + 40, box.y + 40)
await scroll({ totalDistance: 100, duration: 5000 })
// node of tsconfig.json should be rendered now
await expectToFind('.gitako-side-bar .files a[title="tsconfig.json"]')
}
})
})

View file

@ -0,0 +1,13 @@
import { expectToFind } from '../utils'
describe(`in Gitako project page`, () => {
beforeAll(() => page.goto('https://github.com/EnixCoda/Gitako/pull/71'))
it('should render Gitako', async () => {
await expectToFind('.gitako-side-bar .gitako-position-wrapper')
})
it('should render file list', async () => {
await expectToFind('.gitako-side-bar .files .node-item')
})
})

View file

@ -1,7 +1,27 @@
export async function expectToFind(selector: string) {
await expect(await page.waitForSelector(selector)).not.toBeNull()
await expect(page.waitForSelector(selector)).resolves.not.toBeNull()
}
export async function expectToNotFind(selector: string) {
await expect(page.waitForSelector(selector, { timeout: 1000 })).rejects.toThrow()
}
export function sleep(timeout: number) {
return new Promise(resolve => setTimeout(resolve, timeout))
}
export async function scroll({
totalDistance,
step = 1,
duration = 500,
}: {
totalDistance: number
step?: number
duration?: number
}) {
let distance = 0
while ((distance += step) < totalDistance) {
await (page.mouse as any).wheel({ deltaY: step })
await sleep((duration * step) / totalDistance)
}
}