Skip to content

Commit

Permalink
bugfix: added failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Sep 8, 2024
1 parent ed6eb66 commit 8c94bc0
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 22 deletions.
102 changes: 102 additions & 0 deletions __tests__/compare.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {calculateTotalRow} from "../src/compare";

describe("calculateTotalRow", () => {
test("empty returns zero", () => {
expect(calculateTotalRow([])).toEqual({
"baseBytes": 0,
"bytes": 0,
"metafile": "(Total)",
"outfile": "-",
"remark": "no-change",
"tree": undefined,
})
})

describe("single row", () => {
test("given decreased, return decreased", () => {
expect(calculateTotalRow([{
"baseBytes": 100,
"bytes": 50,
"metafile": "",
"outfile": "",
"remark": "decreased",
"tree": undefined,
}])).toEqual({
"baseBytes": 100,
"bytes": 50,
"metafile": "(Total)",
"outfile": "-",
"remark": "decreased",
"tree": undefined,
})
})
test("given increased, return increased", () => {
expect(calculateTotalRow([{
"baseBytes": 50,
"bytes": 100,
"metafile": "",
"outfile": "",
"remark": "increased",
"tree": undefined,
}])).toEqual({
"baseBytes": 50,
"bytes": 100,
"metafile": "(Total)",
"outfile": "-",
"remark": "increased",
"tree": undefined,
})
})
test("given no-change, return no-change", () => {
expect(calculateTotalRow([{
"baseBytes": 50,
"bytes": 50,
"metafile": "",
"outfile": "",
"remark": "no-change",
"tree": undefined,
}])).toEqual({
"baseBytes": 50,
"bytes": 50,
"metafile": "(Total)",
"outfile": "-",
"remark": "no-change",
"tree": undefined,
})
})
test("given added, return increased", () => {
expect(calculateTotalRow([{
"baseBytes": 0,
"bytes": 50,
"metafile": "",
"outfile": "",
"remark": "added",
"tree": undefined,
}])).toEqual({
"baseBytes": 0,
"bytes": 50,
"metafile": "(Total)",
"outfile": "-",
"remark": "increased",
"tree": undefined,
})
})
test("given deleted, return decreased", () => {
expect(calculateTotalRow([{
"baseBytes": 50,
"bytes": 0,
"metafile": "",
"outfile": "",
"remark": "deleted",
"tree": undefined,
}])).toEqual({
"baseBytes": 50,
"bytes": 0,
"metafile": "(Total)",
"outfile": "-",
"remark": "decreased",
"tree": undefined,
})
})
})
})
47 changes: 25 additions & 22 deletions src/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,35 +234,38 @@ function filesize(bytes: number): string {
throw new Error("Too large file size!! Are you sure?");
}

export function calculateTotalRow(data: Array<CompareResult>): CompareResult {
const totalRow = data.reduce(
(acc, d) => {
const { bytes, baseBytes, ...rest } = acc;
return {
...rest,
baseBytes: baseBytes + d.baseBytes,
bytes: bytes + d.bytes,
};
},
{
baseBytes: 0,
bytes: 0,
metafile: "(Total)",
outfile: "-",
remark: "added",
tree: undefined,
},
);
totalRow.remark =
totalRow.bytes > totalRow.baseBytes ? "increased" : "decreased";
return totalRow
}

function markdownTable(
data: Array<CompareResult>,
sizeComparisonFilters: Set<SizeComparisonFilter>,
redThreshold: number,
): string {
const totalRow = data.reduce(
(acc, d) => {
const { bytes, baseBytes, ...rest } = acc;
return {
...rest,
baseBytes: baseBytes + d.baseBytes,
bytes: bytes + d.bytes,
};
},
{
baseBytes: 0,
bytes: 0,
metafile: "(Total)",
outfile: "-",
remark: "added",
tree: undefined,
},
);
totalRow.remark =
totalRow.bytes > totalRow.baseBytes ? "increased" : "decreased";
const totalRows: Array<CompareResult> = sizeComparisonFilters.has("total")
? [totalRow]
? [calculateTotalRow(data)]
: [];

const individualRows = data.filter((d) =>
sizeComparisonFilters.has(d.remark),
);
Expand Down

0 comments on commit 8c94bc0

Please sign in to comment.