Refactored Bundle tests to be better contextualized

This commit is contained in:
Alexander Cerutti
2021-10-14 17:47:00 +02:00
parent b584269bc0
commit 55828591ca

View File

@@ -8,61 +8,83 @@ describe("Bundle", () => {
bundle = new Bundle("application/vnd.apple.pkpass"); bundle = new Bundle("application/vnd.apple.pkpass");
}); });
it("should throw an error if no mime-type is specified in the constructor", () => { describe("freezable", () => {
// @ts-expect-error it("should expose freeze method and bundle itself to be frozen", () => {
expect(() => new Bundle()).toThrowError( const [bundle, freeze] = Bundle.freezable("any/any");
Error, freeze();
"Cannot build Bundle. MimeType is missing", expect(bundle.isFrozen).toBe(true);
); });
}); });
it("should expose the mime-type as public property", () => { describe("mimeType", () => {
expect(bundle.mimeType).toBe("application/vnd.apple.pkpass"); it("should throw an error if no mime-type is specified in the constructor", () => {
// @ts-expect-error
expect(() => new Bundle()).toThrowError(
Error,
"Cannot build Bundle. MimeType is missing",
);
});
it("should expose the mime-type as public property", () => {
expect(bundle.mimeType).toBe("application/vnd.apple.pkpass");
});
}); });
it("should allow to add buffers", () => { describe("addBuffer", () => {
const buffer = Buffer.alloc(0); it("should allow to add buffers", () => {
bundle.addBuffer("pass.json", buffer); const buffer = Buffer.alloc(0);
bundle.addBuffer("pass.json", buffer);
expect(bundle[filesSymbol]).toEqual({ "pass.json": buffer }); expect(bundle[filesSymbol]).toEqual({ "pass.json": buffer });
});
}); });
it("should throw error if freezed", async () => { describe("exporting", () => {
addEmptyFilesToBundle(bundle); describe("getAsStream", () => {
it("should return a stream", () => {
addEmptyFilesToBundle(bundle);
await bundle.getAsBuffer(); expect(bundle.getAsStream()).toBeInstanceOf(Stream);
});
expect(() => it("should freeze the bundle", () => {
bundle.addBuffer("icon.png", Buffer.alloc(0)), bundle.getAsStream();
).toThrowError(Error, "Cannot add file. Bundle is closed."); expect(bundle.isFrozen).toBe(true);
}); });
it("should return a stream with 'getAsStream'", () => { it("should throw error if a file is attempted to be added when bundle is frozen", () => {
addEmptyFilesToBundle(bundle); addEmptyFilesToBundle(bundle);
expect(bundle.getAsStream()).toBeInstanceOf(Stream); bundle.getAsStream();
});
it("should freeze the bundle when using 'getAsStream'", () => { expect(() =>
bundle.getAsStream(); bundle.addBuffer("icon.png", Buffer.alloc(0)),
expect(bundle.isFrozen).toBe(true); ).toThrowError(Error, "Cannot add file. Bundle is closed.");
}); });
});
it("should return a buffer with 'getAsBuffer'", async () => { describe("getAsBuffer", () => {
addEmptyFilesToBundle(bundle); it("should return a buffer", async () => {
addEmptyFilesToBundle(bundle);
expect(await bundle.getAsBuffer()).toBeInstanceOf(Buffer); expect(await bundle.getAsBuffer()).toBeInstanceOf(Buffer);
}); });
it("should freeze the bundle when using 'getAsBuffer'", async () => { it("should freeze the bundle", async () => {
await bundle.getAsBuffer(); await bundle.getAsBuffer();
expect(bundle.isFrozen).toBe(true); expect(bundle.isFrozen).toBe(true);
}); });
it("freezables should expose freezable and bundle itself to be frozen", () => { it("should throw error if a file is attempted to be added when bundle is frozen", async () => {
const [bundle, freeze] = Bundle.freezable("any/any"); addEmptyFilesToBundle(bundle);
freeze();
expect(bundle.isFrozen).toBe(true); await bundle.getAsBuffer();
expect(() =>
bundle.addBuffer("icon.png", Buffer.alloc(0)),
).toThrowError(Error, "Cannot add file. Bundle is closed.");
});
});
}); });
}); });