Skip to content

Commit

Permalink
Add tests and fix a couple more PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pete-Y-CS committed Jul 14, 2023
1 parent 2eda6f2 commit 88de90a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/lib/common/data_getter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FeatureCollection } from "geojson";
import authoritiesUrl from "../../../assets/authorities.geojson?url";

export async function getAuthoritiesData(): Promise<FeatureCollection> {
export async function getAuthoritiesGeoJson(): Promise<FeatureCollection> {
const resp = await fetch(authoritiesUrl);
const body = await resp.text();
return JSON.parse(body);
Expand Down
1 change: 1 addition & 0 deletions src/pages/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
let showInstructions = false;
const params = new URLSearchParams(window.location.search);
// TODO Add validation and some kind of error page
let authorityName: string = params.get("authority")!;
let style: string = params.get("style") || "streets";
let schema: Schema = (params.get("schema") as Schema) || "v1";
Expand Down
1 change: 1 addition & 0 deletions src/pages/BrowseSchemes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
let gj = JSON.parse(text);
gjScheme.set(gj);
addSchemeToSidebar(gj);
errorMessage = "";
$map?.fitBounds(bbox(gj), { padding: 20, animate: false });
} catch (err) {
Expand Down
16 changes: 10 additions & 6 deletions src/pages/ChooseArea.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
import Radio from "../lib/govuk/Radio.svelte";
import SecondaryButton from "../lib/govuk/SecondaryButton.svelte";
import "maplibre-gl/dist/maplibre-gl.css";
import { getAuthoritiesData } from "../lib/common/data_getter";
import { getAuthoritiesGeoJson } from "../lib/common/data_getter";
import FileInput from "../lib/common/FileInput.svelte";
import About from "../lib/sidebar/About.svelte";
import { bbox } from "../maplibre_helpers";
import type { Schema } from "../types";
let showAbout = false;
const params = new URLSearchParams(window.location.search);
let errorMessage: string = params.get("error")!;
let pageErrorMessage: string = params.get("error") || "";
let uploadErrorMessage: string = "";
let inputValue: string;
let dataList: HTMLDataListElement;
Expand All @@ -39,7 +40,7 @@
// For govuk components. Must happen here.
initAll();
const json: FeatureCollection = await getAuthoritiesData();
const json: FeatureCollection = await getAuthoritiesGeoJson();
for (let feature of json.features) {
let option = document.createElement("option");
option.value = feature.properties!.name;
Expand Down Expand Up @@ -131,7 +132,7 @@
window.localStorage.setItem(filename, JSON.stringify(gj));
window.location.href = `scheme.html?authority=${gj.authority}&schema=${schema}`;
} catch (err) {
errorMessage = `Couldn't load scheme from a file: ${err}`;
pageErrorMessage = `Couldn't load scheme from a file: ${err}`;
}
}
Expand All @@ -158,8 +159,8 @@
<SecondaryButton on:click={() => (showAbout = !showAbout)}
>About</SecondaryButton
>
{#if errorMessage}
<ErrorMessage {errorMessage} />
{#if pageErrorMessage}
<ErrorMessage errorMessage={pageErrorMessage} />
{/if}

<FormElement
Expand Down Expand Up @@ -195,6 +196,9 @@

<hr />

{#if uploadErrorMessage}
<ErrorMessage errorMessage={uploadErrorMessage} />
{/if}
<FileInput
label="Or upload an ATIP GeoJSON file"
{loadFile}
Expand Down
63 changes: 63 additions & 0 deletions tests/data/Adu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"type": "FeatureCollection",
"features": [
{
"geometry": {
"coordinates": [
[-0.343517934752635, 50.83443926871279],
[-0.34351569994271874, 50.834438700341536],
[-0.34321469951591466, 50.83443799977002],
[-0.3432029575114566, 50.83444605229561],
[-0.34321469951591466, 50.83443799977002],
[-0.3422981996989191, 50.83444090008217],
[-0.3423013711361761, 50.83444192171151],
[-0.3422981996989191, 50.83444090008217],
[-0.3419981999545171, 50.83443539983127],
[-0.34200479762583036, 50.834435613869815],
[-0.3419981999545171, 50.83443539983127],
[-0.34141300003176345, 50.83443399958754],
[-0.34108819959107417, 50.834420400945646],
[-0.34087890039001867, 50.83439889996486],
[-0.3401875000225311, 50.83430569967028],
[-0.3399347999588344, 50.83428619968005],
[-0.33965239969838434, 50.83428680042688],
[-0.3396458774696988, 50.834281270498366],
[-0.33965239969838434, 50.83428680042688],
[-0.3392308006725132, 50.83429450041841],
[-0.3390479006973858, 50.83428809994659],
[-0.33878789978014734, 50.83424480030957],
[-0.33834540029949883, 50.83413690060346],
[-0.33823339931310326, 50.834118999607036],
[-0.3380549006324096, 50.83407760023655],
[-0.3380569261957923, 50.83407332666031],
[-0.3380549006324096, 50.83407760023655],
[-0.3381299005685101, 50.833942600373376],
[-0.33827329992250627, 50.83361960002792],
[-0.33826499981000835, 50.83361408089127]
],
"type": "LineString"
},
"properties": {
"length_meters": 449.684,
"waypoints": [
{
"lat": 50.83443926871279,
"lon": -0.343517934752635,
"snapped": true
},
{
"lat": 50.83361408089127,
"lon": -0.33826499981000835,
"snapped": true
}
],
"intervention_type": "route",
"name": "Route from Dankton Lane and West Street to Cokeham Road and Western Road North"
},
"type": "Feature",
"id": 1
}
],
"authority": "Adu",
"origin": "atip-v2"
}
24 changes: 24 additions & 0 deletions tests/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,27 @@ test("other tools work when route tool doesn't load", async ({ page }) => {
await clickMap(page, 500, 500);
await page.getByRole("button", { name: "1) Untitled point" }).isVisible();
});

test("Redirected to homepage with error when incorrect authority given to scheme page", async ({
page,
}) => {
await page.goto("/scheme.html?authority=Adu");

await expect(page.getByText("Authority name not found:")).toBeVisible();
await expect(page.url()).toBe(
"http://localhost:8080/?error=Authority%20name%20not%20found:%20Adu"
);
});

test("Upload invalid geojson to select authority page", async ({ page }) => {
await page.goto("/");
await page
.getByLabel("Or upload an ATIP GeoJSON file")
.setInputFiles("tests/data/Adu.json");

await expect(
page.getByText(
"Couldn't load scheme from a file: Error: Unknown authority Adu"
)
).toBeVisible();
});
11 changes: 11 additions & 0 deletions tests/homepage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ test("choosing a local authority and clicking start changes the url", async ({
/.*scheme.html\?authority=West%20Yorkshire%20Combined%20Authority/
);
});

test("Uploading a valid atip geojson redirects to the appropriate authority scheme page", async ({
page,
}) => {
await page.goto("/");
await page
.getByLabel("Or upload an ATIP GeoJSON file")
.setInputFiles("tests/data/Adur.json");

await expect(page).toHaveURL(/.*scheme.html\?authority=Adur/);
});

0 comments on commit 88de90a

Please sign in to comment.