UNPKG

malwoden

Version:

![alt text](./coverage/badge-lines.svg) ![alt text](./coverage/badge-statements.svg) ![alt text](./coverage/badge-functions.svg) ![alt text](./coverage/badge-branches.svg)

149 lines 5.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var jsdom_1 = require("jsdom"); var char_code_1 = require("./char-code"); var color_1 = require("./color"); var glyph_1 = require("./glyph"); var canvas_terminal_1 = require("./canvas-terminal"); describe("RetroTerminal", function () { beforeEach(function () { var dom = new jsdom_1.JSDOM(); //@ts-ignore global.document = dom.window.document; //@ts-ignore global.window = dom.window; //@ts-ignore global.Image = window.Image; //@ts-ignore window.HTMLCanvasElement.prototype.getContext = function () { return { fillRect: function () { }, clearRect: function () { }, getImageData: function (x, y, w, h) { return { data: new Array(w * h * 4), }; }, putImageData: function () { }, createImageData: function () { return []; }, setTransform: function () { }, drawImage: function () { }, save: function () { }, fillText: function () { }, restore: function () { }, beginPath: function () { }, moveTo: function () { }, lineTo: function () { }, closePath: function () { }, stroke: function () { }, translate: function () { }, scale: function () { }, rotate: function () { }, arc: function () { }, fill: function () { }, measureText: function () { return { width: 0 }; }, transform: function () { }, rect: function () { }, clip: function () { }, }; }; }); it("Can be created with a font", function () { var font = new canvas_terminal_1.Font("Courier", 24); var terminal = new canvas_terminal_1.CanvasTerminal({ width: 20, height: 10, font: font, }); }); it("Can be mounted to a node", function () { var div = document.createElement("div"); document.body.appendChild(div); expect(div.childNodes).toHaveLength(0); var font = new canvas_terminal_1.Font("Courier", 24); var terminal = new canvas_terminal_1.CanvasTerminal({ width: 20, height: 10, font: font, mountNode: div, }); expect(div.childNodes).toHaveLength(1); }); // it("Won't render until the font is loaded", () => { // const term = new RetroTerminal({ // width: 10, // height: 10, // charWidth: 10, // charHeight: 10, // imageURL: "/public/tilemap.png", // }); // //@ts-ignore // const spy = jest.spyOn(term, "getColorFont"); // term.render(); // expect(spy).not.toHaveBeenCalled(); // }); // it("Will render once the font is loaded", () => { // const term = new RetroTerminal({ // width: 10, // height: 10, // charWidth: 10, // charHeight: 10, // imageURL: "/public/tilemap.png", // }); // const spy = jest.spyOn(term, "render"); // expect(spy).not.toHaveBeenCalled(); // const onLoadFunc = term["_font"].onload!; // expect(onLoadFunc).toBeTruthy(); // //@ts-ignore // onLoadFunc(); // expect(spy).toHaveBeenCalledTimes(1); // }); it("Can render", function () { var font = new canvas_terminal_1.Font("Courier", 24); var term = new canvas_terminal_1.CanvasTerminal({ width: 20, height: 10, font: font, }); // Will cache red term.drawCharCode({ x: 0, y: 0 }, 102, color_1.Color.Red); // The Green will be ignore, overriden by blue term.drawCharCode({ x: 1, y: 0 }, 102, color_1.Color.Green); term.drawCharCode({ x: 1, y: 0 }, 102, color_1.Color.Blue); // Only need one copy of yellow, 3 total term.drawCharCode({ x: 2, y: 0 }, 102, color_1.Color.Yellow); term.drawGlyph({ x: 3, y: 0 }, glyph_1.Glyph.fromCharCode(char_code_1.CharCode.whiteSmilingFace, color_1.Color.Yellow)); // Spaces are ignored term.drawGlyph({ x: 5, y: 0 }, new glyph_1.Glyph(" ", color_1.Color.Purple)); term.render(); }); it("Can be destroyed", function () { var font = new canvas_terminal_1.Font("Courier", 24); var term = new canvas_terminal_1.CanvasTerminal({ width: 20, height: 10, font: font, }); expect(window.document.body.childNodes).toHaveLength(1); term.delete(); expect(window.document.body.childNodes).toHaveLength(0); // Ensure it won't error on recalls term.delete(); }); it("Can get pixel to char", function () { var font = new canvas_terminal_1.Font("Courier", 24); var term = new canvas_terminal_1.CanvasTerminal({ width: 20, height: 20, font: font, }); term["charWidth"] = 16; var pos = term.windowToTilePoint({ x: 16, y: 25 }); expect(pos).toEqual({ x: 1, y: 1 }); }); }); //# sourceMappingURL=canvas-terminal.spec.js.map