Skip to content

Commit

Permalink
Convert some specs to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeltonen committed Dec 29, 2018
1 parent 874018e commit f5f9a1b
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 200 deletions.
116 changes: 0 additions & 116 deletions spec/specs/_.coffee

This file was deleted.

146 changes: 146 additions & 0 deletions spec/specs/_.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { _ } from "../..";
import { expect } from "chai";

describe("Bacon._", () => {
it("head", () => {
expect(_.head([5,2,9])).to.equal(5);
expect(_.head([])).to.equal(undefined);
});
it("always", () => {
expect(_.always(5)()).to.equal(5)
});
it("negate", () => {
expect(_.negate(_.always(true))("timanttikobra")).to.be.false
});
it("empty", () => {
expect(_.empty([])).to.be.true;
expect(_.empty([1])).to.be.false;
});
it("tail", () => {
expect(_.tail([1,2,3])).to.deep.equal([2,3]);
expect(_.tail([1])).to.deep.equal([]);
expect(_.tail([])).to.deep.equal([]);
});
it("filter", () => {
expect(_.filter(_.empty, ["","1",[],[2]])).to.deep.equal(["",[]])
});
it("map", () =>
expect(_.map(_.head, [
[], [1], [2,2], [3,3,3]
])).to.deep.equal([
undefined, 1, 2, 3
])
);
it("flatMap", () => {
expect(_.flatMap((x: number) => [x, x], [1,2,3])).to.deep.equal([1,1,2,2,3,3])
});
describe("each", () =>
it("provides key and value to iterator", function() {
const expectKeyVals = function(x: object, expectedKeys: string[], expectedValues: Array<string | number>) {
const keys: string[] = [];
const values: string[] = [];
_.each(x, (key: string, value: string) => {
keys.push(key);
values.push(value);
});
expect([keys, values]).to.deep.equal([expectedKeys, expectedValues]);
};
expectKeyVals({ cat: "furry", bird: "feathery" }, ["cat","bird"], ["furry","feathery"]);
expectKeyVals([1,2,3], ["0","1","2"], [1,2,3]);
})
);
describe("toArray", () => {
it("works", () => {
expect(_.toArray(2)).to.deep.equal([2])
});

it("ignores rest of arguments", () => {
expect(_.toArray(1)).to.deep.equal([1])
});

it("should, when given an array, return it back (not a copy)", () => {
const arr: any[] = [];
expect(_.toArray(arr)).to.equal(arr);
});
});
it("indexOf", () => {
expect(_.indexOf([1,2], 1)).to.equal(0);
expect(_.indexOf([1,2], 2)).to.equal(1);
expect(_.indexOf([1,2], 3)).to.equal(-1);
});
it("contains", () => {
expect(_.contains([2,4,6], 4)).to.be.true;
expect(_.contains([2,4,6], 3)).to.be.false;
expect(_.contains)
});
it("id", () => {
const obj = {};
expect(_.id(obj)).to.equal(obj);
});
it("last", () => {
expect(_.last([2,4])).to.equal(4)
});
it("all", () =>
it("works", () => {
expect(_.all([ [false,true], [true,true] ], _.head)).to.be.false;
expect(_.all([ [true,false], [true,true] ], _.head)).to.be.true;
})
);
describe("any", () =>
it("works", () => {
expect(_.any([ [false,true], [true,true] ], _.head)).to.be.true;
expect(_.any([ [false,false], [false,true] ], _.head)).to.be.false;
})
);
it("without", () => {
expect(_.without("apple", ["bacon","apple","apple","omelette"])).to.deep.equal(["bacon","omelette"])
});
it("remove", () => {
expect(_.remove("apple", ["bacon","apple","apple","omelette"])).to.deep.equal(["apple"]);
expect(_.remove("raisin", ["bacon","apple","apple","omelette"])).to.deep.equal(undefined);
});
it("fold", () => {
expect(_.fold([1,2,3,4,5], 0, (s: number, n: number) => s + n)).to.equal(15)
});
describe("toString", () => {
it("for booleans", () => {
expect(_.toString(true)).to.equal("true")
});
it("for strings", () => {
expect(_.toString("lol")).to.equal("lol")
});
it("for dates", () => {
expect(_.toString(new Date((new Date(0)).getTimezoneOffset() * 60 * 1000))).to.contain("1970")
});
it("for arrays", () => {
expect(_.toString([1,2,3])).to.equal("[1,2,3]")
});
it("for numbers", () => {
expect(_.toString(1)).to.equal("1");
expect(_.toString(1.1)).to.equal("1.1");
});
it("for undefined and null", () => {
expect(_.toString(undefined)).to.equal("undefined");
expect(_.toString(null)).to.equal("undefined");
});
it("for objects", () => {
expect(_.toString({a: "b"})).to.equal("{a:b}");
expect(_.toString({a: "b", c: "d"})).to.equal("{a:b,c:d}");
});
it("for circular refs", () => {
const obj = { name : "nasty", self: {}};
obj.self = obj;
expect(_.toString(obj).length).to.be.below(100);
});
it("works even when enumerable properties throw errors on access", () => {
const obj = { "name": "madcow" };
Object.defineProperty(obj, "prop", {
enumerable: true,
get() {
throw new Error("an error");
}
});
expect(_.toString(obj)).to.equal("{name:madcow,prop:Error: an error}");
});
});
});
20 changes: 0 additions & 20 deletions spec/specs/event.coffee

This file was deleted.

22 changes: 22 additions & 0 deletions spec/specs/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as Bacon from "../..";
import { expect } from "chai";

describe("Bacon.Event", () =>
describe("String presentations", () => {
describe("Initial(1).toString", () =>
it("is 1", () => expect(new Bacon.Initial(1).toString()).to.equal("1"))
);
describe("Next({a:1i}).toString", () =>
it("is {a:1}", () => expect(new Bacon.Next({a:1}).toString()).to.equal("{a:1}"))
);
describe("Error({a:1}).toString", () =>
it("is <error> {a:1}", () => expect(new Bacon.Error({a:1}).toString()).to.equal("<error> {a:1}"))
);
describe("End.toString", () =>
it("is <end>", () => expect(new Bacon.End().toString()).to.equal("<end>"))
);
describe("inspect", () =>
it("is the same as toString", () => expect(new Bacon.Initial(1).inspect()).to.equal("1"))
);
})
);
28 changes: 0 additions & 28 deletions spec/specs/observable.coffee

This file was deleted.

40 changes: 40 additions & 0 deletions spec/specs/observable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as Bacon from "../..";
import { expect } from "chai";

describe("Observable::onEnd", () =>
it("is called on stream end", () => {
const s = new Bacon.Bus();
let ended = false;
s.onEnd(() => {
ended = true;
return undefined;
});
s.push("LOL");
expect(ended).to.deep.equal(false);
s.end();
expect(ended).to.deep.equal(true);
})
);

describe("Meta-info", function() {
const obs = Bacon.once(1).map(function() {});
describe("Observable::desc", () =>
it("returns structured description", () => {
expect(obs.desc.method).to.equal("map")
})
);

describe("Observable::deps", () =>
it("returns dependencies", function() {
expect(obs.deps().length).to.equal(1);
expect(obs.deps()[0].toString()).to.equal("Bacon.once(1)");
})
);

describe("Observable::internalDeps", () =>
it("returns \"real\" deps", function() {
expect(obs.deps().length).to.equal(1);
expect(obs.deps()[0].toString()).to.equal("Bacon.once(1)");
})
);
});
14 changes: 0 additions & 14 deletions spec/specs/packagemanagement.coffee

This file was deleted.

Loading

0 comments on commit f5f9a1b

Please sign in to comment.