Skip to content

Commit

Permalink
Merge branch 'better-mapper-test' into add-histograms
Browse files Browse the repository at this point in the history
  • Loading branch information
kavigupta committed Sep 1, 2024
2 parents dc2ebea + e4c13a6 commit 4c787de
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
16 changes: 15 additions & 1 deletion react/test/mapper_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@

import { TARGET, screencap } from './test_utils';
import { Selector } from 'testcafe';
import { TARGET, download_or_check_string, most_recent_download_path, screencap } from './test_utils';

const fs = require('fs');

async function check_geojson(t, path) {
// download the geojson by clicking the button
await t.click(Selector('button').withText('Export as GeoJSON'));
await t.wait(3000);
const mrdp = most_recent_download_path();
const most_recent_download = fs.readFileSync(mrdp, 'utf8');
await download_or_check_string(t, most_recent_download, path);
}

fixture('mapping')
.page(TARGET + '/mapper.html?settings=H4sIAAAAAAAAA1WOzQ6CQAyEX8XUeCOGixeO%2BggejSEFy7Kh%2B5PdRSWEd7dLjMHe2plvpjMociqg76d60PYBFVwTJoICOs2JAlQzkMWGSbQOOZIoo22TdjZrafIk0O9UwBODzv4I1e2%2BLAW0jl2oo8RugKitYlrtPObDmbEddgcQIKDxGytrSxjgG2Rwq%2FlAkZJoFk3eL2NDPbF%2BQ27OpBRPUiTIiotnX64j0Iu06uWr8ngSd4OR%2FtNdNJLzAd2YY7skAQAA')
Expand All @@ -10,6 +22,7 @@ fixture('mapping')

test("state-map", async t => {
await screencap(t, "state-map");
await check_geojson(t, "state-map-geojson");
})

fixture('mapping-more-complex')
Expand All @@ -24,4 +37,5 @@ test("mapping-more-complex", async t => {
await t.eval(() => location.reload(true));
await t.wait(5000);
await screencap(t, "mapping-more-complex");
await check_geojson(t, "mapping-more-complex-geojson");
})
14 changes: 14 additions & 0 deletions react/test/string_check_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IS_TESTING } from "./test_utils";



fixture('mapping')
.page(TARGET)
// no local storage
.beforeEach(async t => {
await t.eval(() => localStorage.clear());
});

test("state-map", async t => {
await t.expect(IS_TESTING).ok("String tests are in overwrite mode. Set IS_TESTING to true to run tests.")
})
20 changes: 16 additions & 4 deletions react/test/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import { Selector, ClientFunction } from 'testcafe';

const fs = require('fs');
const path = require('path');


export const TARGET = process.env.URBANSTATS_TEST_TARGET ?? "http://localhost:8000"
export const SEARCH_FIELD = Selector('input').withAttribute('placeholder', 'Search Urban Stats');
export const getLocation = ClientFunction(() => document.location.href);

const IS_TESTING = true;
export const IS_TESTING = true;

export function comparison_page(locations) {
const params = new URLSearchParams();
Expand Down Expand Up @@ -98,20 +100,30 @@ export async function download_image(t, name) {
await copy_most_recent_file(t, name);
}

function most_recent_file_path() {
export function most_recent_download_path() {
// get the most recent file in the downloads folder
const path = require('path');
const downloadsFolder = require('downloads-folder');
const files = fs.readdirSync(downloadsFolder());
const sorted = files.map(x => path.join(downloadsFolder(), x)).sort((a, b) => fs.statSync(b).mtimeMs - fs.statSync(a).mtimeMs);

console.log("Most recent download: " + sorted[0]);
return sorted[0];
}

async function copy_most_recent_file(t, name) {
// copy the file to the screenshots folder
const screenshotsFolder = path.join(__dirname, '..', 'screenshots');
fs.copyFileSync(most_recent_file_path(), path.join(screenshotsFolder, name + '_' + t.browser.name + '.png'));
fs.copyFileSync(most_recent_download_path(), path.join(screenshotsFolder, name + '_' + t.browser.name + '.png'));
}

export async function download_or_check_string(t, string, name) {
const path_to_file = path.join(__dirname, '..', "..", "tests", 'reference_strings', name + '.txt');
if (IS_TESTING) {
const expected = fs.readFileSync(path_to_file, 'utf8');
await t.expect(string).eql(expected);
} else {
fs.writeFileSync(path_to_file, string);
}
}

export async function download_or_check_string(t, string, name) {
Expand Down
1 change: 1 addition & 0 deletions tests/reference_strings/mapping-more-complex-geojson.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/reference_strings/state-map-geojson.txt

Large diffs are not rendered by default.

0 comments on commit 4c787de

Please sign in to comment.