Skip to content

Commit 7eb1275

Browse files
Add more unit tests (#311)
1 parent 31adf77 commit 7eb1275

File tree

9 files changed

+164
-9
lines changed

9 files changed

+164
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"check": "tsc --noEmit",
1010
"fmt": "prettier --write .",
1111
"fix": "prettier --write packages/; eslint --fix packages/",
12-
"lint": "prettier --check . && eslint packages/"
12+
"lint": "eslint packages/ && prettier --check ."
1313
},
1414
"devDependencies": {
1515
"@parcel/compressor-brotli": "~2.12.0",

packages/ct/src/js/scorecard.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import {
22
ScorecardValues,
33
formatHeader,
44
formatReformLine,
5-
} from "@prn-parking-lots/shared/src/js/city-ui/scorecard";
6-
import type { CityStats } from "./types";
5+
} from "@prn-parking-lots/shared/src/js/city-ui/scorecard.ts";
6+
7+
import type { CityStats } from "./types.ts";
78

89
export default function formatScorecard(stats: CityStats): ScorecardValues {
910
const header = formatHeader({
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
import createDropdownGroups from "../src/js/dropdownGroups";
4+
5+
test("createDropdownGroups", () => {
6+
const common = {
7+
percentage: "",
8+
population: "",
9+
reforms: "",
10+
url: null,
11+
};
12+
const input = {
13+
city1: {
14+
...common,
15+
name: "City 1",
16+
},
17+
city2: {
18+
...common,
19+
name: "City 2",
20+
},
21+
};
22+
expect(createDropdownGroups(input)).toEqual([
23+
{
24+
label: "Group 1",
25+
cities: [
26+
{ id: "city1", name: "City 1" },
27+
{ id: "city2", name: "City 2" },
28+
],
29+
},
30+
]);
31+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
import { formatReformLine } from "@prn-parking-lots/shared/src/js/city-ui/scorecard.ts";
4+
5+
import formatScorecard from "../src/js/scorecard.ts";
6+
7+
test.describe("formatScorecard", () => {
8+
test("full stats", () => {
9+
const { listEntries } = formatScorecard({
10+
name: "Hartford - rail station",
11+
percentage: "25%",
12+
population: "42,412",
13+
reforms: "adopted",
14+
url: "https://parkingreform.org",
15+
});
16+
expect(listEntries).toEqual([
17+
"42,412 city residents",
18+
formatReformLine("adopted", "https://parkingreform.org"),
19+
]);
20+
});
21+
22+
test("minimum stats", () => {
23+
const { listEntries } = formatScorecard({
24+
name: "Hartford - rail station",
25+
percentage: "25%",
26+
population: "42,412",
27+
reforms: null,
28+
url: null,
29+
});
30+
expect(listEntries).toEqual(["42,412 city residents"]);
31+
});
32+
});

packages/primary/src/js/scorecard.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ import {
22
ScorecardValues,
33
formatHeader,
44
formatReformLine,
5-
} from "@prn-parking-lots/shared/src/js/city-ui/scorecard";
6-
import type { CityStats } from "./types";
5+
} from "@prn-parking-lots/shared/src/js/city-ui/scorecard.ts";
6+
7+
import type { CityStats } from "./types.ts";
8+
9+
export const COMMUNITY_WARNING = `<div class="community-contribution-warning">
10+
<i class="fa-solid fa-triangle-exclamation" aria-hidden="true"></i>
11+
Community-maintained map
12+
</div>`;
713

814
export default function formatScorecard(stats: CityStats): ScorecardValues {
915
let header = formatHeader({
@@ -12,10 +18,7 @@ export default function formatScorecard(stats: CityStats): ScorecardValues {
1218
boundaryDescription: "central city",
1319
});
1420
if (stats.contribution) {
15-
header += `<div class="community-contribution-warning">
16-
<i class="fa-solid fa-triangle-exclamation" aria-hidden="true"></i>
17-
Community-maintained map
18-
</div>`;
21+
header += COMMUNITY_WARNING;
1922
}
2023

2124
const listEntries = [];
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
import {
4+
formatHeader,
5+
formatReformLine,
6+
} from "@prn-parking-lots/shared/src/js/city-ui/scorecard.ts";
7+
8+
import formatScorecard, { COMMUNITY_WARNING } from "../src/js/scorecard.ts";
9+
10+
test.describe("formatScorecard", () => {
11+
test("full stats", () => {
12+
const { header, listEntries } = formatScorecard({
13+
name: "Hartford, CT",
14+
percentage: "25%",
15+
population: "42,412",
16+
reforms: "adopted",
17+
url: "https://parkingreform.org",
18+
cityType: "suburb",
19+
urbanizedAreaPopulation: "104,241",
20+
parkingScore: "88",
21+
contribution: "email@parkingreform.org",
22+
});
23+
expect(header).toEqual(
24+
formatHeader({
25+
name: "Hartford, CT",
26+
percentage: "25%",
27+
boundaryDescription: "central city",
28+
}) + COMMUNITY_WARNING,
29+
);
30+
expect(listEntries).toEqual([
31+
"88/100 parking score (lower is better)",
32+
"City type: suburb",
33+
"42,412 city residents",
34+
"104,241 urbanized area residents",
35+
formatReformLine("adopted", "https://parkingreform.org"),
36+
`<a href="mailto:email@parkingreform.org">Email data maintainer</a>`,
37+
]);
38+
});
39+
40+
test("minimal stats", () => {
41+
const { header, listEntries } = formatScorecard({
42+
name: "Hartford, CT",
43+
percentage: "25%",
44+
population: "42,412",
45+
reforms: null,
46+
url: null,
47+
cityType: "suburb",
48+
urbanizedAreaPopulation: "104,241",
49+
parkingScore: null,
50+
contribution: null,
51+
});
52+
expect(header).toEqual(
53+
formatHeader({
54+
name: "Hartford, CT",
55+
percentage: "25%",
56+
boundaryDescription: "central city",
57+
}),
58+
);
59+
expect(listEntries).toEqual([
60+
"City type: suburb",
61+
"42,412 city residents",
62+
"104,241 urbanized area residents",
63+
]);
64+
});
65+
});

packages/shared/tests/cityId.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, test } from "@playwright/test";
2+
23
import {
34
extractCityIdFromUrl,
45
determineShareUrl,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
import { formatHeader, formatReformLine } from "../src/js/city-ui/scorecard.ts";
4+
5+
test("formatHeader", () => {
6+
const result = formatHeader({
7+
name: "My City",
8+
percentage: "25%",
9+
boundaryDescription: "downtown",
10+
});
11+
expect(result).toEqual(`
12+
<h1 class="scorecard-title">Parking lots in My City</h1>
13+
<p>25% of the downtown is off-street parking</p>
14+
`);
15+
});
16+
17+
test("formatReformLine", () => {
18+
const result = formatReformLine("adopted", "https://parkingreform.org");
19+
expect(result).toEqual(
20+
`Parking reforms adopted (<a class="external-link" title="view parking reform details" href="https://parkingreform.org" target="_blank">details <i aria-hidden="true" class="fa-solid fa-arrow-right"></i></a>)`,
21+
);
22+
});

0 commit comments

Comments
 (0)