Skip to content

Commit

Permalink
debug article test (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebrody authored Feb 2, 2025
1 parent 4ec0b18 commit c85115d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 67 deletions.
40 changes: 21 additions & 19 deletions react/src/components/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ function PointerButtonsIndex(props: { ordinal?: number, statpath: string, type:

function PointerButtonIndex(props: {
getData: () => Promise<string[]>
originalPos?: number
originalPos?: number // 1-indexed
direction: -1 | 1
total: number
longname: string
Expand All @@ -980,26 +980,28 @@ function PointerButtonIndex(props: {
const colors = useColors()
const navigation = useContext(Navigator.Context)
const [showHistoricalCDs] = useSetting('show_historical_cds')
const outOfBounds = (pos: number): boolean => pos < 0 || pos >= props.total
const newPos = (oldPos: number): number => oldPos - 1 + props.direction
const onClick = async (): Promise<void> => {
{
const data = await props.getData()
let pos = newPos(data.indexOf(props.longname) + 1)
while (!outOfBounds(pos)) {
const name = data[pos]
if (!showHistoricalCDs && isHistoricalCD(name)) {
pos += props.direction
continue
}
void navigation.navigate({
kind: 'article',
longname: name,
universe,
}, { history: 'push', scroll: { kind: 'element', element: buttonRef.current! } })
return
/* eslint-disable no-console -- Debugging test failure */
console.log(`Click on pointer button! props=${JSON.stringify(props)}`)
const data = await props.getData()
let pos = data.indexOf(props.longname) + props.direction
console.log(`Starting position=${pos}`)
while (pos >= 0 && pos < props.total) {
const name = data[pos]
console.log(`name=${name}`)
if (!showHistoricalCDs && isHistoricalCD(name)) {
pos += props.direction
continue
}
console.log(`navigate to ${name}`)
void navigation.navigate({
kind: 'article',
longname: name,
universe,
}, { history: 'push', scroll: { kind: 'element', element: buttonRef.current! } })
return
}
/* eslint-enable no-console */
}

const buttonStyle: React.CSSProperties = {
Expand All @@ -1019,7 +1021,7 @@ function PointerButtonIndex(props: {

let disabled: boolean = props.disable ?? false
if (props.originalPos !== undefined) {
disabled = outOfBounds(newPos(props.originalPos)) || props.originalPos > props.total
disabled = props.originalPos + props.direction < 1 || props.originalPos + props.direction > props.total
}

const buttonRef = useRef<HTMLButtonElement>(null) // Need the ref otherwise the mouse enter and leave events can be sent to the wrong elem
Expand Down
102 changes: 54 additions & 48 deletions react/test/article_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,54 +146,60 @@ test('create-comparison-from-article', async (t) => {
urbanstatsFixture('lr overall', `/article.html?longname=Nairobi+%28Center%29+5MPC%2C+Kenya&s=D9dego8tisfjWgh`)

test('lr-overall-other-stat', async (t) => {
// button with a < on it
const prevOverallArea = Selector('button[data-test-id="-1"]').nth(1)
const nextOverallArea = Selector('button[data-test-id="1"]').nth(1)
const prevOverallCompactness = Selector('button[data-test-id="-1"]').nth(3)
const nextOverallCompactness = Selector('button[data-test-id="1"]').nth(3)
await t
.click(prevOverallArea)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=49633%2C+USA&universe=world`)
await t.wait(100)
await t
.click(nextOverallArea)
await t.wait(100)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Nairobi+%28Center%29+5MPC%2C+Kenya`)
// check that prevOverallCompactness is disabled
await t.expect(prevOverallCompactness.hasAttribute('disabled')).ok()
// check that prevOverallCompactness does nothing when clicked
await t.click(prevOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Nairobi+%28Center%29+5MPC%2C+Kenya`)

await t.click(nextOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Singapore+%28Center%29+5MPC%2C+Singapore`)
// check that prevOverallCompactness is enabled
await t.expect(prevOverallCompactness.hasAttribute('disabled')).notOk()
await t.click(prevOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Nairobi+%28Center%29+5MPC%2C+Kenya`)
// least compact region
await t.navigateTo('/article.html?longname=Fiji&s=D9dego8tisfjWgh')
// check that nextOverallCompactness is disabled
await t.expect(nextOverallCompactness.hasAttribute('disabled')).ok()
// check that nextOverallCompactness does nothing when clicked
await t.click(nextOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Fiji`)
// check that prevOverallCompactness is enabled
await t.expect(prevOverallCompactness.hasAttribute('disabled')).notOk()
await t.click(prevOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Northern%2C+Fiji`)
// check that nextOverallCompactness is enabled
await t.expect(nextOverallCompactness.hasAttribute('disabled')).notOk()
await t.click(nextOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Fiji`)
try {
// button with a < on it
const prevOverallArea = Selector('button[data-test-id="-1"]').nth(1)
const nextOverallArea = Selector('button[data-test-id="1"]').nth(1)
const prevOverallCompactness = Selector('button[data-test-id="-1"]').nth(3)
const nextOverallCompactness = Selector('button[data-test-id="1"]').nth(3)
await t
.click(prevOverallArea)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=49633%2C+USA&universe=world`)
await t.wait(100)
await t
.click(nextOverallArea)
await t.wait(100)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Nairobi+%28Center%29+5MPC%2C+Kenya`)
// check that prevOverallCompactness is disabled
await t.expect(prevOverallCompactness.hasAttribute('disabled')).ok()
// check that prevOverallCompactness does nothing when clicked
await t.click(prevOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Nairobi+%28Center%29+5MPC%2C+Kenya`)

await t.click(nextOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Singapore+%28Center%29+5MPC%2C+Singapore`)
// check that prevOverallCompactness is enabled
await t.expect(prevOverallCompactness.hasAttribute('disabled')).notOk()
await t.click(prevOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Nairobi+%28Center%29+5MPC%2C+Kenya`)
// least compact region
await t.navigateTo('/article.html?longname=Fiji&s=D9dego8tisfjWgh')
// check that nextOverallCompactness is disabled
await t.expect(nextOverallCompactness.hasAttribute('disabled')).ok()
// check that nextOverallCompactness does nothing when clicked
await t.click(nextOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Fiji`)
// check that prevOverallCompactness is enabled
await t.expect(prevOverallCompactness.hasAttribute('disabled')).notOk()
await t.click(prevOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Northern%2C+Fiji`)
// check that nextOverallCompactness is enabled
await t.expect(nextOverallCompactness.hasAttribute('disabled')).notOk()
await t.click(nextOverallCompactness)
await t.expect(getLocationWithoutSettings())
.eql(`${target}/article.html?longname=Fiji`)
}
finally {
// eslint-disable-next-line no-console -- Debugging test failure
console.log(await t.getBrowserConsoleMessages())
}
})

urbanstatsFixture('all stats test', `/article.html?longname=California%2C+USA`)
Expand Down

0 comments on commit c85115d

Please sign in to comment.