|
1 | 1 | import { parse } from '@/libs/papaparse'
|
2 | 2 | import { describe, expect, test } from 'vitest'
|
| 3 | +import { z } from 'zod' |
3 | 4 |
|
4 | 5 | describe('libs/papaparse', () => {
|
5 | 6 | describe('parse', () => {
|
6 | 7 | test('csv を渡すと指定したオブジェクトの型にパース変換する', async () => {
|
7 |
| - type CSVRecord = { |
8 |
| - hoge: string |
9 |
| - fuga: number |
10 |
| - } |
| 8 | + const schema = z.array( |
| 9 | + z.object({ |
| 10 | + hoge: z.coerce.string(), |
| 11 | + fuga: z.number().min(0).max(120), |
| 12 | + piyo: z.coerce.string(), |
| 13 | + }), |
| 14 | + ) |
11 | 15 |
|
12 |
| - const data = ['hoge,fuga', 'a,1', 'a,4'].join('\r\n') |
| 16 | + const data = ['hoge,fuga,piyo', 'a,1,3', 'a,4,piyo'].join('\r\n') |
13 | 17 | const file = new File([data], 'foo.csv', {
|
14 | 18 | type: 'text/csv',
|
15 | 19 | })
|
16 | 20 |
|
17 |
| - const result = await parse<CSVRecord>(file) |
| 21 | + const result = await parse(file, schema) |
18 | 22 | expect(result).toEqual([
|
19 |
| - { hoge: 'a', fuga: 1 }, |
20 |
| - { hoge: 'a', fuga: 4 }, |
| 23 | + { hoge: 'a', fuga: 1, piyo: '3' }, |
| 24 | + { hoge: 'a', fuga: 4, piyo: 'piyo' }, |
21 | 25 | ])
|
22 | 26 | })
|
| 27 | + |
| 28 | + test('拡張子が csv 以外のファイルを渡すとエラーになる', async () => { |
| 29 | + const schema = z.array( |
| 30 | + z.object({ |
| 31 | + hoge: z.coerce.string(), |
| 32 | + fuga: z.number().min(0).max(120), |
| 33 | + piyo: z.coerce.string(), |
| 34 | + }), |
| 35 | + ) |
| 36 | + |
| 37 | + const data = ['non defined header'].join('\r\n') |
| 38 | + const file = new File([data], 'foo.txt', { |
| 39 | + type: 'text/csv', |
| 40 | + }) |
| 41 | + |
| 42 | + await expect(parse(file, schema)).rejects.toThrowError( |
| 43 | + 'CSV ファイルを選択してください', |
| 44 | + ) |
| 45 | + }) |
| 46 | + |
| 47 | + test('ファイルタイプが text/csv 以外のファイルを渡すとエラーになる', async () => { |
| 48 | + const schema = z.array( |
| 49 | + z.object({ |
| 50 | + hoge: z.coerce.string(), |
| 51 | + fuga: z.number().min(0).max(120), |
| 52 | + piyo: z.coerce.string(), |
| 53 | + }), |
| 54 | + ) |
| 55 | + |
| 56 | + const data = ['non defined header'].join('\r\n') |
| 57 | + const file = new File([data], 'foo.csv', { |
| 58 | + type: 'text/plain', |
| 59 | + }) |
| 60 | + |
| 61 | + await expect(parse(file, schema)).rejects.toThrowError( |
| 62 | + 'CSV ファイルを選択してください', |
| 63 | + ) |
| 64 | + }) |
| 65 | + |
| 66 | + test('カンマ区切りではないファイルを渡すとエラーになる', async () => { |
| 67 | + const schema = z.array( |
| 68 | + z.object({ |
| 69 | + hoge: z.coerce.string(), |
| 70 | + fuga: z.number().min(0).max(120), |
| 71 | + piyo: z.coerce.string(), |
| 72 | + }), |
| 73 | + ) |
| 74 | + |
| 75 | + // 一行目はヘッダーとして認識されるため、パースエラーは二行以上必要 |
| 76 | + const data = ['/', '/'].join('\r\n') |
| 77 | + const file = new File([data], 'foo.csv', { |
| 78 | + type: 'text/csv', |
| 79 | + }) |
| 80 | + |
| 81 | + await expect(parse(file, schema)).rejects.toThrowError( |
| 82 | + 'ァイルのパースに失敗しました', |
| 83 | + ) |
| 84 | + }) |
23 | 85 | })
|
24 | 86 | })
|
0 commit comments