Skip to content

Commit b7b4d25

Browse files
AKritskiyAKritskiy
authored andcommitted
added tests
1 parent 8519de5 commit b7b4d25

File tree

6 files changed

+339
-2
lines changed

6 files changed

+339
-2
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
steps:
1313
- uses: actions/checkout@v2
1414
- run: yarn
15-
# - run: yarn test
15+
- run: yarn test
1616
- run: yarn build
1717
- name: Copy files to library output folder
1818
run: |

packages/main-library/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "export-json-as-xlsx",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { getContentProperty } from "../index"
2+
3+
test("Should access first level property", () => {
4+
const supportTicket = {
5+
id: "e4te583nbt",
6+
title: "Problem with server",
7+
user: "daniel31",
8+
}
9+
10+
expect(getContentProperty(supportTicket, "user")).toBe("daniel31")
11+
12+
expect(getContentProperty(supportTicket, "description")).toBe("")
13+
})
14+
15+
test("Should access second level property", () => {
16+
const employee = {
17+
name: "Sophie",
18+
age: "21",
19+
email: {
20+
21+
personal: "[email protected]",
22+
},
23+
}
24+
25+
expect(getContentProperty(employee, "email.work")).toBe("[email protected]")
26+
27+
expect(getContentProperty(employee, "email.personal")).toBe("[email protected]")
28+
29+
expect(getContentProperty(employee, "email.business")).toBe("")
30+
})
31+
32+
test("Should access third level property", () => {
33+
const purchase = {
34+
id: "934as44951",
35+
costumer: {
36+
37+
phoneNumber: "44444444",
38+
billingAddress: {
39+
line1: "123 Street",
40+
city: "Montreal",
41+
postalCode: 55555,
42+
},
43+
},
44+
}
45+
46+
expect(getContentProperty(purchase, "costumer.billingAddress.line1")).toBe("123 Street")
47+
48+
expect(getContentProperty(purchase, "costumer.billingAddress.city")).toBe("Montreal")
49+
50+
expect(getContentProperty(purchase, "costumer.billingAddress.postalCode")).toBe(55555)
51+
52+
expect(getContentProperty(purchase, "costumer.billingAddress.countryCode")).toBe("")
53+
})
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { getJsonSheetRow, IContent } from "../index"
2+
3+
test("", () => {
4+
const track = {
5+
id: "4ase432i",
6+
name: "Help",
7+
artist: "Galantis",
8+
album: {
9+
id: "532o48sn3",
10+
name: "Help",
11+
albumType: "single",
12+
totalTracks: 1,
13+
releaseDate: "2014-03-11",
14+
},
15+
explicit: false,
16+
popularity: 21,
17+
durationMs: 4010,
18+
}
19+
20+
expect(getJsonSheetRow(track, [{ label: "ID", value: "id" }])).toEqual({ ID: "4ase432i" })
21+
22+
expect(getJsonSheetRow(track, [{ label: "Name", value: "name" }])).toEqual({ Name: "Help" })
23+
24+
expect(getJsonSheetRow(track, [{ label: "Artist", value: "artist" }])).toEqual({ Artist: "Galantis" })
25+
26+
expect(getJsonSheetRow(track, [{ label: "Album", value: "album.name" }])).toEqual({ Album: "Help" })
27+
28+
expect(
29+
getJsonSheetRow(track, [
30+
{
31+
label: "Explicit content",
32+
value: (content: IContent) => {
33+
return content.explicit ? "Yes" : "No"
34+
},
35+
},
36+
])
37+
).toEqual({ "Explicit content": "No" })
38+
39+
expect(getJsonSheetRow(track, [{ label: "Popularity", value: "popularity" }])).toEqual({ Popularity: 21 })
40+
41+
expect(
42+
getJsonSheetRow(track, [
43+
{
44+
label: "Duration",
45+
value: (content: IContent) => {
46+
return (content.durationMs as number) / 1000 + "s"
47+
},
48+
},
49+
])
50+
).toEqual({ Duration: "4.01s" })
51+
52+
expect(getJsonSheetRow(track, [{ label: "URI", value: "uri" }])).toEqual({ URI: "" })
53+
54+
expect(getJsonSheetRow(track, [{ label: "Album", value: "album" }])).toEqual({
55+
Album: {
56+
id: "532o48sn3",
57+
name: "Help",
58+
albumType: "single",
59+
totalTracks: 1,
60+
releaseDate: "2014-03-11",
61+
},
62+
})
63+
})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { utils } from "xlsx"
2+
import { getJsonSheetRow, getWorksheetColumnWidths } from "../index"
3+
4+
test("Should return only one column width", () => {
5+
const content = { username: "art3mis" }
6+
const columns = [{ label: "Username", value: "username" }]
7+
8+
const jsonSheetRow = getJsonSheetRow(content, columns)
9+
const worksheet = utils.json_to_sheet([jsonSheetRow])
10+
11+
// Column label is larger than content
12+
expect(getWorksheetColumnWidths(worksheet)).toEqual([{ width: 9 }])
13+
14+
expect(getWorksheetColumnWidths(worksheet, 0)).toEqual([{ width: 8 }])
15+
16+
expect(getWorksheetColumnWidths(worksheet, 4)).toEqual([{ width: 12 }])
17+
})
18+
19+
test("Should return two column widths", () => {
20+
const content = { name: "Interstellar", year: 2014 }
21+
const columns = [
22+
{ label: "Movie", value: "name" },
23+
{ label: "Year", value: "year" },
24+
]
25+
26+
const jsonSheetRow = getJsonSheetRow(content, columns)
27+
const worksheet = utils.json_to_sheet([jsonSheetRow])
28+
29+
// Content is larger than column label
30+
expect(getWorksheetColumnWidths(worksheet)).toEqual([{ width: 13 }, { width: 5 }])
31+
32+
expect(getWorksheetColumnWidths(worksheet, 0)).toEqual([{ width: 12 }, { width: 4 }])
33+
34+
expect(getWorksheetColumnWidths(worksheet, 4)).toEqual([{ width: 16 }, { width: 8 }])
35+
})
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import { read as readBufferWorkBook } from "xlsx"
2+
import jsonxlsx, { IContent, IJsonSheet, ISettings } from "../index"
3+
4+
describe("json-as-xlsx", () => {
5+
it("should return undefined if sheets array is empty", () => {
6+
const sheets: IJsonSheet[] = []
7+
const result = jsonxlsx(sheets)
8+
expect(result).toBeUndefined()
9+
})
10+
11+
describe("writeOptions.type is set to buffer", () => {
12+
const settings: ISettings = {
13+
writeOptions: {
14+
type: "buffer",
15+
},
16+
}
17+
18+
it("should return buffer", () => {
19+
const sheets = [
20+
{
21+
columns: [{ label: "Name", value: "name" }],
22+
content: [{ name: "Martin" }],
23+
},
24+
]
25+
const buffer = jsonxlsx(sheets, settings)
26+
expect(buffer).toBeInstanceOf(Buffer)
27+
})
28+
29+
it("should return parsable xlsx buffer", () => {
30+
const sheets = [
31+
{
32+
columns: [{ label: "Name", value: "name" }],
33+
content: [{ name: "Martin" }],
34+
},
35+
]
36+
const buffer = jsonxlsx(sheets, settings)
37+
const workBook = readBufferWorkBook(buffer)
38+
39+
expect(workBook).toBeDefined()
40+
})
41+
42+
it("should return one column filled cells", () => {
43+
const sheets = [
44+
{
45+
sheet: "Authors",
46+
columns: [{ label: "Name", value: "name" }],
47+
content: [{ name: "Martin" }, { name: "Kent" }],
48+
},
49+
]
50+
const buffer = jsonxlsx(sheets, settings)
51+
const workBook = readBufferWorkBook(buffer)
52+
const workSheet = workBook.Sheets.Authors
53+
54+
expect(workSheet["!ref"]).toBe("A1:A3")
55+
expect(workSheet.A1.v).toBe("Name")
56+
expect(workSheet.A2.v).toBe("Martin")
57+
expect(workSheet.A3.v).toBe("Kent")
58+
})
59+
60+
it("should return only the column headers if there is no content", () => {
61+
const sheetName = "Authors"
62+
const sheets = [
63+
{
64+
sheet: sheetName,
65+
columns: [
66+
{ label: "Name", value: "name" },
67+
{ label: "Age", value: "age" },
68+
],
69+
content: [],
70+
},
71+
]
72+
const buffer = jsonxlsx(sheets, settings)
73+
const workBook = readBufferWorkBook(buffer)
74+
const workSheet = workBook.Sheets[sheetName]
75+
76+
expect(workSheet.A1.v).toBe("Name")
77+
expect(workSheet.B1.v).toBe("Age")
78+
})
79+
80+
it("should handle deep props", () => {
81+
const sheets = [
82+
{
83+
sheet: "Users",
84+
columns: [{ label: "IP", value: "metadata.ip" }],
85+
content: [
86+
{ name: "Martin", metadata: { ip: "0.0.0.0" } },
87+
{ name: "Robert", metadata: { ip: "0.0.0.1" } },
88+
],
89+
},
90+
]
91+
const buffer = jsonxlsx(sheets, settings)
92+
const workBook = readBufferWorkBook(buffer)
93+
const workSheet = workBook.Sheets.Users
94+
95+
expect(workSheet.A1.v).toBe("IP")
96+
expect(workSheet.A2.v).toBe("0.0.0.0")
97+
expect(workSheet.A3.v).toBe("0.0.0.1")
98+
})
99+
100+
it("should handle function column value", () => {
101+
const maskPhoneNumber = (cell: IContent): string => new String(cell.phone).replace(/^(\d{3})(\d{4}).*/, "$1-$2")
102+
103+
const sheets = [
104+
{
105+
sheet: "Users",
106+
columns: [{ label: "Phone number", value: maskPhoneNumber }],
107+
content: [
108+
{ name: "Martin", phone: "1234567" },
109+
{ name: "Robert", phone: "1234568" },
110+
],
111+
},
112+
]
113+
const buffer = jsonxlsx(sheets, settings)
114+
const workBook = readBufferWorkBook(buffer)
115+
const workSheet = workBook.Sheets.Users
116+
117+
expect(workSheet.A1.v).toBe("Phone number")
118+
expect(workSheet.A2.v).toBe("123-4567")
119+
expect(workSheet.A3.v).toBe("123-4568")
120+
})
121+
122+
it("should call optional callback", () => {
123+
const callback = jest.fn()
124+
const sheets = [
125+
{
126+
columns: [{ label: "Name", value: "name" }],
127+
content: [{ name: "Martin" }],
128+
},
129+
]
130+
jsonxlsx(sheets, settings, callback)
131+
132+
expect(callback).toBeCalledTimes(1)
133+
})
134+
135+
it("should handle multiple sheets", () => {
136+
const sheets = [
137+
{
138+
sheet: "Authors",
139+
columns: [
140+
{ label: "Name", value: "name" },
141+
{ label: "Age", value: "age" },
142+
],
143+
content: [
144+
{ name: "Martin", age: 50 },
145+
{ name: "Robert", age: 20 },
146+
{ name: "Andrea", age: 35 },
147+
],
148+
},
149+
{
150+
sheet: "Books",
151+
columns: [
152+
{ label: "Title", value: "title" },
153+
{ label: "Author", value: "author" },
154+
],
155+
content: [
156+
{ title: "TDD", author: "Martin" },
157+
{ title: "Git", author: "Robert" },
158+
],
159+
},
160+
]
161+
const buffer = jsonxlsx(sheets, settings)
162+
const workBook = readBufferWorkBook(buffer)
163+
164+
const authorsSheet = workBook.Sheets.Authors
165+
const booksSheet = workBook.Sheets.Books
166+
167+
expect(workBook.SheetNames).toEqual(["Authors", "Books"])
168+
169+
expect(authorsSheet.A1.v).toBe("Name")
170+
expect(authorsSheet.B1.v).toBe("Age")
171+
expect(authorsSheet.A2.v).toBe("Martin")
172+
expect(authorsSheet.B2.v).toBe(50)
173+
expect(authorsSheet.A3.v).toBe("Robert")
174+
expect(authorsSheet.B3.v).toBe(20)
175+
expect(authorsSheet.A4.v).toBe("Andrea")
176+
expect(authorsSheet.B4.v).toBe(35)
177+
178+
expect(booksSheet.A1.v).toBe("Title")
179+
expect(booksSheet.B1.v).toBe("Author")
180+
expect(booksSheet.A2.v).toBe("TDD")
181+
expect(booksSheet.B2.v).toBe("Martin")
182+
expect(booksSheet.A3.v).toBe("Git")
183+
expect(booksSheet.B3.v).toBe("Robert")
184+
})
185+
})
186+
})

0 commit comments

Comments
 (0)