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)

165 lines 6.01 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 retro_terminal_1 = require("./retro-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 from a URL", function () { var term = new retro_terminal_1.RetroTerminal({ width: 10, height: 10, charWidth: 10, charHeight: 10, imageURL: "/public/tilemap.png", }); var fullOptions = new retro_terminal_1.RetroTerminal({ width: 10, height: 10, charWidth: 10, charHeight: 10, foreColor: color_1.Color.Green, backColor: color_1.Color.Blue, imageURL: "/public/tilemap.png", }); }); it("Can be mounted to a node", function () { var div = document.createElement("div"); document.body.appendChild(div); expect(div.childNodes).toHaveLength(0); var term = new retro_terminal_1.RetroTerminal({ width: 10, height: 10, charWidth: 10, charHeight: 10, imageURL: "/public/tilemap.png", mountNode: div, }); expect(div.childNodes).toHaveLength(1); }); it("Won't render until the font is loaded", function () { var term = new retro_terminal_1.RetroTerminal({ width: 10, height: 10, charWidth: 10, charHeight: 10, imageURL: "/public/tilemap.png", }); //@ts-ignore var spy = jest.spyOn(term, "getColorFont"); term.render(); expect(spy).not.toHaveBeenCalled(); }); it("Will render once the font is loaded", function () { var term = new retro_terminal_1.RetroTerminal({ width: 10, height: 10, charWidth: 10, charHeight: 10, imageURL: "/public/tilemap.png", }); var spy = jest.spyOn(term, "render"); expect(spy).not.toHaveBeenCalled(); var onLoadFunc = term["_font"].onload; expect(onLoadFunc).toBeTruthy(); //@ts-ignore onLoadFunc(); expect(spy).toHaveBeenCalledTimes(1); }); it("Can cache and render multiple colors", function () { var term = new retro_terminal_1.RetroTerminal({ width: 10, height: 10, charWidth: 10, charHeight: 10, imageURL: "/public/tilemap.png", }); // 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)); // Force the image to be loaded term["_imageLoaded"] = true; term.render(); expect(term["_fontColorCache"].size).toEqual(3); }); it("Can be destroyed", function () { var term = new retro_terminal_1.RetroTerminal({ width: 10, height: 10, charWidth: 10, charHeight: 10, imageURL: "/public/tilemap.png", }); 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 term = new retro_terminal_1.RetroTerminal({ width: 10, height: 10, charWidth: 10, charHeight: 10, imageURL: "/public/tilemap.png", }); var pos = term.windowToTilePoint({ x: 10, y: 10 }); expect(pos).toEqual({ x: 1, y: 1 }); }); }); //# sourceMappingURL=retro-terminal.spec.js.map