1
0
Fork 0
mirror of synced 2024-09-21 11:53:49 +12:00
budibase/packages/server/src/utilities/tests/csv.spec.ts

34 lines
1,008 B
TypeScript
Raw Normal View History

2023-05-02 21:44:25 +12:00
import { jsonFromCsvString } from "../csv"
describe("csv", () => {
describe("jsonFromCsvString", () => {
test("multiple lines csv can be casted", async () => {
const csvString = '"id","title"\n"1","aaa"\n"2","bbb"'
const result = await jsonFromCsvString(csvString)
expect(result).toEqual([
{ id: "1", title: "aaa" },
{ id: "2", title: "bbb" },
])
2023-05-02 22:57:18 +12:00
result.forEach(r => expect(Object.keys(r)).toEqual(["id", "title"]))
})
test("empty values are casted as undefined", async () => {
const csvString =
'"id","optional","title"\n1,,"aaa"\n2,"value","bbb"\n3,,"ccc"'
const result = await jsonFromCsvString(csvString)
expect(result).toEqual([
2023-05-02 23:46:53 +12:00
{ id: "1", optional: null, title: "aaa" },
2023-05-02 22:57:18 +12:00
{ id: "2", optional: "value", title: "bbb" },
2023-05-02 23:46:53 +12:00
{ id: "3", optional: null, title: "ccc" },
2023-05-02 22:57:18 +12:00
])
result.forEach(r =>
expect(Object.keys(r)).toEqual(["id", "optional", "title"])
)
2023-05-02 21:44:25 +12:00
})
})
})