mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-09 17:57:26 +03:00
Fixed importing servers in android due to wrong mime type
This commit is contained in:
parent
4869435aca
commit
fe81bfccef
2 changed files with 14 additions and 4 deletions
|
@ -1,13 +1,19 @@
|
||||||
import { CsvJson } from 'csvjson';
|
import { CsvJson } from 'csvjson';
|
||||||
import { ServerData } from '../data';
|
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 {
|
export default class ServersImporter {
|
||||||
public constructor(private readonly csvjson: CsvJson, private readonly fileReaderFactory: () => FileReader) {}
|
public constructor(private readonly csvjson: CsvJson, private readonly fileReaderFactory: () => FileReader) {}
|
||||||
|
|
||||||
public readonly importServersFromFile = async (file?: File | null): Promise<ServerData[]> => {
|
public readonly importServersFromFile = async (file?: File | null): Promise<ServerData[]> => {
|
||||||
if (!file || file.type !== CSV_MIME_TYPE) {
|
if (!isCsv(file)) {
|
||||||
throw new Error('No file provided or file is not a CSV');
|
throw new Error('No file provided or file is not a CSV');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,8 +29,12 @@ describe('ServersImporter', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('reads file when a CSV is provided', async () => {
|
it.each([
|
||||||
await importer.importServersFromFile(Mock.of<File>({ type: 'text/csv' }));
|
[ 'text/csv' ],
|
||||||
|
[ 'text/comma-separated-values' ],
|
||||||
|
[ 'application/csv' ],
|
||||||
|
])('reads file when a CSV is provided', async (type) => {
|
||||||
|
await importer.importServersFromFile(Mock.of<File>({ type }));
|
||||||
|
|
||||||
expect(readAsText).toHaveBeenCalledTimes(1);
|
expect(readAsText).toHaveBeenCalledTimes(1);
|
||||||
expect(toObject).toHaveBeenCalledTimes(1);
|
expect(toObject).toHaveBeenCalledTimes(1);
|
||||||
|
|
Loading…
Reference in a new issue