Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kflorence committed Jan 18, 2024
1 parent e7e8b74 commit 55bc029
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
27 changes: 17 additions & 10 deletions test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class PuzzleFixture {
modifiers: undefined
}

constructor () {
constructor (id) {
this.after = this.after.bind(this)
this.before = this.before.bind(this)
this.url = `${PuzzleFixture.baseUrl}/#/${id}`
}

async after () {
Expand All @@ -33,32 +34,38 @@ class PuzzleFixture {
)

this.driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build()
await this.driver.get('http://localhost:1234')
await this.driver.get(this.url)

this.elements.body = await this.driver.findElement(By.tagName('body'))
this.elements.canvas = await this.driver.findElement(By.id('puzzle'))
this.elements.modifiers = await this.driver.findElement(By.id('modifiers-mutable'))
}

async clickModifier (name) {
const modifier = await this.driver.findElement(By.className(`modifier-${name}`))
const actions = this.driver.actions({ async: true })
await actions.move({ origin: modifier }).click().perform()
const origin = this.#getModifier(name)
await this.driver.actions({ async: true }).move({ origin }).click().perform()
}

async isSolved () {
return hasClass(this.elements.body, 'puzzle-solved')
}

async selectTile (r, c) {
async selectModifier (name) {
const origin = this.#getModifier(name)
await this.driver.actions({ async: true }).move({ origin }).press().pause(500).release().perform()
}

async clickOnTile (r, c) {
// Center on the tile we want to click on. This ensures it is visible
await this.driver.executeScript(`return beaming.centerOnTile(${r}, ${c})`)
await this.driver.actions({ async: true }).move({ origin: this.elements.canvas }).click().perform()
}

const actions = this.driver.actions({ async: true })
await actions.move({ origin: this.elements.canvas }).click().perform()

await this.driver.wait(until.elementIsVisible(this.elements.modifiers), 500)
async #getModifier (name) {
return await this.driver.findElement(By.className(`modifier-${name}`))
}

static baseUrl = 'http://localhost:1234'
}

async function hasClass (element, name) {
Expand Down
6 changes: 3 additions & 3 deletions test/puzzles/001.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const { PuzzleFixture } = require('../fixtures.js')
const assert = require('assert')

describe('Puzzle 001', function () {
const puzzle = new PuzzleFixture()
const puzzle = new PuzzleFixture('001')

after(puzzle.after)
before(puzzle.before)

it('should be solvable', async function () {
await puzzle.selectTile(2, 0)
it('should be solved', async function () {
await puzzle.clickOnTile(2, 0)
await puzzle.clickModifier('toggle')

assert(await puzzle.isSolved())
Expand Down
17 changes: 17 additions & 0 deletions test/puzzles/002.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-env mocha */
const { PuzzleFixture } = require('../fixtures.js')
const assert = require('assert')

describe('Puzzle 002', function () {
const puzzle = new PuzzleFixture('002')

after(puzzle.after)
before(puzzle.before)

it('should be solved', async function () {
await puzzle.clickOnTile(2, 0)
await puzzle.clickModifier('rotate')

assert(await puzzle.isSolved())
})
})
19 changes: 19 additions & 0 deletions test/puzzles/003.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-env mocha */
const { PuzzleFixture } = require('../fixtures.js')
const assert = require('assert')

describe('Puzzle 003', function () {
const puzzle = new PuzzleFixture('003')

after(puzzle.after)
before(puzzle.before)

it('should be solved', async function () {
await puzzle.clickOnTile(2, 1)
await puzzle.selectModifier('rotate')
await puzzle.clickOnTile(2, 0)
await puzzle.clickModifier('rotate')

assert(await puzzle.isSolved())
})
})
23 changes: 23 additions & 0 deletions test/puzzles/004.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-env mocha */
const { PuzzleFixture } = require('../fixtures.js')
const assert = require('assert')

describe('Puzzle 004', function () {
const puzzle = new PuzzleFixture('004')

after(puzzle.after)
before(puzzle.before)

it('should be solved', async function () {
await puzzle.clickOnTile(0, 0)
await puzzle.clickModifier('rotate')
await puzzle.clickModifier('rotate')
await puzzle.clickModifier('rotate')

await puzzle.clickOnTile(2, 0)
await puzzle.clickModifier('swap')
await puzzle.clickOnTile(0, 0)

assert(await puzzle.isSolved())
})
})

0 comments on commit 55bc029

Please sign in to comment.