-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from HarryPeach/dev
Dev branch changes
- Loading branch information
Showing
2 changed files
with
78 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
@@ -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); | ||
}); | ||
|
@@ -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); | ||
|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters