|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { writeFileSync } from "fs"; |
| 3 | +import { formatRow, formatCsv, createCsvFile } from "./createFile"; |
| 4 | +import { parseCsv } from "./parseCsv"; |
| 5 | + |
| 6 | +vi.mock("fs", async () => ({ |
| 7 | + default: vi.fn(), |
| 8 | + writeFileSync: vi.fn().mockReturnValue("foo") |
| 9 | +})); |
| 10 | + |
| 11 | +describe("createCsvFile", () => { |
| 12 | + const data = [ |
| 13 | + ["product_code", "quantity", "pick_location"], |
| 14 | + ["B1234", "2", "A1"], |
| 15 | + ["B1235", "3", "A2"], |
| 16 | + ["B1236", "4", "A3"] |
| 17 | + ]; |
| 18 | + |
| 19 | + describe("formatRow", () => { |
| 20 | + it("should format a row", () => { |
| 21 | + const row = ["product_code", "quantity", "pick_location"]; |
| 22 | + const formatted = formatRow(row); |
| 23 | + expect(formatted).toBe("product_code,quantity,pick_location"); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe("formatCsv", () => { |
| 28 | + it("should format a csv", () => { |
| 29 | + const formatted = formatCsv(data); |
| 30 | + expect(formatted).toBe("product_code,quantity,pick_location\r\nB1234,2,A1\r\nB1235,3,A2\r\nB1236,4,A3"); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should create a csv file", () => { |
| 35 | + const csv = createCsvFile("./src/output.csv", data); |
| 36 | + const formatted = formatCsv(data); |
| 37 | + expect(writeFileSync).toBeCalled(); |
| 38 | + expect(writeFileSync).toHaveBeenCalledWith("./src/output.csv", formatted); |
| 39 | + expect(csv).toBe("foo"); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe("parseCsv", () => { |
| 44 | + it("should return an array of arrays", () => { |
| 45 | + const csv = `product_code,quantity,pick_location\r\n |
| 46 | + B1234,2,A1\r\n |
| 47 | + B1235,3,A2\r\n |
| 48 | + B1236,4,A3\r\n`; |
| 49 | + const parsed = parseCsv(csv); |
| 50 | + expect(parsed).toEqual([ |
| 51 | + ["product_code", "quantity", "pick_location"], |
| 52 | + ["B1234", "2", "A1"], |
| 53 | + ["B1235", "3", "A2"], |
| 54 | + ["B1236", "4", "A3"] |
| 55 | + ]); |
| 56 | + }); |
| 57 | + it("should trim whitespace and trailing commas", () => { |
| 58 | + const csv = `product_code,quantity,pick_location, \r\n |
| 59 | + B1234,2,A1 \r\n |
| 60 | + B1235,3,A2, \r\n |
| 61 | + B1236,4,A3 \r\n`; |
| 62 | + const parsed = parseCsv(csv); |
| 63 | + expect(parsed).toEqual([ |
| 64 | + ["product_code", "quantity", "pick_location"], |
| 65 | + ["B1234", "2", "A1"], |
| 66 | + ["B1235", "3", "A2"], |
| 67 | + ["B1236", "4", "A3"] |
| 68 | + ]); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("readCsv", () => { |
| 73 | + it.todo("should read a csv file"); |
| 74 | +}); |
| 75 | + |
| 76 | +describe("logger", () => { |
| 77 | + it.todo("should log messages to the console"); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("checkColumns", () => { |
| 81 | + it.todo("should check for required columns in csv file"); |
| 82 | +}); |
0 commit comments