Browse Source

Refactored `build brick and break` test.

pull/603/head
Marie Malarme 4 years ago committed by Clément
parent
commit
cb6bbda2f5
  1. 105
      puppeteer/build-brick-and-break_test.js

105
puppeteer/build-brick-and-break_test.js

@ -1,69 +1,22 @@
export const tests = [] export const tests = []
export const setup = async ({ page }) => { export const setup = async ({ page }) => ({
const body = await page.$('body')
const hammer = await page.$('#hammer')
const dynamite = await page.$('#dynamite')
return {
body,
hammer,
dynamite,
getDivsLength: async () =>
await page.$$eval('div', (nodes) => nodes.length),
getBricksIds: async () => getBricksIds: async () =>
await page.$$eval('div', (nodes) => await page.$$eval('div', (nodes) =>
nodes.filter((node) => node.id.includes('brick')).map((n) => n.id), nodes.filter((node) => node.id.includes('brick')).map((n) => n.id),
), ),
getMiddleBricksIds: async () => })
await page.$$eval('div', (nodes) =>
nodes
.filter(
(node) => node.id.includes('brick') && node.dataset.foundation,
)
.map((n) => n.id),
),
getExpectedRepairedIds: async () =>
await page.$eval('body', (body) => {
const getIdInt = (str) => str.replace('brick-', '')
return body.dataset.reparations
.split(',')
.sort((a, b) => getIdInt(b) - getIdInt(a))
.map((id) => {
const isMiddleBrick = getIdInt(id) % 3 === 2
const status = isMiddleBrick ? 'in progress' : 'repaired'
return `${id}_${status}`
})
}),
getRepairedIds: async () =>
await page.$$eval('div', (nodes) => {
const getIdInt = (str) => str.replace('brick-', '')
return nodes
.filter(
(node) =>
node.dataset.repaired === 'true' ||
node.dataset.repaired === 'in progress',
)
.sort((a, b) => getIdInt(b.id) - getIdInt(a.id))
.map(({ id }) => {
const isMiddleBrick = getIdInt(id) % 3 === 2
const status = isMiddleBrick ? 'in progress' : 'repaired'
return `${id}_${status}`
})
}),
}
}
const between = (expected, min, max) => expected >= min && expected <= max const between = (expected, min, max) => expected >= min && expected <= max
tests.push(async ({ page, eq, getDivsLength }) => { tests.push(async ({ page, eq }) => {
// check that the brick divs are built at a regular interval of 100ms // check that the brick divs are built at a regular interval of 100ms
// the average of the divs built every 100ms must be close to 10 // the average of the divs built every 100ms must be close to 10
let repeat = 0 let repeat = 0
let buildSteps = [] let buildSteps = []
while (repeat < 3) { while (repeat < 3) {
const divs = await getDivsLength() const divs = await page.$$eval('div', (nodes) => nodes.length)
buildSteps.push(divs) buildSteps.push(divs)
await page.waitFor(1000) await page.waitFor(1000)
repeat++ repeat++
@ -92,29 +45,59 @@ tests.push(async ({ page, eq, getBricksIds }) => {
eq(bricksIds, allBricksIds) eq(bricksIds, allBricksIds)
}) })
tests.push(async ({ eq, getMiddleBricksIds }) => { tests.push(async ({ page, eq }) => {
// check that the middle column bricks have the `foundation` attribute to `true` // check that the middle column bricks have the `foundation` attribute to `true`
const expectedIds = allBricksIds.filter( const expectedIds = allBricksIds.filter(
(b) => b.replace('brick-', '') % 3 === 2, (b) => b.replace('brick-', '') % 3 === 2,
) )
const middleBricksIds = await getMiddleBricksIds() const middleBricksIds = await page.$$eval('div', (nodes) =>
nodes
.filter((node) => node.id.includes('brick') && node.dataset.foundation)
.map((n) => n.id),
)
eq(middleBricksIds, expectedIds) eq(middleBricksIds, expectedIds)
}) })
tests.push(async ({ eq, hammer, getExpectedRepairedIds, getRepairedIds }) => { tests.push(async ({ page, eq }) => {
// check that the bricks to repair have the right repaired attribute // check that the bricks to repair have the right repaired attribute
const hammer = await page.$('#hammer')
await hammer.click() await hammer.click()
eq(await getRepairedIds(), await getExpectedRepairedIds())
})
tests.push(async ({ eq, hammer, getExpectedRepairedIds, getRepairedIds }) => { const expectedRepairedIds = await page.$eval('body', (body) => {
// check that the bricks to repair have the right repaired attribute const getIdInt = (str) => str.replace('brick-', '')
await hammer.click() return body.dataset.reparations
eq(await getRepairedIds(), await getExpectedRepairedIds()) .split(',')
.sort((a, b) => getIdInt(b) - getIdInt(a))
.map((id) => {
const isMiddleBrick = getIdInt(id) % 3 === 2
const status = isMiddleBrick ? 'in progress' : 'repaired'
return `${id}_${status}`
})
})
const repairedIds = await page.$$eval('div', (nodes) => {
const getIdInt = (str) => str.replace('brick-', '')
return nodes
.filter(
(node) =>
node.dataset.repaired === 'true' ||
node.dataset.repaired === 'in progress',
)
.sort((a, b) => getIdInt(b.id) - getIdInt(a.id))
.map(({ id }) => {
const isMiddleBrick = getIdInt(id) % 3 === 2
const status = isMiddleBrick ? 'in progress' : 'repaired'
return `${id}_${status}`
})
})
eq(repairedIds, expectedRepairedIds)
}) })
tests.push(async ({ eq, dynamite, getBricksIds }) => { tests.push(async ({ page, eq, getBricksIds }) => {
// check that the last brick is removed on each dynamite click // check that the last brick is removed on each dynamite click
const dynamite = await page.$('#dynamite')
for (const i of allBricksIds.keys()) { for (const i of allBricksIds.keys()) {
await dynamite.click() await dynamite.click()
const { length } = allBricksIds const { length } = allBricksIds

Loading…
Cancel
Save