Skip to content

Commit

Permalink
Add test to check if headers are right
Browse files Browse the repository at this point in the history
  • Loading branch information
fhennig committed Jan 22, 2025
1 parent 62818c6 commit a9f5b03
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions website/tests/pages/submission/template.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Download } from '@playwright/test';
import * as XLSX from 'xlsx';

import { expect, test } from '../../e2e.fixture.ts';

Expand Down Expand Up @@ -38,26 +39,58 @@ test.describe('The submit page', () => {

let download = await revisePage.downloadTsvMetadataTemplate();

const expectedHeaders = ["accession", "submissionId", "country", "date"];

expect(download.suggestedFilename()).toBe('Test_Dummy_Organism_metadata_revision_template.tsv');
const content = await getDownloadedContent(download);
const content = await getDownloadedContentAsString(download);
expect(content).toStrictEqual('accession\tsubmissionId\tcountry\tdate\n');

download = await revisePage.downloadXlsMetadataTemplate();
expect(download.suggestedFilename()).toBe('Test_Dummy_Organism_metadata_revision_template.xls');
var workbook = await getDownloadedContentAsXls(download);

Check failure on line 50 in website/tests/pages/submission/template.spec.ts

View workflow job for this annotation

GitHub Actions / Check format and types

Unexpected var, use let or const instead
expectHeaders(workbook, expectedHeaders);

download = await revisePage.downloadXlsMetadataTemplate();
download = await revisePage.downloadXlsxMetadataTemplate();
expect(download.suggestedFilename()).toBe('Test_Dummy_Organism_metadata_revision_template.xlsx');
workbook = await getDownloadedContentAsXls(download);
expectHeaders(workbook, expectedHeaders);
});

async function getDownloadedContent(download: Download) {
async function getDownloadedContent(download: Download): Promise<ArrayBuffer> {
const readable = await download.createReadStream();
return new Promise((resolve) => {
let data = '';
readable.on('data', (chunk) => (data += chunk as string));
readable.on('end', () => resolve(data));
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
readable.on('data', (chunk) => chunks.push(chunk as Buffer));
readable.on('end', () => {
const buffer = Buffer.concat(chunks);
resolve(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
});
readable.on('error', reject);
});
}

async function getDownloadedContentAsString(download: Download): Promise<string> {
const arrayBuffer = await getDownloadedContent(download);
return new TextDecoder().decode(arrayBuffer);
}

async function getDownloadedContentAsXls(download: Download): Promise<XLSX.WorkBook> {
const arrayBuffer = await getDownloadedContent(download);
return XLSX.read(arrayBuffer);
}

function expectHeaders(workBook: XLSX.WorkBook, headers: string[]) {
expect(workBook.SheetNames.length).toBe(1);
const sheet = workBook.Sheets[workBook.SheetNames[0]];

const aoa = XLSX.utils.sheet_to_json(sheet, { header: 1 });
expect(aoa.length).toBeGreaterThan(0);

const sheetHeaders = aoa[0];
expect(sheetHeaders).toEqual(headers);
}


function skipDownloadTestInWebkit(browserName: 'chromium' | 'firefox' | 'webkit') {
test.skip(
browserName === 'webkit',
Expand Down

0 comments on commit a9f5b03

Please sign in to comment.