Skip to content

Commit

Permalink
Merge pull request #5 from HarryPeach/dev
Browse files Browse the repository at this point in the history
Dev branch changes
  • Loading branch information
HarryPeach authored Sep 16, 2020
2 parents aa412b6 + 9c0dfe6 commit 68652bd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 20 deletions.
85 changes: 68 additions & 17 deletions kia.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import Kia from "./mod.ts";
import {
assertThrowsAsync,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import Kia, { forPromise } from "./mod.ts";
import { assertThrows } from "https://deno.land/[email protected]/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;
}
}
Expand All @@ -17,14 +16,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);
});
Expand Down Expand Up @@ -64,7 +63,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);
Expand All @@ -85,12 +84,64 @@ 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);
},
});

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 fail", async () => {
const testWriter = new TestWriter();
await forPromise(
() => {
throw new Error();
},
{ writer: testWriter }
);

expect(
new TextDecoder()
.decode(Uint8Array.from(testWriter.buffer))
.includes("X")
).toBe(true);
});

expect(inArray).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);
});
13 changes: 10 additions & 3 deletions kia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) : "√"
)
)
);
}

/**
Expand Down Expand Up @@ -200,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();
Expand Down

0 comments on commit 68652bd

Please sign in to comment.