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,6 +8,15 @@ describe("Bundle", () => {
bundle = new Bundle("application/vnd.apple.pkpass"); bundle = new Bundle("application/vnd.apple.pkpass");
}); });
describe("freezable", () => {
it("should expose freeze method and bundle itself to be frozen", () => {
const [bundle, freeze] = Bundle.freezable("any/any");
freeze();
expect(bundle.isFrozen).toBe(true);
});
});
describe("mimeType", () => {
it("should throw an error if no mime-type is specified in the constructor", () => { it("should throw an error if no mime-type is specified in the constructor", () => {
// @ts-expect-error // @ts-expect-error
expect(() => new Bundle()).toThrowError( expect(() => new Bundle()).toThrowError(
@@ -19,15 +28,54 @@ describe("Bundle", () => {
it("should expose the mime-type as public property", () => { it("should expose the mime-type as public property", () => {
expect(bundle.mimeType).toBe("application/vnd.apple.pkpass"); expect(bundle.mimeType).toBe("application/vnd.apple.pkpass");
}); });
});
describe("addBuffer", () => {
it("should allow to add buffers", () => { it("should allow to add buffers", () => {
const buffer = Buffer.alloc(0); const buffer = Buffer.alloc(0);
bundle.addBuffer("pass.json", buffer); 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", () => {
describe("getAsStream", () => {
it("should return a stream", () => {
addEmptyFilesToBundle(bundle);
expect(bundle.getAsStream()).toBeInstanceOf(Stream);
});
it("should freeze the bundle", () => {
bundle.getAsStream();
expect(bundle.isFrozen).toBe(true);
});
it("should throw error if a file is attempted to be added when bundle is frozen", () => {
addEmptyFilesToBundle(bundle);
bundle.getAsStream();
expect(() =>
bundle.addBuffer("icon.png", Buffer.alloc(0)),
).toThrowError(Error, "Cannot add file. Bundle is closed.");
});
});
describe("getAsBuffer", () => {
it("should return a buffer", async () => {
addEmptyFilesToBundle(bundle);
expect(await bundle.getAsBuffer()).toBeInstanceOf(Buffer);
});
it("should freeze the bundle", async () => {
await bundle.getAsBuffer();
expect(bundle.isFrozen).toBe(true);
});
it("should throw error if a file is attempted to be added when bundle is frozen", async () => {
addEmptyFilesToBundle(bundle); addEmptyFilesToBundle(bundle);
await bundle.getAsBuffer(); await bundle.getAsBuffer();
@@ -36,33 +84,7 @@ describe("Bundle", () => {
bundle.addBuffer("icon.png", Buffer.alloc(0)), bundle.addBuffer("icon.png", Buffer.alloc(0)),
).toThrowError(Error, "Cannot add file. Bundle is closed."); ).toThrowError(Error, "Cannot add file. Bundle is closed.");
}); });
it("should return a stream with 'getAsStream'", () => {
addEmptyFilesToBundle(bundle);
expect(bundle.getAsStream()).toBeInstanceOf(Stream);
}); });
it("should freeze the bundle when using 'getAsStream'", () => {
bundle.getAsStream();
expect(bundle.isFrozen).toBe(true);
});
it("should return a buffer with 'getAsBuffer'", async () => {
addEmptyFilesToBundle(bundle);
expect(await bundle.getAsBuffer()).toBeInstanceOf(Buffer);
});
it("should freeze the bundle when using 'getAsBuffer'", async () => {
await bundle.getAsBuffer();
expect(bundle.isFrozen).toBe(true);
});
it("freezables should expose freezable and bundle itself to be frozen", () => {
const [bundle, freeze] = Bundle.freezable("any/any");
freeze();
expect(bundle.isFrozen).toBe(true);
}); });
}); });