forked from LuisEnMarroquin/json-as-xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
AKritskiy
authored and
AKritskiy
committed
Jul 19, 2023
1 parent
8519de5
commit b7b4d25
Showing
6 changed files
with
339 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/main-library/src/__tests__/contentProperty.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { getContentProperty } from "../index" | ||
|
||
test("Should access first level property", () => { | ||
const supportTicket = { | ||
id: "e4te583nbt", | ||
title: "Problem with server", | ||
user: "daniel31", | ||
} | ||
|
||
expect(getContentProperty(supportTicket, "user")).toBe("daniel31") | ||
|
||
expect(getContentProperty(supportTicket, "description")).toBe("") | ||
}) | ||
|
||
test("Should access second level property", () => { | ||
const employee = { | ||
name: "Sophie", | ||
age: "21", | ||
email: { | ||
work: "sophiedev@job.com", | ||
personal: "sophie@email.com", | ||
}, | ||
} | ||
|
||
expect(getContentProperty(employee, "email.work")).toBe("sophiedev@job.com") | ||
|
||
expect(getContentProperty(employee, "email.personal")).toBe("sophie@email.com") | ||
|
||
expect(getContentProperty(employee, "email.business")).toBe("") | ||
}) | ||
|
||
test("Should access third level property", () => { | ||
const purchase = { | ||
id: "934as44951", | ||
costumer: { | ||
email: "jeniffer@email.com", | ||
phoneNumber: "44444444", | ||
billingAddress: { | ||
line1: "123 Street", | ||
city: "Montreal", | ||
postalCode: 55555, | ||
}, | ||
}, | ||
} | ||
|
||
expect(getContentProperty(purchase, "costumer.billingAddress.line1")).toBe("123 Street") | ||
|
||
expect(getContentProperty(purchase, "costumer.billingAddress.city")).toBe("Montreal") | ||
|
||
expect(getContentProperty(purchase, "costumer.billingAddress.postalCode")).toBe(55555) | ||
|
||
expect(getContentProperty(purchase, "costumer.billingAddress.countryCode")).toBe("") | ||
}) |
63 changes: 63 additions & 0 deletions
63
packages/main-library/src/__tests__/getJsonSheetRow.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { getJsonSheetRow, IContent } from "../index" | ||
|
||
test("", () => { | ||
const track = { | ||
id: "4ase432i", | ||
name: "Help", | ||
artist: "Galantis", | ||
album: { | ||
id: "532o48sn3", | ||
name: "Help", | ||
albumType: "single", | ||
totalTracks: 1, | ||
releaseDate: "2014-03-11", | ||
}, | ||
explicit: false, | ||
popularity: 21, | ||
durationMs: 4010, | ||
} | ||
|
||
expect(getJsonSheetRow(track, [{ label: "ID", value: "id" }])).toEqual({ ID: "4ase432i" }) | ||
|
||
expect(getJsonSheetRow(track, [{ label: "Name", value: "name" }])).toEqual({ Name: "Help" }) | ||
|
||
expect(getJsonSheetRow(track, [{ label: "Artist", value: "artist" }])).toEqual({ Artist: "Galantis" }) | ||
|
||
expect(getJsonSheetRow(track, [{ label: "Album", value: "album.name" }])).toEqual({ Album: "Help" }) | ||
|
||
expect( | ||
getJsonSheetRow(track, [ | ||
{ | ||
label: "Explicit content", | ||
value: (content: IContent) => { | ||
return content.explicit ? "Yes" : "No" | ||
}, | ||
}, | ||
]) | ||
).toEqual({ "Explicit content": "No" }) | ||
|
||
expect(getJsonSheetRow(track, [{ label: "Popularity", value: "popularity" }])).toEqual({ Popularity: 21 }) | ||
|
||
expect( | ||
getJsonSheetRow(track, [ | ||
{ | ||
label: "Duration", | ||
value: (content: IContent) => { | ||
return (content.durationMs as number) / 1000 + "s" | ||
}, | ||
}, | ||
]) | ||
).toEqual({ Duration: "4.01s" }) | ||
|
||
expect(getJsonSheetRow(track, [{ label: "URI", value: "uri" }])).toEqual({ URI: "" }) | ||
|
||
expect(getJsonSheetRow(track, [{ label: "Album", value: "album" }])).toEqual({ | ||
Album: { | ||
id: "532o48sn3", | ||
name: "Help", | ||
albumType: "single", | ||
totalTracks: 1, | ||
releaseDate: "2014-03-11", | ||
}, | ||
}) | ||
}) |
35 changes: 35 additions & 0 deletions
35
packages/main-library/src/__tests__/getWorksheetColumWidths.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { utils } from "xlsx" | ||
import { getJsonSheetRow, getWorksheetColumnWidths } from "../index" | ||
|
||
test("Should return only one column width", () => { | ||
const content = { username: "art3mis" } | ||
const columns = [{ label: "Username", value: "username" }] | ||
|
||
const jsonSheetRow = getJsonSheetRow(content, columns) | ||
const worksheet = utils.json_to_sheet([jsonSheetRow]) | ||
|
||
// Column label is larger than content | ||
expect(getWorksheetColumnWidths(worksheet)).toEqual([{ width: 9 }]) | ||
|
||
expect(getWorksheetColumnWidths(worksheet, 0)).toEqual([{ width: 8 }]) | ||
|
||
expect(getWorksheetColumnWidths(worksheet, 4)).toEqual([{ width: 12 }]) | ||
}) | ||
|
||
test("Should return two column widths", () => { | ||
const content = { name: "Interstellar", year: 2014 } | ||
const columns = [ | ||
{ label: "Movie", value: "name" }, | ||
{ label: "Year", value: "year" }, | ||
] | ||
|
||
const jsonSheetRow = getJsonSheetRow(content, columns) | ||
const worksheet = utils.json_to_sheet([jsonSheetRow]) | ||
|
||
// Content is larger than column label | ||
expect(getWorksheetColumnWidths(worksheet)).toEqual([{ width: 13 }, { width: 5 }]) | ||
|
||
expect(getWorksheetColumnWidths(worksheet, 0)).toEqual([{ width: 12 }, { width: 4 }]) | ||
|
||
expect(getWorksheetColumnWidths(worksheet, 4)).toEqual([{ width: 16 }, { width: 8 }]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
import { read as readBufferWorkBook } from "xlsx" | ||
import jsonxlsx, { IContent, IJsonSheet, ISettings } from "../index" | ||
|
||
describe("json-as-xlsx", () => { | ||
it("should return undefined if sheets array is empty", () => { | ||
const sheets: IJsonSheet[] = [] | ||
const result = jsonxlsx(sheets) | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
describe("writeOptions.type is set to buffer", () => { | ||
const settings: ISettings = { | ||
writeOptions: { | ||
type: "buffer", | ||
}, | ||
} | ||
|
||
it("should return buffer", () => { | ||
const sheets = [ | ||
{ | ||
columns: [{ label: "Name", value: "name" }], | ||
content: [{ name: "Martin" }], | ||
}, | ||
] | ||
const buffer = jsonxlsx(sheets, settings) | ||
expect(buffer).toBeInstanceOf(Buffer) | ||
}) | ||
|
||
it("should return parsable xlsx buffer", () => { | ||
const sheets = [ | ||
{ | ||
columns: [{ label: "Name", value: "name" }], | ||
content: [{ name: "Martin" }], | ||
}, | ||
] | ||
const buffer = jsonxlsx(sheets, settings) | ||
const workBook = readBufferWorkBook(buffer) | ||
|
||
expect(workBook).toBeDefined() | ||
}) | ||
|
||
it("should return one column filled cells", () => { | ||
const sheets = [ | ||
{ | ||
sheet: "Authors", | ||
columns: [{ label: "Name", value: "name" }], | ||
content: [{ name: "Martin" }, { name: "Kent" }], | ||
}, | ||
] | ||
const buffer = jsonxlsx(sheets, settings) | ||
const workBook = readBufferWorkBook(buffer) | ||
const workSheet = workBook.Sheets.Authors | ||
|
||
expect(workSheet["!ref"]).toBe("A1:A3") | ||
expect(workSheet.A1.v).toBe("Name") | ||
expect(workSheet.A2.v).toBe("Martin") | ||
expect(workSheet.A3.v).toBe("Kent") | ||
}) | ||
|
||
it("should return only the column headers if there is no content", () => { | ||
const sheetName = "Authors" | ||
const sheets = [ | ||
{ | ||
sheet: sheetName, | ||
columns: [ | ||
{ label: "Name", value: "name" }, | ||
{ label: "Age", value: "age" }, | ||
], | ||
content: [], | ||
}, | ||
] | ||
const buffer = jsonxlsx(sheets, settings) | ||
const workBook = readBufferWorkBook(buffer) | ||
const workSheet = workBook.Sheets[sheetName] | ||
|
||
expect(workSheet.A1.v).toBe("Name") | ||
expect(workSheet.B1.v).toBe("Age") | ||
}) | ||
|
||
it("should handle deep props", () => { | ||
const sheets = [ | ||
{ | ||
sheet: "Users", | ||
columns: [{ label: "IP", value: "metadata.ip" }], | ||
content: [ | ||
{ name: "Martin", metadata: { ip: "0.0.0.0" } }, | ||
{ name: "Robert", metadata: { ip: "0.0.0.1" } }, | ||
], | ||
}, | ||
] | ||
const buffer = jsonxlsx(sheets, settings) | ||
const workBook = readBufferWorkBook(buffer) | ||
const workSheet = workBook.Sheets.Users | ||
|
||
expect(workSheet.A1.v).toBe("IP") | ||
expect(workSheet.A2.v).toBe("0.0.0.0") | ||
expect(workSheet.A3.v).toBe("0.0.0.1") | ||
}) | ||
|
||
it("should handle function column value", () => { | ||
const maskPhoneNumber = (cell: IContent): string => new String(cell.phone).replace(/^(\d{3})(\d{4}).*/, "$1-$2") | ||
|
||
const sheets = [ | ||
{ | ||
sheet: "Users", | ||
columns: [{ label: "Phone number", value: maskPhoneNumber }], | ||
content: [ | ||
{ name: "Martin", phone: "1234567" }, | ||
{ name: "Robert", phone: "1234568" }, | ||
], | ||
}, | ||
] | ||
const buffer = jsonxlsx(sheets, settings) | ||
const workBook = readBufferWorkBook(buffer) | ||
const workSheet = workBook.Sheets.Users | ||
|
||
expect(workSheet.A1.v).toBe("Phone number") | ||
expect(workSheet.A2.v).toBe("123-4567") | ||
expect(workSheet.A3.v).toBe("123-4568") | ||
}) | ||
|
||
it("should call optional callback", () => { | ||
const callback = jest.fn() | ||
const sheets = [ | ||
{ | ||
columns: [{ label: "Name", value: "name" }], | ||
content: [{ name: "Martin" }], | ||
}, | ||
] | ||
jsonxlsx(sheets, settings, callback) | ||
|
||
expect(callback).toBeCalledTimes(1) | ||
}) | ||
|
||
it("should handle multiple sheets", () => { | ||
const sheets = [ | ||
{ | ||
sheet: "Authors", | ||
columns: [ | ||
{ label: "Name", value: "name" }, | ||
{ label: "Age", value: "age" }, | ||
], | ||
content: [ | ||
{ name: "Martin", age: 50 }, | ||
{ name: "Robert", age: 20 }, | ||
{ name: "Andrea", age: 35 }, | ||
], | ||
}, | ||
{ | ||
sheet: "Books", | ||
columns: [ | ||
{ label: "Title", value: "title" }, | ||
{ label: "Author", value: "author" }, | ||
], | ||
content: [ | ||
{ title: "TDD", author: "Martin" }, | ||
{ title: "Git", author: "Robert" }, | ||
], | ||
}, | ||
] | ||
const buffer = jsonxlsx(sheets, settings) | ||
const workBook = readBufferWorkBook(buffer) | ||
|
||
const authorsSheet = workBook.Sheets.Authors | ||
const booksSheet = workBook.Sheets.Books | ||
|
||
expect(workBook.SheetNames).toEqual(["Authors", "Books"]) | ||
|
||
expect(authorsSheet.A1.v).toBe("Name") | ||
expect(authorsSheet.B1.v).toBe("Age") | ||
expect(authorsSheet.A2.v).toBe("Martin") | ||
expect(authorsSheet.B2.v).toBe(50) | ||
expect(authorsSheet.A3.v).toBe("Robert") | ||
expect(authorsSheet.B3.v).toBe(20) | ||
expect(authorsSheet.A4.v).toBe("Andrea") | ||
expect(authorsSheet.B4.v).toBe(35) | ||
|
||
expect(booksSheet.A1.v).toBe("Title") | ||
expect(booksSheet.B1.v).toBe("Author") | ||
expect(booksSheet.A2.v).toBe("TDD") | ||
expect(booksSheet.B2.v).toBe("Martin") | ||
expect(booksSheet.A3.v).toBe("Git") | ||
expect(booksSheet.B3.v).toBe("Robert") | ||
}) | ||
}) | ||
}) |