From b214f15e87ddb1f783961d2dc6c4ca09e065b239 Mon Sep 17 00:00:00 2001 From: Harry Peach Date: Sat, 23 May 2020 11:35:24 +0100 Subject: [PATCH 1/4] Added windows alternative for tick symbol --- kia.test.ts | 13 ++++++++++++- kia.ts | 9 ++++++++- out.txt | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 out.txt diff --git a/kia.test.ts b/kia.test.ts index 0b1ed96..c2492e7 100644 --- a/kia.test.ts +++ b/kia.test.ts @@ -1,4 +1,4 @@ -import Kia from "./mod.ts"; +import Kia, { forPromise } from "./mod.ts"; import { assertThrowsAsync, assertThrows, @@ -94,3 +94,14 @@ Deno.test("set() changes the kia options", () => { expect(inArray).toBe(true); }); + +// Deno.test("forPromise succeed", () => { +// const testWriter = new TestWriter(); +// forPromise(() => {}, { writer: testWriter }); + +// console.error(testWriter.buffer[testWriter.buffer.length]); +// Deno.writeTextFileSync("out.txt", testWriter.buffer.join()); +// expect(testWriter.buffer[testWriter.buffer.length - 1].includes("√")).toBe( +// true +// ); +// }); diff --git a/kia.ts b/kia.ts index b229b72..5e5e6d4 100644 --- a/kia.ts +++ b/kia.ts @@ -141,7 +141,14 @@ export default class Kia { * @param text The message to be shown when stopped */ succeed(text: string = this.options.text) { - return this.stopWithFlair(text, Colors.bold(Colors.green("√"))); + return this.stopWithFlair( + text, + Colors.bold( + Colors.green( + Deno.build.os === "windows" ? String.fromCharCode(30) : "√" + ) + ) + ); } /** diff --git a/out.txt b/out.txt new file mode 100644 index 0000000..3881db1 --- /dev/null +++ b/out.txt @@ -0,0 +1 @@ + ⠋ ,[?25l \ No newline at end of file From aa0acdf4bbc64d748a514f97fce95ce69958c31a Mon Sep 17 00:00:00 2001 From: Harry Peach Date: Sat, 23 May 2020 12:04:42 +0100 Subject: [PATCH 2/4] Added tests for forPromise --- kia.test.ts | 68 +++++++++++++++++++++++++++++++++++++++-------------- kia.ts | 4 ++-- out.txt | 1 - 3 files changed, 52 insertions(+), 21 deletions(-) delete mode 100644 out.txt diff --git a/kia.test.ts b/kia.test.ts index c2492e7..f663d30 100644 --- a/kia.test.ts +++ b/kia.test.ts @@ -5,9 +5,11 @@ import { } from "https://deno.land/std@0.51.0/testing/asserts.ts"; import { expect } from "https://deno.land/x/expect/mod.ts"; class TestWriter implements Deno.WriterSync { - buffer: string[] = []; + buffer: number[] = []; writeSync(p: Uint8Array): number { - this.buffer.push(new TextDecoder().decode(p)); + p.forEach((pi) => { + this.buffer.push(pi); + }); return p.length; } } @@ -85,23 +87,53 @@ Deno.test("set() changes the kia options", () => { kia.set({ text: SEARCH_KEY }); kia.renderNextFrame(); - let inArray = false; - testWriter.buffer.forEach((item) => { - if (item.includes(SEARCH_KEY)) { - inArray = true; - } - }); + expect( + new TextDecoder() + .decode(Uint8Array.from(testWriter.buffer)) + .includes(SEARCH_KEY) + ).toBe(true); +}); + +Deno.test({ + name: "forPromise succeed (Not Windows)", + ignore: Deno.build.os === "windows", + fn: async () => { + const testWriter = new TestWriter(); + await forPromise(() => {}, { writer: testWriter }); + expect( + new TextDecoder() + .decode(Uint8Array.from(testWriter.buffer)) + .includes("√") + ).toBe(true); + }, +}); - expect(inArray).toBe(true); +Deno.test({ + name: "forPromise succeed (Windows)", + ignore: Deno.build.os !== "windows", + fn: async () => { + const testWriter = new TestWriter(); + await forPromise(() => {}, { writer: testWriter }); + expect( + new TextDecoder() + .decode(Uint8Array.from(testWriter.buffer)) + .includes(String.fromCharCode(30)) + ).toBe(true); + }, }); -// Deno.test("forPromise succeed", () => { -// const testWriter = new TestWriter(); -// forPromise(() => {}, { writer: testWriter }); +Deno.test("forPromise fail", async () => { + const testWriter = new TestWriter(); + await forPromise( + () => { + throw new Error(); + }, + { writer: testWriter } + ); -// console.error(testWriter.buffer[testWriter.buffer.length]); -// Deno.writeTextFileSync("out.txt", testWriter.buffer.join()); -// expect(testWriter.buffer[testWriter.buffer.length - 1].includes("√")).toBe( -// true -// ); -// }); + expect( + new TextDecoder() + .decode(Uint8Array.from(testWriter.buffer)) + .includes("X") + ).toBe(true); +}); diff --git a/kia.ts b/kia.ts index 5e5e6d4..394ac38 100644 --- a/kia.ts +++ b/kia.ts @@ -207,10 +207,10 @@ export default class Kia { /** * Starts a spinner for a promise */ -export const forPromise = (action: Function, options: InputOptions) => { +export const forPromise = async (action: Function, options: InputOptions) => { const kia = new Kia(options).start(); - (async () => { + await (async () => { try { await action(); kia.succeed(); diff --git a/out.txt b/out.txt deleted file mode 100644 index 3881db1..0000000 --- a/out.txt +++ /dev/null @@ -1 +0,0 @@ - ⠋ ,[?25l \ No newline at end of file From c1e18db1630ac5555b6a802bc88b9eb84412f217 Mon Sep 17 00:00:00 2001 From: Harry Peach Date: Sat, 23 May 2020 12:08:34 +0100 Subject: [PATCH 3/4] Fix spinner output interfering with test output --- kia.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kia.test.ts b/kia.test.ts index f663d30..50f6e8e 100644 --- a/kia.test.ts +++ b/kia.test.ts @@ -19,14 +19,14 @@ function sleep(ms: number) { } Deno.test("spinner isSpinning when running", () => { - const kia = new Kia(); + const kia = new Kia({ writer: new TestWriter() }); kia.start(); expect(kia.isSpinning()).toEqual(true); kia.stop(); }); Deno.test("spinner !isSpinning when not running", () => { - const kia = new Kia().start(); + const kia = new Kia({ writer: new TestWriter() }).start(); kia.stop(); expect(kia.isSpinning()).toEqual(false); }); @@ -66,7 +66,7 @@ Deno.test("renderNextFrame() advances the spinner", () => { }); Deno.test("check renderNextFrame can't be called if spinner is running", () => { - const kia = new Kia().start(); + const kia = new Kia({ writer: new TestWriter() }).start(); assertThrows(() => { kia.renderNextFrame(); }, Error); From 9c0dfe6486ae17ec5f8ecc29c10b6d3f17d02d86 Mon Sep 17 00:00:00 2001 From: Harry Peach Date: Sat, 23 May 2020 12:43:18 +0100 Subject: [PATCH 4/4] Added test for cursor hiding/showing --- kia.test.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/kia.test.ts b/kia.test.ts index 50f6e8e..0702bd8 100644 --- a/kia.test.ts +++ b/kia.test.ts @@ -1,8 +1,5 @@ import Kia, { forPromise } from "./mod.ts"; -import { - assertThrowsAsync, - assertThrows, -} from "https://deno.land/std@0.51.0/testing/asserts.ts"; +import { assertThrows } from "https://deno.land/std@0.52.0/testing/asserts.ts"; import { expect } from "https://deno.land/x/expect/mod.ts"; class TestWriter implements Deno.WriterSync { buffer: number[] = []; @@ -137,3 +134,14 @@ Deno.test("forPromise fail", async () => { .includes("X") ).toBe(true); }); + +Deno.test("hidden cursor is returned", () => { + const testWriter = new TestWriter(); + const kia = new Kia({ writer: testWriter }).start(); + kia.stop(); + expect( + new TextDecoder() + .decode(Uint8Array.from(testWriter.buffer)) + .includes("\x1b[?25h") + ).toBe(true); +});