Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions tests/awards-timeline.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// calling the unexported WinnerCard with a synthetic entry.

import assert from 'node:assert/strict';
import test from 'node:test';
import test, { before } from 'node:test';

import { importSource } from './helpers-jsx.mjs';
import {
Expand All @@ -27,7 +27,18 @@ const { default: AwardsTimeline } = await importSource(
);
const { default: awardsData } = await importSource('data/awards.json');

const timeline = AwardsTimeline();
// Rendering at module scope would let a change that breaks the render throw
// before node:test starts, so the process dies with an uncaught exception
// instead of a failing test. A root `before` hook runs the same code inside
// the runner, so the failure is reported as a test failure and is attributed
// to whatever caused it.
let timeline;
let WinnerCard;

before(() => {
timeline = AwardsTimeline();
WinnerCard = componentNamed(timeline, 'WinnerCard');
});

/** @returns {any[]} every element in the tree, in document order */
function elements(tree) {
Expand All @@ -50,8 +61,6 @@ function componentNamed(tree, name) {
return element.type;
}

const WinnerCard = componentNamed(timeline, 'WinnerCard');

function cardElements(tree = timeline) {
return elements(tree).filter((element) => element.type === WinnerCard);
}
Expand Down
19 changes: 14 additions & 5 deletions tests/community-people.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// That hook's effect is already covered at 100% by tests/use-focus-trap.test.mjs.

import assert from 'node:assert/strict';
import test from 'node:test';
import test, { before } from 'node:test';

import React from 'react';

Expand Down Expand Up @@ -118,10 +118,16 @@ function findByClass(tree, className) {

// `PersonCard` and `PersonDialog` are private, so they are taken from the tree
// the exported component produces rather than imported.
//
// Rendering at module scope would let a change that breaks the render throw
// before node:test starts, so the process dies with an uncaught exception
// instead of a failing test. A root `before` hook runs the same code inside
// the runner, so the failure is reported as a test failure and is attributed
// to whatever caused it.
const SECTIONS = Object.keys(peopleData.people);
const SAMPLE_SECTION = SECTIONS[0];
const PersonCard = CommunityPeople({ section: SAMPLE_SECTION }).props
.children[0].type;
let PersonCard;
let PersonDialog;

const FULL_PERSON = {
name: 'Ada Lovelace',
Expand All @@ -143,8 +149,11 @@ function openDialog(person) {
return { card, dialogElement };
}

const { dialogElement: SAMPLE_DIALOG } = openDialog(FULL_PERSON);
const PersonDialog = SAMPLE_DIALOG.type;
before(() => {
PersonCard = CommunityPeople({ section: SAMPLE_SECTION }).props.children[0]
.type;
PersonDialog = openDialog(FULL_PERSON).dialogElement.type;
});

function renderDialog(person, onClose = () => {}) {
return render(PersonDialog, {
Expand Down
20 changes: 15 additions & 5 deletions tests/metrics-dashboard.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// than as snapshots of today's numbers.

import assert from 'node:assert/strict';
import test from 'node:test';
import test, { before } from 'node:test';

import { formatDate } from '../src/components/shared/formatDate.js';
import { importWithData } from './helpers-component-data.mjs';
Expand All @@ -29,7 +29,20 @@ const { default: MetricsDashboard } = await importSource(
);
const { default: metricsData } = await importSource('data/metrics.json');

const dashboard = MetricsDashboard();
// Rendering at module scope would let a change that breaks the render throw
// before node:test starts, so the process dies with an uncaught exception
// instead of a failing test. A root `before` hook runs the same code inside
// the runner, so the failure is reported as a test failure and is attributed
// to whatever caused it.
let dashboard;
let lifecycle;
let Sparkline;

before(() => {
dashboard = MetricsDashboard();
lifecycle = renderNested(dashboard, 'LifecycleSection');
Sparkline = componentNamed(lifecycle, 'Sparkline');
});

/** @returns {any[]} every element in the tree, in document order */
function elements(tree) {
Expand Down Expand Up @@ -75,9 +88,6 @@ function parsePoints(value) {
});
}

const lifecycle = renderNested(dashboard, 'LifecycleSection');
const Sparkline = componentNamed(lifecycle, 'Sparkline');

test('the freshness line states when the data was collected', () => {
const meta = elements(dashboard).find((element) =>
textOf(element).startsWith('Last updated'),
Expand Down
Loading