Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for fieldSeparator in main tests #97

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

228 changes: 12 additions & 216 deletions lib/__specs__/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ describe("ExportToCsv", () => {
expect(typeof string === "string").toBeTruthy();
});

it("should use fieldSeparator if supplied", () => {
const options: ConfigOptions = {
title: "Test Csv",
useBom: false,
useKeysAsHeaders: true,
fieldSeparator: ";",
};

const string = asString(generateCsv(options)([{ test: "hello" }]));
expect(string).toEqual('"test"\r\n"hello"\r\n');
});

it("should use keys of first object in collection as headers", () => {
const options: ConfigOptions = {
title: "Test Csv",
Expand Down Expand Up @@ -300,219 +312,3 @@ describe("ExportToCsv", () => {
expect(firstLine).toBe("Test Csv 2\r");
});
});

describe("ExportToCsv As A Text File", () => {
it("should create a comma seperated string", () => {
const options: ConfigOptions = {
title: "Test Csv 1",
useTextFile: true,
useBom: true,
showColumnHeaders: true,
useKeysAsHeaders: true,
};

const string = asString(generateCsv(options)(mockData));
expect(typeof string === "string").toBeTruthy();
});

it("should use keys of first object in collection as headers", () => {
const options: ConfigOptions = {
filename: "Test Csv 2",
useTextFile: true,
useBom: true,
showColumnHeaders: true,
useKeysAsHeaders: true,
};

const output = asString(generateCsv(options)(mockData));

const firstLine = output.split("\n")[0];
const keys = firstLine.split(",").map((s: string) => s.trim());

expect(keys).toEqual([
'"name"',
'"age"',
'"average"',
'"approved"',
'"description"',
'"quotedNumber"',
]);
});

it("should only use columns in columnHeaders", () => {
const options: ConfigOptions = {
filename: "Test Csv 2",
useTextFile: true,
useBom: true,
showColumnHeaders: true,
columnHeaders: ["name", "age"],
};

const output = asString(generateCsv(options)(mockData));

const firstLine = output.split("\n")[0];
const keys = firstLine.split(",").map((s: string) => s.trim());

expect(keys).toEqual(['"name"', '"age"']);
});

it("should allow only headers to be generated", () => {
const options: ConfigOptions = {
filename: "Test Csv 2",
useTextFile: true,
useBom: false,
showColumnHeaders: true,
columnHeaders: ["name", "age"],
};

const output = asString(generateCsv(options)([]));

expect(output).toEqual('"name","age"\r\n');
});

it("should throw when no data supplied", () => {
const options: ConfigOptions = {
filename: "Test Csv 2",
useTextFile: true,
useBom: false,
showColumnHeaders: false,
};

expect(() => {
generateCsv(options)([]);
}).toThrow();
});

it("should allow null values", () => {
const options: ConfigOptions = {
filename: "Test Csv 2",
useTextFile: true,
useBom: false,
showColumnHeaders: true,
useKeysAsHeaders: true,
};

const output = asString(
generateCsv(options)([
{
"non-null": 24,
nullish: null,
},
]),
);

expect(output).toBe('"non-null","nullish"\r\n24,"null"\r\n');
});

it("should convert undefined to empty string by default", () => {
const options: ConfigOptions = {
filename: "Test Csv 2",
useTextFile: true,
useBom: false,
showColumnHeaders: true,
useKeysAsHeaders: true,
};

const output = asString(
generateCsv(options)([
{
car: "toyota",
color: "blue",
},
{
car: "chevrolet",
},
]),
);

expect(output).toBe(
'"car","color"\r\n"toyota","blue"\r\n"chevrolet",""\r\n',
);
});

it("should replace undefined with specified value", () => {
const options: ConfigOptions = {
filename: "Test Csv 2",
useTextFile: true,
useBom: false,
showColumnHeaders: true,
useKeysAsHeaders: true,
replaceUndefinedWith: "TEST",
};

const output = asString(
generateCsv(options)([
{
car: "toyota",
color: "blue",
},
{
car: "chevrolet",
},
]),
);

expect(output).toBe(
'"car","color"\r\n"toyota","blue"\r\n"chevrolet","TEST"\r\n',
);
});

it("should handle varying data shapes by manually setting column headers", () => {
const options: ConfigOptions = {
filename: "Test Csv 2",
useTextFile: true,
useBom: false,
showColumnHeaders: true,
columnHeaders: ["car", "color", "town"],
};

const output = asString(
generateCsv(options)([
{
car: "toyota",
color: "blue",
},
{
car: "chevrolet",
},
{
town: "montreal",
},
]),
);

expect(output).toBe(
'"car","color","town"\r\n"toyota","blue",""\r\n"chevrolet","",""\r\n"","","montreal"\r\n',
);
});

it("should properly quote headers", () => {
const options: ConfigOptions = {
filename: "Test Csv 2",
useTextFile: true,
useBom: false,
showColumnHeaders: true,
columnHeaders: ["name", "age"],
};

const output = asString(generateCsv(options)(mockData));
const firstLine = output.split("\n")[0];

expect(firstLine).toBe('"name","age"\r');
});

it("should put the title on the first line", () => {
const options: ConfigOptions = {
title: "Test Csv 2",
showTitle: true,
useBom: false,
showColumnHeaders: true,
columnHeaders: ["name", "age"],
};

const output = asString(generateCsv(options)(mockData));
const firstLine = output.split("\n")[0];

expect(firstLine).toBe("Test Csv 2\r");
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/alexcaza/export-to-csv#readme",
"devDependencies": {
"@playwright/test": "^1.37.1",
"@playwright/test": "1.42.1",
"bun-types": "^1.0.28",
"http-server": "^14.1.1",
"prettier": "3.0.3",
Expand Down
Loading