Added tests for utils

This commit is contained in:
Alexander Cerutti
2021-10-13 20:54:16 +02:00
parent d43ff0aca8
commit 5c4c371c3b

View File

@@ -1,59 +1,39 @@
import { splitBufferBundle } from "../lib/utils"; import { processDate, removeHidden } from "../lib/utils";
import type { BundleUnit } from "../lib/schemas";
describe("splitBufferBundle", () => { describe("Utils", () => {
it("should split the bundle in language-organized files buffers and normal files with valid bundleUnit passed", () => { describe("removeHidden", () => {
const zeroBuffer = Buffer.alloc(0); it("should remove files that start with dot", () => {
const payload: BundleUnit = { const filesList = [
"en.lproj/thumbnail@2x.png": zeroBuffer, "a.png",
"de.lproj/background@2x.png": zeroBuffer, "b.png",
"it.lproj/thumbnail@2x.png": zeroBuffer, ".DS_Store",
"thumbnail@2x.png": zeroBuffer, "not_the_droids_you_are_looking_for.txt",
"background.png": zeroBuffer, ];
};
const result = splitBufferBundle(payload); expect(removeHidden(filesList)).toEqual([
"a.png",
expect(result).toBeDefined(); "b.png",
expect(result.length).toBe(2); "not_the_droids_you_are_looking_for.txt",
expect(result[0]).toEqual({ ]);
"en.lproj": {
"thumbnail@2x.png": zeroBuffer,
},
"de.lproj": {
"background@2x.png": zeroBuffer,
},
"it.lproj": {
"thumbnail@2x.png": zeroBuffer,
},
});
expect(result[1]).toEqual({
"thumbnail@2x.png": zeroBuffer,
"background.png": zeroBuffer,
}); });
}); });
it("should return empty partitionedBufferBundle if BundleUnit is empty object", () => { describe("processDate", () => {
const result = splitBufferBundle({}); it("should throw Invalid date if args[0] is not a date", () => {
//@ts-expect-error
expect(() => processDate(5)).toThrow("Invalid date");
//@ts-expect-error
expect(() => processDate({})).toThrow("Invalid date");
//@ts-expect-error
expect(() => processDate("ciao")).toThrow("Invalid date");
//@ts-expect-error
expect(() => processDate(true)).toThrow("Invalid date");
});
expect(result).toBeDefined(); it("should convert a Date object to a valid W3C date", () => {
expect(result.length).toBe(2); expect(processDate(new Date(2020, 6, 1, 0, 0, 0, 0))).toBe(
expect(result[0]).toEqual({}); "2020-07-01T00:00:00Z",
expect(result[1]).toEqual({}); );
}); });
it("should return empty partitionedBufferBundle if BundleUnit is undefined", () => {
const resultUndefined = splitBufferBundle(undefined);
const resultNull = splitBufferBundle(null);
expect(resultUndefined).toBeDefined();
expect(resultUndefined.length).toBe(2);
expect(resultUndefined[0]).toEqual({});
expect(resultUndefined[1]).toEqual({});
expect(resultNull).toBeDefined();
expect(resultNull.length).toBe(2);
expect(resultNull[0]).toEqual({});
expect(resultNull[1]).toEqual({});
}); });
}); });