Skip to content

Commit

Permalink
periodic-table.test.ts don't iterate over full table, do random subse…
Browse files Browse the repository at this point in the history
…t for speed
  • Loading branch information
janosh committed Jul 12, 2023
1 parent e4bcc92 commit be68b6a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
20 changes: 20 additions & 0 deletions tests/index.ts
@@ -0,0 +1,20 @@
export function random_sample<T>(input_list: T[], n_samples: number): T[] {
// If the subset size is greater than the list size, return the original list
if (n_samples >= input_list.length) {
return input_list
}

// Generate a random subset
const rand_sample = []
const used_indices = new Set()

while (rand_sample.length < n_samples) {
const rand_idx = Math.floor(Math.random() * input_list.length)
if (!used_indices.has(rand_idx)) {
rand_sample.push(input_list[rand_idx])
used_indices.add(rand_idx)
}
}

return rand_sample
}
23 changes: 11 additions & 12 deletions tests/periodic-table.test.ts
@@ -1,4 +1,5 @@
import { expect, test } from '@playwright/test'
import { random_sample } from '.'
import element_data from '../src/lib/element/data.ts'
import {
categories,
Expand All @@ -13,7 +14,10 @@ test.describe(`Periodic Table`, () => {
await page.goto(`/`, { waitUntil: `networkidle` })

const element_tiles = await page.$$(`.element-tile`)
expect(element_tiles).toHaveLength(element_data.length + 2)
const n_lanthanide_actinide_placeholders = 2
expect(element_tiles).toHaveLength(
element_data.length + n_lanthanide_actinide_placeholders,
)

for (const category of categories) {
let count = category_counts[category] as number
Expand Down Expand Up @@ -44,9 +48,8 @@ test.describe(`Periodic Table`, () => {
})
await page.goto(`/`, { waitUntil: `networkidle` })

const element_tiles = await page.$$(`.element-tile`)
for (const element_tile of element_tiles) {
await element_tile.hover()
for (const tile of random_sample(await page.$$(`.element-tile`), 5)) {
await tile.hover()
}

expect(logs, logs.join(`\n`)).toHaveLength(0)
Expand All @@ -64,16 +67,12 @@ test.describe(`Periodic Table`, () => {
await page.click(`text=${heatmap_label}`)
}

for (const _ of Array(5)) {
// check 5 random element tiles display the expected heatmap value

const rand_idx = Math.floor(Math.random() * element_data.length)
const random_element = element_data[rand_idx]

const heatmap_val = pretty_num(random_element[heatmap_keys.at(-1)])
// check 5 random element tiles display the expected heatmap value
for (const rand_elem of random_sample(element_data, 5)) {
const heatmap_val = pretty_num(rand_elem[heatmap_keys.at(-1)])

// make sure heatmap value is displayed correctly
const text = `${rand_idx + 1} ${random_element.symbol} ${heatmap_val}`
const text = `${rand_elem.number} ${rand_elem.symbol} ${heatmap_val}`
const elem_tile = await page.$(`text=${text}`, { strict: true })
await page.pause()
expect(elem_tile, `selector text=${text}`).not.toBeNull()
Expand Down

0 comments on commit be68b6a

Please sign in to comment.