From fe81bfccef4b4aaec86ee0bd28fd41dfad6ef3af Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 30 Dec 2020 20:52:05 +0100 Subject: [PATCH] Fixed importing servers in android due to wrong mime type --- src/servers/services/ServersImporter.ts | 10 ++++++++-- test/servers/services/ServersImporter.test.ts | 8 ++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/servers/services/ServersImporter.ts b/src/servers/services/ServersImporter.ts index 62de8f53..03c1ca25 100644 --- a/src/servers/services/ServersImporter.ts +++ b/src/servers/services/ServersImporter.ts @@ -1,13 +1,19 @@ import { CsvJson } from 'csvjson'; import { ServerData } from '../data'; -const CSV_MIME_TYPE = 'text/csv'; + +interface CsvFile extends File { + type: 'text/csv' | 'text/comma-separated-values' | 'application/csv'; +} + +const CSV_MIME_TYPES = [ 'text/csv', 'text/comma-separated-values', 'application/csv' ]; +const isCsv = (file?: File | null): file is CsvFile => !!file && CSV_MIME_TYPES.includes(file.type); export default class ServersImporter { public constructor(private readonly csvjson: CsvJson, private readonly fileReaderFactory: () => FileReader) {} public readonly importServersFromFile = async (file?: File | null): Promise => { - if (!file || file.type !== CSV_MIME_TYPE) { + if (!isCsv(file)) { throw new Error('No file provided or file is not a CSV'); } diff --git a/test/servers/services/ServersImporter.test.ts b/test/servers/services/ServersImporter.test.ts index 28c4e390..f6749ca6 100644 --- a/test/servers/services/ServersImporter.test.ts +++ b/test/servers/services/ServersImporter.test.ts @@ -29,8 +29,12 @@ describe('ServersImporter', () => { ); }); - it('reads file when a CSV is provided', async () => { - await importer.importServersFromFile(Mock.of({ type: 'text/csv' })); + it.each([ + [ 'text/csv' ], + [ 'text/comma-separated-values' ], + [ 'application/csv' ], + ])('reads file when a CSV is provided', async (type) => { + await importer.importServersFromFile(Mock.of({ type })); expect(readAsText).toHaveBeenCalledTimes(1); expect(toObject).toHaveBeenCalledTimes(1);